# 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.

Tensors:

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

- theano.tensor.scalar: 0-dimensional array.
- theano.tensor.vector: 1-dimensional array.
- theano.tensor.matrix:2-dimensional array.
- theano.tensor.tensor3:3-dimensional array.

import theano.tensor as H H.scalar() H.vector() x=H.matrix('x') y=H.matrix('y') z=x+y z.eval({x:[[1,2],[2,3]],y:[[3,4],[4,5]]}) H.tensor3()

Output: Tensor objects Output

Explanation:

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 x=T.arange(10) y=T.reshape(x,(2,5)) y.eval() T.arange(10).reshape((2,5))[::-1].T.eval()

Output: Dimension Manipulation Operator Program and 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 cond=T.vector('cond') a,b=T.vectors('a','b') c=T.switch(cond,a,b) c.eval({cond:[1,0],a:[5,5],b:[2,3]})

Output: Elementwise Operators program and Output

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