Theano in Python

In this tutorial, you are going to learn about the Theano library available in Python.

One of the most important libraries in deep learning for Python is Theano. Theano consists of the capability to run the code in CPU and GPU. Theano contains tensor operations.

First of all, we have to install Theano. Command to install Theano: conda install theano.

Theano does not replace Numpy but it works in concert to it.


Some of the few tensor objects in Theano. They are:

  1. theano.tensor.scalar: 0-dimensional array.
  2. theano.tensor.vector: 1-dimensional array.
  3. theano.tensor.matrix:2-dimensional array.
  4. theano.tensor.tensor3:3-dimensional array.
import theano.tensor as H

Output: Tensor objects Output

tensor example


First of all, import theano.tensor as H.

Call the function H.scalar() it shows the scalar format output which is a 0-dimensional array.

Call the function H.vector() it shows the vector format output which is a one-dimensional array.

Next made an addition using matrix format. Creating x and y variables is a two-dimensional array.

Assigning z =x+y as additional variables.

By using z.eval() function we can assign the x and y variable numbers to add it shows the addition of x and y.

Call the function H.tensor3() it shows the three-dimensional array.

Operations of tensors:

Theano provides a lot of operators to work with tensors.

Dimension Manipulation Operators:

Examples of above Operator functions are reshape(), fill(), flatten() etc.,

import theano.tensor as T

Output: Dimension Manipulation Operator Program and Output

Dimensional manipulation operator output

Program Explanation:

First import theano.tensor from theano in Python as T.

Create x variable with T.arange() function which is used for mesh grid arrays and range.

Create y variable with T.reshape() function which is used to reshape the dimension of tensors.

By eval()  it shows the series of numbers from 0 to 9.

By combining of arange() reshape() and eval() it shows 2X5 matrix with type of matrix.

Elementwise Operators:

Examples of above Operators functions are add(), mil(), sub(), exp() etc.

The second type of multidimensional arrays is Elementwise Operators.

import theano.tensor as T

Output: Elementwise Operators program and Output

Elementwise Operator

Program Explanation:

First import theano.tensor as T from theano in Python as T.

Create a condition as cond which takes true or false.

Assign a and b variables with vectors(a,b)

Create another variable c with T.switch() function which accepts three inputs.

By evaluating the output using condition if the condition is true it takes the x value otherwise the condition is false it takes the y value.

These are some types of Operators such as Dimension Manipulation Operators, Elementwise Operators, Reduction Operators and Linear Algebra, etc.,


Leave a Reply

Your email address will not be published. Required fields are marked *