What is the shared variable in theano?

This recipe explains what is shared variable in theano.

Recipe Objective - What is a shared variable in theano?

The shared function creates what is known as shared variables. These variables are a mix of symbolic and non-symbolic variables with a value that can be shared across several functions. It's referred to as a shared variable because its value is shared across multiple functions. The.get value() and.set value() methods can be used to obtain and modify the value. In the method, there is an additional parameter called update. updates must be accompanied by a list of form pairs (shared-variable, new expression).

Complete Guide to Tensorflow for Deep Learning with Python for Free

For more related projects -

/projects/data-science-projects/tensorflow-projects
/projects/data-science-projects/keras-deep-learning-projects

Example -

# Import libraries
import theano
from theano import shared,tensor

# Creating shared variable as 'shared_variable' with value '0'
shared_variable = shared(0)

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

# Creating function which will take x as input and returns shared_variable and later updates shared_variable value with some expression mentioned in updates parameter.
fun = theano.function([x], shared_variable, updates=[(shared_variable, shared_variable + x)])

# Calling function
print('Calling function =>',fun(10))

# Printing shared_variable value
print('Value of shared variable =>',shared_variable.get_value())

# Calling function
print('Calling function =>',fun(100))

# Printing shared_variable value
print('Value of shared variable =>',shared_variable.get_value())

Output -
Calling function => 0
Value of shared variable => 10
Calling function => 10
Value of shared variable => 110

In this way, we can use a shared variable in theano.

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

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.

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.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

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.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.