Teano in Python

How to install Theano:

 pip install theano 

Some of the symbols we need are in the tensor subpackage Theano. We often import such packages with a convenient name, say T.

 from theano import * import theano.tensor as T 

Why Theano Python Library:
Theano — a kind of hybrid between numy and sympy, an attempt is made to combine them into one powerful library. Some of the advantages of theano are as follows:

  • Stability optimization: Theano can find some unstable expressions and can use more stable means to evaluate them.
  • Execution speed optimization: As mentioned earlier, theano can use the latest 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 generate symbolic graphics for calculating gradients

Teano Basics:
Theano — is a Python library that allows you to efficiently define, optimize, and evaluate mathematical expressions involving multidimensional arrays. Some Theano implementations are as follows.

Subtract two scalars:

# Show Python program
# subtract two scalars

 

import theano

from theano import tensor

  
# Variable declarations

a = tensor.dscalar ()

b = tensor.dscalar ()

 
You read

res = a - b

# Convert it to a callable
# so it takes a matrix as parameters

func = theano.function ([a, b], res)

 
# Function call

assert 20.0 = = func ( 30.5 , 10.5 )

It will not give any output since the statement of two numbers is the same as the given number, hence , it converts to the true value.

Adding two scalars:

# Show Python program
# adding two scalars

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

 
# Convert it to a callable
# so it takes a matrix as parameters

f = function ([x, y], z)

f ( 5 , 7 )

Exit :

 arr ay (12.0) 

Add two matrices:

# Show Python program
# addition of two matrices

 
# Adding two matrices

import numpy

import theano.tensor as T

from theano import function

x = T.dmatrix ( ' x' )

y = T.dmatrix ( 'y' < code class = "plain">)

z = x + y

f = function ([x, y], z)

 

f ([[ 30 , 50 ], [ 2 , 3 ]], [[ 60 , 70 ], [ 3 , 4 ]])

Exit:

 array ([[90., 120.], [5., 7.]]) 

Logistic f function using theano:
Let's try to calculate the logistic curve, which is defined as:

Logistic function:

# Python program to illustrate logistics
# sigmoid function using theano
# Download theano library

 

import theano

from theano import tensor

 
# Variable declaration

a = tensor.dmatrix ( 'a' )

  
# Sigmoid function

sig = 1 / ( 1 + tensor.exp ( - a))

 
# It now accepts a matrix as parameters

log = theano.function ([a], sig)

  
# Call functions

print (log ([[ 0 , 1 ], [ - 1 , - 2 ]]))

Exit:

 [[0.5 0.73105858 0.26894142 0.11920292]] 

Theano — it is a core library mainly used for deep learning research and development and directly for creating deep learning models or in handy libraries like Keras. It supports both convolutional networks and recurrent networks, as well as combinations thereof.





Get Solution for free from DataCamp guru