How to manipulate expression using dynet

This recipe helps you manipulate expression using dynet

Recipe Objective - How to manipulate expression using dynet?

DyNET provides numbers of operation on expression. We can use those operation to manipulate the expressions.

Explore the Real-World Applications of Recommender Systems

Creating Vector Expression:-

import dynet as dy
import numpy as np

# Fist we create some vector Expressions.
exp1 = dy.vecInput(4)
exp1.set([1, 2, 3, 4])

exp2 = dy.vecInput(4)
exp2.set([5, 6, 7, 8])

# Concatenate list of expressions to a single batched expression.
# All input expressions must have the same shape.
e_batch = dy.concatenate_to_batch([exp1, exp2])

math1 = dy.inputTensor(np.array([[4, 2], [0, 3], [1, 3], [5, 6]])) # A 4x2 matrix
math2 = dy.inputTensor(np.array([[0, 2], [1, 2]])) # A 2x2 matrix

Basic Math Problem:-

# Add
exp_add = exp1 + exp2 # Element-wise addition

# Minus
exp_sub = exp2 - exp1 # Element-wise minus

# Negative
exp_neg = -exp1 # Should be [-1.0, -2.0, -3.0, -4.0]

# Multiply
exp_mul = exp1 * dy.transpose(exp1) #It's Matrix multiplication (like e1.dot(exp2) in numpy)

math = math1 * math2

print(f"Add_Exp = {exp_add}")
print(f"Minus_Exp = {exp_sub}")
print(f"Negtive_Exp = {exp_neg}")
print(f"Multiply_Exp = {exp_mul}")
print(f"Multuply_Math = {math}")

Matrix Manipulation:-

# Reshape
new_dimension = (2, 2)
exp_rs = dy.reshape(e1, new_dimension) # Col major
print('reshape a vector:\n', exp_rs.value())

# Transpose
exp_trsp = dy.transpose(exp1)
print('e1 dimension:', exp1.dim())
print('e1 transpose dimension', exp_trsp.dim())

# inverse
# Not implemented on GPU yet.
exp_inv = dy.inverse(dy.inputTensor([[1, 3], [3, 1]]))
print('Inverse a matrix:\n', exp_inv.npvalue())

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

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.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.