Theano is a Python library that allows us to evaluate mathematical operations including multi-dimensional arrays efficiently. It is mostly used in building Deep Learning Projects. Theano works way faster on the Graphics Processing Unit (GPU) rather than on the CPU.
This article will help you to understand What is Theano in Python and how to install and work with Theono in Python.
Theano in Python
Theano attains high speeds that give tough competition to C implementations for problems involving large amounts of data. It can take advantage of GPUs, making it perform better than C on a CPU by considerable orders of magnitude under certain circumstances.
Theano also knows how to take structures and convert them into very efficient code that uses Numpy and some native libraries. It is mainly designed to handle the types of computation required for large neural network algorithms used in Deep Learning. That is why, Theanos is a trendy library in the field of Deep Learning.
How to install Theano?
Before installing Theano in your system, You need to make sure that Python and Numpy is already installed in your system. To install Python , You can refer to this article.
How to download and install Python Latest Version on Windows
To install NumPy in your system , You can refer to these below mentioned articles.
To install Theano, You need to run this command in your terminal.
pip install theano
Several symbols we will need to use are in the tensor subpackage of Theano. We often import such packages with a handy name, let's say, T.
from theano import *
import theano.tensor as T
Why Theano Python Library ?
Theano is a sort of hybrid between numpy and sympy, an attempt is made to combine the two into one powerful library. Some advantages of theano are as follows:
- Stability Optimization: Theano can find out some unstable expressions and can use more stable means to evaluate them.
- Execution Speed Optimization: As mentioned earlier, theano can make use of recent GPUs and execute parts of expressions in your CPU or GPU, making it much faster than Python.
- Symbolic Differentiation: Theano is smart enough to automatically create symbolic graphs for computing gradients.
Basics of Theano
Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. Some Theano implementations are as follows.
Subtracting two scalars
Python program showing subtraction of two scalars
Python
import theano
from theano import tensor
# Declaring variables
a = tensor.dscalar()
b = tensor.dscalar()
# Subtracting
res = a - b
# Converting it to a callable object
# so that it takes matrix as parameters
func = theano.function([a, b], res)
# Calling function
assert 20.0 == func(30.5, 10.5)
It will not provide any output as the assertion of two numbers matches the number given, hence it results into a true value.
Adding two scalars
Python
# Python program showing
# addition of two scalars
# Addition of two scalars
import numpy
import theano.tensor as T
from theano import function
# Declaring two variables
x = T.dscalar('x')
y = T.dscalar('y')
# Summing up the two numbers
z = x + y
# Converting it to a callable object
# so that it takes matrix as parameters
f = function([x, y], z)
f(5, 7)
Output:
array(12.0)
Adding two matrices
Python program showing addition of two matrices.
Python
import numpy
import theano.tensor as T
from theano import function
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)
f([[30, 50], [2, 3]], [[60, 70], [3, 4]])
Output:
array([[ 90., 120.],
[ 5., 7.]])
Logistic function using theano :
Let’s try to compute the logistic curve, which is given by:

Logistic Function
Python program to illustrate logistic sigmoid function using theano
Python
# Load theano library
import theano
from theano import tensor
# Declaring variable
a = tensor.dmatrix('a')
# Sigmoid function
sig = 1 / (1 + tensor.exp(-a))
# Now it takes matrix as parameters
log = theano.function([a], sig)
# Calling function
print(log([[0, 1], [-1, -2]]))
Output :
[[0.5 0.73105858
0.26894142 0.11920292]]
Theano is a foundation library mainly used for deep learning research and development and directly to create deep learning models or by convenient libraries such as Keras. It supports both convolutional networks and recurrent networks, as well as combinations of the two.
Conclusion
In the dynamic landscape of deep learning, Theano shines as a versatile and efficient library. As we continue to delve into the intricacies of AI, Theano remains a steadfast ally, propelling us into a future where innovation knows no bounds.
Similar Reads
Python vs Cpython Python is a high-level, interpreted programming language favored for its readability and versatility. It's widely used in web development, data science, machine learning, scripting, and more. However, Cpython is the default and most widely used implementation of the Python language. It's written in
4 min read
Python While Loop Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.In this example, the condition for while will be True as long as the counter variable (count) i
5 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Version History Python, one of the most popular programming languages today, has a rich history of development and evolution. From its inception in the late 1980s to its current status as a versatile and powerful language, Python's version history reflects the language's adaptability and the community's dedication
5 min read
What is Python Interpreter Well if you are new to Python you have come across the word interpreters but before knowing about Python interpreters, we need to what is an interpreter, what it will do, the advantages and disadvantages of the interpreter, and so on. Well in this handy article, we will cover the Importance of Inter
4 min read