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

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

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

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

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.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

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

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

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.