Theano is an open-source Python library for numerical computation and symbolic mathematics. It was widely used for developing deep learning models by optimizing mathematical expressions and efficiently executing them on CPUs and GPUs.
- Symbolic Computation: Builds symbolic mathematical expressions that can be optimized before execution.
- Automatic Differentiation: Automatically computes gradients, making it suitable for training machine learning models.
- CPU and GPU Execution: Supports running computations on both CPUs and GPUs for improved performance.
- Expression Optimization: Optimizes mathematical expressions to improve execution speed and numerical stability.
Note: Theano is no longer actively maintained. For new machine learning and deep learning projects, libraries such as TensorFlow, PyTorch, or JAX are recommended.
Installation
Before installing Theano, ensure that Python and NumPy are installed on your system. Then install Theano using pip:
pip install theano
Basic Operations
Theano represents mathematical expressions as symbolic graphs instead of executing them immediately. These expressions are then compiled into callable Python functions, allowing efficient execution on CPUs or GPUs.
Example 1: The example below creates two symbolic scalar variables, subtracts them, and compiles the expression into a callable function.
import theano
import theano.tensor as T
a = T.dscalar("a")
b = T.dscalar("b")
res = a - b
sub = theano.function([a, b], res)
print(sub(30.5, 10.5))
Output
20.0
Explanation:
- T.dscalar() creates symbolic double-precision scalar variables.
- a - b defines the subtraction expression.
- theano.function() compiles the symbolic expression into a callable function.
- Calling subtract(30.5, 10.5) returns the computed result.
Example 2: The example below performs addition on two symbolic scalar variables.
import theano
import theano.tensor as T
x = T.dscalar("x")
y = T.dscalar("y")
z = x + y
add = theano.function([x, y], z)
print(add(5, 7))
Output
12.0
Example 3: Theano also supports operations on multidimensional arrays. The example below adds two matrices element by element.
import theano
import theano.tensor as T
x = T.dmatrix("x")
y = T.dmatrix("y")
z = x + y
res = theano.function([x, y], z)
print(res( [[30, 50], [2, 3]],
[[60, 70], [3, 4]] ))
Output
[[ 90. 120.]
[ 5. 7.]]
Explanation:
- T.dmatrix() creates symbolic matrices.
- The + operator performs element-wise matrix addition.
- The compiled function adds the corresponding elements of both matrices.
Example 4: The sigmoid (logistic) function is widely used in machine learning and neural networks. The example below computes the sigmoid value for every element in a matrix.
import theano
import theano.tensor as T
x = T.dmatrix("x")
sigmoid = 1 / (1 + T.exp(-x))
predict = theano.function([x], sigmoid)
print(predict([[0, 1], [-1, -2]]))
Output
[[0.5 0.73105858]
[0.26894142 0.11920292]]
Explanation:
- T.exp() computes the exponential value.
- The sigmoid formula is defined symbolically.
Automatic Differentiation
One of Theano's most powerful features is automatic differentiation. It can compute derivatives of mathematical expressions automatically, which is especially useful when training machine learning and deep learning models.
import theano
import theano.tensor as T
x = T.dscalar("x")
y = x ** 2 + 3 * x
gradient = T.grad(y, x)
f = theano.function([x], gradient)
print(f(5))
Output
13.0
Explanation:
- Defines the expression x2 +3x.
- T.grad() computes its derivative.
- The derivative is evaluated for x = 5.