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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in 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

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

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.

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.