How to compute gradient in derivatives in theano?

This recipe helps you to compute gradient in derivatives in theano.

Recipe Objective - How to compute gradient in derivatives in theano?

Let's create a function to find out the derivative of some expression y with respect to its parameter x. To do this we will use the macro tt.grad. For instance, we can compute the gradient of 2x^3 with respect to x. Note that: d(2x^3)/dx = 6x^2.

For more related projects -

/projects/data-science-projects/tensorflow-projects
/projects/data-science-projects/neural-network-projects

Example -

# Import libraries
import theano
from theano import tensor
from theano import pp

# Creating a scalar
x = tensor.dscalar('x')

# Creating 'y' expression
y = (2 * x ** 3)

# Computing derivative
derivative = tensor.grad(y, x)

# Print out the gradient
pp(derivative)

# Convert the expression into a callable object that takes 'x' as input and computes a value for 'derivative'
fun = theano.function([x], derivative)

# Calling function
fun(3)

Output -
array(54.)

In this way, we can compute the gradient in derivatives in theano.

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

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

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.