Change language

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.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically