What is looping in theano?

This recipe explains what is looping in theano.

Recipe Objective - What is looping in theano?

The scan functions provide the basic functionality needed to do loops in Theano.

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/neural-network-projects

Example -
Let's say, given k you want to get x^n. More precisely, if x is a tensor you want to compute x^n elementwise:

# Importing libraries
import theano
from theano import tensor

# Creating a scalar 'n' and vector 'x'
n = tensor.iscalar("n")
x = tensor.vector("x")

output, updates = theano.scan(fn=lambda previous_result, x: previous_result * x, outputs_info=tensor.ones_like(x), non_sequences=x, n_steps=n)

# We only care about x**n, but scan has provided us with x**1 to x**n.
# Let's take only the last result
output = output[-1]

# Creating function that returns x**n
fun_power = theano.function(inputs=[x,n], outputs=output, updates=updates)

# Calling function
print(fun_power(range(5),2))

# Calling function
print(fun_power(range(10),3))

Output -
[ 0.  1.  4.  9. 16.]
[  0.   1.   8.  27.  64. 125. 216. 343. 512. 729.]

In this way, looping can be done in theano.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

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.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

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

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

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.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.