Now, that you have understood the basics of Theano, let us begin with the different data types available to you for creating your expressions. The following table gives you a partial list of data types defined in Theano.
Data type | Theano type |
---|---|
Byte | bscalar, bvector, bmatrix, brow, bcol, btensor3, btensor4, btensor5, btensor6, btensor7 |
16-bit integers | wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4, wtensor5, wtensor6, wtensor7 |
32-bit integers | iscalar, ivector, imatrix, irow, icol, itensor3, itensor4, itensor5, itensor6, itensor7 |
64-bit integers | lscalar, lvector, lmatrix, lrow, lcol, ltensor3, ltensor4, ltensor5, ltensor6, ltensor7 |
float | fscalar, fvector, fmatrix, frow, fcol, ftensor3, ftensor4, ftensor5, ftensor6, ftensor7 |
double | dscalar, dvector, dmatrix, drow, dcol, dtensor3, dtensor4, dtensor5, dtensor6, dtensor7 |
complex | cscalar, cvector, cmatrix, crow, ccol, ctensor3, ctensor4, ctensor5, ctensor6, ctensor7 |
The above list is not exhaustive and the reader is referred to the tensor creation document for a complete list.
I will now give you a few examples of how to create variables of various kinds of data in Theano.
Scalar
To construct a scalar variable you would use the syntax −
Syntax
x = theano.tensor.scalar ('x') x = 5.0 print (x)
Output
5.0
One-dimensional Array
To create a one dimensional array, use the following declaration −
Example
f = theano.tensor.vector f = (2.0, 5.0, 3.0) print (f)f = theano.tensor.vector f = (2.0, 5.0, 3.0) print (f) print (f[0]) print (f[2])
Output
(2.0, 5.0, 3.0) 2.0 3.0
If you do f[3] it would generate an index out of range error as shown here −
print f([3])
Output
IndexError Traceback (most recent call last) <ipython-input-13-2a9c2a643c3a> in <module> 4 print (f[0]) 5 print (f[2]) ----> 6 print (f[3]) IndexError: tuple index out of range
Two-dimensional Array
To declare a two-dimensional array you would use the following code snippet −
Example
m = theano.tensor.matrix m = ([2,3], [4,5], [2,4]) print (m[0]) print (m[1][0])
Output
[2, 3] 4
5-dimensional Array
To declare a 5-dimensional array, use the following syntax −
Example
m5 = theano.tensor.tensor5 m5 = ([0,1,2,3,4], [5,6,7,8,9], [10,11,12,13,14]) print (m5[1]) print (m5[2][3])
Output
[5, 6, 7, 8, 9] 13
You may declare a 3-dimensional array by using the data type tensor3 in place of tensor5, a 4-dimensional array using the data type tensor4, and so on up to tensor7.
Plural Constructors
Sometimes, you may want to create variables of the same type in a single declaration. You can do so by using the following syntax −
Syntax
from theano.tensor import * x, y, z = dmatrices('x', 'y', 'z') x = ([1,2],[3,4],[5,6]) y = ([7,8],[9,10],[11,12]) z = ([13,14],[15,16],[17,18]) print (x[2]) print (y[1]) print (z[0])
Output
[5, 6] [9, 10] [13, 14]
Leave a Reply