What is the scan function in theano?

This recipe explains what is scan function in theano.

Recipe Objective - What is the scan function in theano?

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

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, we can make use of the scan function 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

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.

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.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

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.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

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.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

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.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python