Theano
(1) Theano
Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently.
Theano features:
- tight integration with NumPy – Use numpy.ndarray in Theano-compiled functions.
- transparent use of a GPU – Perform data-intensive calculations up to 140x faster than with CPU.(float32 only)
- efficient symbolic differentiation – Theano does your derivatives for function with one or many inputs.
- speed and stability optimizations – Get the right answer for log(1+x) even when x is really tiny.
- dynamic C code generation – Evaluate expressions faster.
- extensive unit-testing and self-verification – Detect and diagnose many types of mistake.
Theano has been powering large-scale computationally intensive scientific investigations since 2007. But it is also approachable enough to be used in the classroom
(2) Install
Basic user install instructions
pip install Theano OR pip install Theano --user
update
sudo pip install --upgrade --no-deps theano
The following command will update Theano and Numpy/Scipy (warning bellow):
sudo pip install --upgrade theano
Numpy
[1] Matrix conventions
(1) Array Creation
array()
import numpy as np
data1 = [6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
arr1
array([ 6. , 7.5, 8. , 0. , 1. ])
arrange()
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(2, 10, dtype=np.float)
array([ 2., 3., 4., 5., 6., 7., 8., 9.])
np.arange(2, 3, 0.1)
array([ 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])
indices()
np.indices((3,3))
array([[[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]])
(2) Indexing
single element indexing
x = np.arange(10)
x[2]
2
x[-2]
8
x.shape = (2,5)
x[1,3]
8
x[1,-1]
9
x[0]
array([0, 1, 2, 3, 4])
Other indexing options
x = np.arange(10)
x[2:5]
array([2, 3, 4])
x[:-7]
array([0, 1, 2])
x[1:7:2]
array([1, 3, 5])
y = np.arange(35).reshape(5,7)
y[1:5:2,::3]
array([[ 7, 10, 13], [21, 24, 27]])
(3) Index Arrays
x = np.arange(10,1,-1)
x[np.array([3, 3, 1, 8])]
array([7, 7, 9, 2])
It is an error to have index values out of bounds:
x[np.array([3, 3, 20, 8])]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-17-3ad16747a54d> in <module>() ----> 1 x[np.array([3, 3, 20, 8])] IndexError: index 20 is out of bounds for size 9
(4) Indexing Multi-dimensional arrays
y[np.array([0,2,4]), np.array([0,1,2])]
array([ 0, 15, 30])
If the index arrays do not have the same shape, there is an attempt to
broadcast them to the same shape. If they cannot be broadcast to
the same shape, an exception is raised:
y[np.array([0,2,4]), np.array([0,1])]
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-19-1a836a248e48> in <module>() ----> 1 y[np.array([0,2,4]), np.array([0,1])] ValueError: shape mismatch: objects cannot be broadcast to a single shape
y[np.array([0,2,4]), 1]
array([ 1, 15, 29])
y[np.array([0,2,4])]
array([[ 0, 1, 2, 3, 4, 5, 6], [14, 15, 16, 17, 18, 19, 20], [28, 29, 30, 31, 32, 33, 34]])
[2] BroadCasting
The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is
“broadcast” across the larger array so that they have compatible shapes
@Rules
When operating on two arrays, NumPy compares their shapes element-wise.
It starts with the trailing dimensions, and works its way forward.
Two dimensions are compatible when
they are equal, or
one of them is 1
If these conditions are not met, a ValueError: frames are not aligned exception
is thrown, indicating that the arrays have incompatible shapes.
The size of the resulting array is the maximum size along each dimension of the
input arrays.
a = np.array([1.0, 2.0, 3.0])
b = np.array([2.0, 2.0, 2.0])
a * b
array([ 2., 4., 6.])
Two Dimensions are not compatible ( x is (4,) , y is (5,) )
x = np.arange(4)
xx = x.reshape(4,1)
y = np.ones(5)
z = np.ones((3,4))
x + y
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-24-f7f60ee3cd0e> in <module>() 4 z = np.ones((3,4)) 5 ----> 6 x + y ValueError: operands could not be broadcast together with shapes (4,) (5,)
xx + y
array([[ 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3.], [ 4., 4., 4., 4., 4.]])
x + z
array([[ 1., 2., 3., 4.], [ 1., 2., 3., 4.], [ 1., 2., 3., 4.]])
(x + z).shape
(3, 4)