Theano 简明教程

Theano - Data Types

现在,你已经了解了 Theano 的基础知识,让我们开始了解可用于创建表达式的不同数据类型。下表为你提供了 Theano 中定义的数据类型的部分列表。

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.

接下来,我将提供一些在 Theano 中创建不同类型数据变量的示例。

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

如果你执行 f[3] ,它会生成一个超出范围错误,如下所示 −

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

你可以使用 tensor5 数据类型代替 tensor3 声明一个三维数组,使用 tensor4 数据类型声明一个四维数组,依此类推,最多到 tensor7

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]