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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

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 Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

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

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.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

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