How to create parameters using dynet

This recipe helps you create parameters using dynet

Recipe Objective - How to create parameters using dynet?

In DyNET parameters needs to be trained. System like Torch where modules have their own parameters, in DyNET parameters are just parameters.

DyNET provides the "ParameterCollection()" module for parameters.

Creating Parameters:-

import dynet as dy
import numpy as np
# First we create a parameter collection and add the parameters to it.
pc = dy.ParameterCollection()
mat1 = m.add_parameters((8,8)) # an 8x8 matrix, return an expr
mat2= m.add_parameters(8) # an 8x1 vector, return as expr

Several ways to initial parameters:-

# Specifiying parameter initialization
scale, mean, stddev = 1, 0, 1

# Creates 3x5 matrix filled with 0 (or any other float)
par1 = m.add_parameters((3,5), init=0)

# Creates 3x5 matrix initialized with U([-scale, scale])
par2 = m.add_parameters((3,5), init='uniform', scale=scale)

# Creates 3x5 matrix initialized with N(mean, stddev)
par3 = m.add_parameters((3,5), init='normal', mean=mean, std=stddev)

# Creates 5x5 identity matrix
par4 = m.add_parameters((5,5), init='identity')

# Creates 3x5 matrix with glorot init
par5 = m.add_parameters((3,5), init='glorot')

par6 = m.add_parameters((3,5)) # By default, the init = 'glorot'

# Creates 3x5 matrix with he init
par7 = m.add_parameters((3,5), init='he')

# Creates 3x5 matrix from a numpy array (size is inferred)
par8 = m.add_parameters((3,5), np.ones((3,5)))

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.