theano学习初步(二) 基础Tensor函数

本文来自于官方文档

Creation(声明)

Theano有多种方式进行声明变量。变量也可以进行命名,从而便于debug,而且每一种声明方式都能够接受name的参数。
下面这三种声明方式,声明的是0维的整型变量,它的名字是myvar

>>> x = scalar('myvar', dtype='int32')
>>> x = iscalar('myvar')
>>> x = TensorType(dtype='int32', broadcastable=())('myvar')

Constructors with optional dtype(用dtype参数进行声明)

theano.tensor.scalar(name=None, dtype=config.floatX)

返回一个0维的numpy.ndarray

theano.tensor.vector(name=None, dtype=config.floatX)

返回一个1维的numpy.ndarray

theano.tensor.row(name=None, dtype=config.floatX)

返回一个2维的numpy.ndarray,但是行数保证是1。

theano.tensor.col(name=None, dtype=config.floatX)

返回一个2维的numpy.ndarray,但是列数保证是1。

theano.tensor.matrix(name=None, dtype=config.floatX)

返回一个2维的numpy.ndarray

theano.tensor.tensor3(name=None, dtype=config.floatX)

返回一个3维的numpy.ndarray

theano.tensor.tensor4(name=None, dtype=config.floatX)

返回一个4维的numpy.ndarray

All Fully-Typed Constructors(用完整的类型进行声明)

上文说的dtype在Theano中都有定义。

Constructor dtype ndim shape broadcastable
bscalar int8 0 () ()
bvector int8 1 (?,) (False,)
brow int8 2 (1,?) (True, False)
bcol int8 2 (?,1) (False, True)
bmatrix int8 2 (?,?) (False, False)
btensor3 int8 3 (?,?,?) (False, False, False)
btensor4 int8 4 (?,?,?,?) (False, False, False, False)
btensor5 int8 5 (?,?,?,?,?) (False, False, False, False, False)
wscalar int16 0 () ()
wvector int16 1 (?,) (False,)
wrow int16 2 (1,?) (True, False)
wcol int16 2 (?,1) (False, True)
wmatrix int16 2 (?,?) (False, False)
wtensor3 int16 3 (?,?,?) (False, False, False)
wtensor4 int16 4 (?,?,?,?) (False, False, False, False)
wtensor5 int16 5 (?,?,?,?,?) (False, False, False, False, False)
iscalar int32 0 () ()
ivector int32 1 (?,) (False,)
irow int32 2 (1,?) (True, False)
icol int32 2 (?,1) (False, True)
imatrix int32 2 (?,?) (False, False)
itensor3 int32 3 (?,?,?) (False, False, False)
itensor4 int32 4 (?,?,?,?) (False, False, False, False)
itensor5 int32 5 (?,?,?,?,?) (False, False, False, False, False)
lscalar int64 0 () ()
lvector int64 1 (?,) (False,)
lrow int64 2 (1,?) (True, False)
lcol int64 2 (?,1) (False, True)
lmatrix int64 2 (?,?) (False, False)
ltensor3 int64 3 (?,?,?) (False, False, False)
ltensor4 int64 4 (?,?,?,?) (False, False, False, False)
ltensor5 int64 5 (?,?,?,?,?) (False, False, False, False, False)
dscalar float64 0 () ()
dvector float64 1 (?,) (False,)
drow float64 2 (1,?) (True, False)
dcol float64 2 (?,1) (False, True)
dmatrix float64 2 (?,?) (False, False)
dtensor3 float64 3 (?,?,?) (False, False, False)
dtensor4 float64 4 (?,?,?,?) (False, False, False, False)
dtensor5 float64 5 (?,?,?,?,?) (False, False, False, False, False)
fscalar float32 0 () ()
fvector float32 1 (?,) (False,)
frow float32 2 (1,?) (True, False)
fcol float32 2 (?,1) (False, True)
fmatrix float32 2 (?,?) (False, False)
ftensor3 float32 3 (?,?,?) (False, False, False)
ftensor4 float32 4 (?,?,?,?) (False, False, False, False)
ftensor5 float32 5 (?,?,?,?,?) (False, False, False, False, False)
cscalar complex64 0 () ()
cvector complex64 1 (?,) (False,)
crow complex64 2 (1,?) (True, False)
ccol complex64 2 (?,1) (False, True)
cmatrix complex64 2 (?,?) (False, False)
ctensor3 complex64 3 (?,?,?) (False, False, False)
ctensor4 complex64 4 (?,?,?,?) (False, False, False, False)
ctensor5 complex64 5 (?,?,?,?,?) (False, False, False, False, False)
zscalar complex128 0 () ()
zvector complex128 1 (?,) (False,)
zrow complex128 2 (1,?) (True, False)
zcol complex128 2 (?,1) (False, True)
zmatrix complex128 2 (?,?) (False, False)
ztensor3 complex128 3 (?,?,?) (False, False, False)
ztensor4 complex128 4 (?,?,?,?) (False, False, False, False)
ztensor5 complex128 5 (?,?,?,?,?) (False, False, False, False, False)

Plural Constructors(多重声明)

  • iscalars, lscalars, fscalars, dscalars
  • ivectors, lvectors, fvectors, dvectors
  • irows, lrows, frows, drows
  • icols, lcols, fcols, dcols
  • imatrices, lmatrices, fmatrices, dmatrices

用法参考如下

from theano.tensor import *

x, y, z = dmatrices(3) # creates three matrix Variables with no names
x, y, z = dmatrices('x', 'y', 'z') # creates three matrix Variables named 'x', 'y' and 'z'

Custom tensor types(一般的类型声明)

如果你想要创建一个非标准的类型,那么就只能创造一个你自己定义的TensorType。你需要将dtypebroadcasting pattern传入声明函数中。
下面的例子是,自创一个五维向量。

dtensor5 = TensorType('float64', (False,)*5)
x = dtensor5()
z = dtensor5('z')

你也可以重构一个已存在的类型

my_dmatrix = TensorType('float64', (False,)*2)
x = my_dmatrix()       # allocate a matrix variable
print my_dmatrix == dmatrix # output is 'True'

他们会很好地结合起来。

Converting from Python Objects(从Python对象中转移)

使用的是shared()函数。

x = shared(numpy.random.randn(3,4))

这个函数似乎有一些细节,虽然可以这么转化,但是缺少上面声明的一些功能。

Shaping and Shuffling

theano.tensor.shape(x)

返回一个lvector用于表示x的shape

theano.tensor.reshape(x, newshape, ndim=None)

Parameters:

  • x (某种TensorVariable (或者是可兼容的类型)) – 要进行reshape的变量
  • newshape (lvector (或者是可兼容的类型)) – x的新形状
  • ndim – 可选的- the length that newshape‘s value will have. If this is None, then reshape() will infer it from newshape.

Return type:

  • variable with x’s dtype, but ndim dimensions

你可能感兴趣的:(机器学习,&,深度学习,机器学习与数学模型)