mirror of
https://github.com/apachecn/ailearning.git
synced 2026-02-10 05:45:40 +08:00
360 lines
6.5 KiB
Markdown
360 lines
6.5 KiB
Markdown
# Theano tensor 模块:基础
|
||
|
||
张量是向量在数学上的一种推广,具体内容可以参考维基百科: [https://en.wikipedia.org/wiki/Tensor](https://en.wikipedia.org/wiki/Tensor)
|
||
|
||
在 Theano 中有一个专门处理张量变量的模块:`theano.tensor` (以下简称 `T`)。
|
||
|
||
In [1]:
|
||
|
||
```py
|
||
import theano
|
||
import theano.tensor as T
|
||
|
||
```
|
||
|
||
```py
|
||
Using gpu device 1: Tesla C2075 (CNMeM is disabled)
|
||
|
||
```
|
||
|
||
## 构造符号变量
|
||
|
||
可以用 `tensor` 模块创造符号变量:
|
||
|
||
In [2]:
|
||
|
||
```py
|
||
x = T.fmatrix()
|
||
|
||
print type(x)
|
||
print type(T.fmatrix)
|
||
|
||
```
|
||
|
||
```py
|
||
<class 'theano.tensor.var.TensorVariable'>
|
||
<class 'theano.tensor.type.TensorType'>
|
||
|
||
```
|
||
|
||
从上面可以看到,`T.fmatrix()` 创造出的是一个 `TensorVariable` 类,而 `T.fmatrix` 本身是一个 `TensorType` 类。
|
||
|
||
除了使用 `fmatrix`,我们还可以通过指定 `matrix` 的 `dtype` 参数来定义,例如下面的三种方式都是产生一个 `int32` 型的标量:
|
||
|
||
In [3]:
|
||
|
||
```py
|
||
x = T.scalar('myvar', dtype='int32')
|
||
x = T.iscalar('myvar')
|
||
x = T.TensorType(dtype='int32', broadcastable=())('myvar')
|
||
|
||
```
|
||
|
||
常用的构造函数有:
|
||
|
||
* `T.scalar(name=None, dtype=config.floatX)`
|
||
* `T.vector(name=None, dtype=config.floatX)`
|
||
* `T.row(name=None, dtype=config.floatX)`
|
||
* `T.col(name=None, dtype=config.floatX)`
|
||
* `T.matrix(name=None, dtype=config.floatX)`
|
||
* `T.tensor3(name=None, dtype=config.floatX)`
|
||
* `T.tensor4(name=None, dtype=config.floatX)`
|
||
|
||
还可以使用一个构造多个变量:
|
||
|
||
* `T.scalars`
|
||
* `T.vectors`
|
||
* `T.rows`
|
||
* `T.cols`
|
||
* `T.matrices`
|
||
|
||
除此之外,我们还可以用 `TensorType` 类自定义的符号变量:
|
||
|
||
`T.TensorType(dtype, broadcastable, name=None)`
|
||
|
||
* `dtype: str`:对应于 `numpy` 中的类型
|
||
* `broadcastable: tuple, list, or array of boolean values`:如果是 `True` 表示该维的维度只能为 1;长度表示符号变量的维度。
|
||
|
||
| pattern | interpretation |
|
||
| --- | --- |
|
||
| [] | scalar |
|
||
| [True] | 1D scalar (vector of length 1) |
|
||
| [True, True] | 2D scalar (1x1 matrix) |
|
||
| [False] | vector |
|
||
| [False, False] | matrix |
|
||
| [False] * n | nD tensor |
|
||
| [True, False] | row (1xN matrix) |
|
||
| [False, True] | column (Mx1 matrix) |
|
||
| [False, True, False] | A Mx1xP tensor (a) |
|
||
| [True, False, False] | A 1xNxP tensor (b) |
|
||
| [False, False, False] | A MxNxP tensor (pattern of a + b) |
|
||
|
||
产生一个五维的变量类型:
|
||
|
||
In [4]:
|
||
|
||
```py
|
||
dtensor5 = T.TensorType('float64', (False,)*5)
|
||
|
||
x = dtensor5()
|
||
|
||
```
|
||
|
||
## 变量方法
|
||
|
||
### .dim
|
||
|
||
维度:
|
||
|
||
In [5]:
|
||
|
||
```py
|
||
print x.ndim
|
||
|
||
```
|
||
|
||
```py
|
||
5
|
||
|
||
```
|
||
|
||
### .type
|
||
|
||
类型:
|
||
|
||
In [6]:
|
||
|
||
```py
|
||
print x.type
|
||
|
||
```
|
||
|
||
```py
|
||
TensorType(float64, 5D)
|
||
|
||
```
|
||
|
||
### .dtype
|
||
|
||
包含的变量类型:
|
||
|
||
In [7]:
|
||
|
||
```py
|
||
print x.dtype
|
||
|
||
```
|
||
|
||
```py
|
||
float64
|
||
|
||
```
|
||
|
||
### .reshape
|
||
|
||
传入一个变量对 x 进行 `reshape`,通常需要指定 `shape` 的 `ndim`:
|
||
|
||
In [8]:
|
||
|
||
```py
|
||
shape = T.ivector("shape")
|
||
|
||
y = x.reshape(shape, ndim=3)
|
||
|
||
```
|
||
|
||
`y` 是 `x` 的一个 `view`:
|
||
|
||
In [9]:
|
||
|
||
```py
|
||
print x.ndim, y.ndim
|
||
|
||
```
|
||
|
||
```py
|
||
5 3
|
||
|
||
```
|
||
|
||
### .dimshuffle
|
||
|
||
`dimshuffle` 改变维度的顺序,返回原始变量的一个 `view`:
|
||
|
||
输入是一个包含 `0,1,...,ndim-1` 和任意数目的 `'x'` 的组合:
|
||
|
||
例如:
|
||
|
||
* `('x')`:将标量变成 1 维数组
|
||
* `(0, 1)`:与原始的 2 维数组相同
|
||
* `(1, 0)`:交换 2 维数组的两个维度,形状从 `N × M` 变 `M × N`
|
||
* `('x', 0)`:形状从 `N` 变成 `1 × N`
|
||
* `(0, 'x')`:形状从 `N` 变成 `N × 1`
|
||
* `(2, 0, 1)`: 形状从 `A × B × C` 变成 `C × A × B`
|
||
* `(0, 'x', 1)`: 形状从 `A × B` 变成 `A × 1 × B`
|
||
* `(1, 'x', 0)`: 形状从 `A × B` 变成 `B × 1 × A`
|
||
* `(1,)`: 将第 0 维除去,除去的维度的大小必须为 1。形状从 `1 × A` 变成 `A`
|
||
|
||
In [10]:
|
||
|
||
```py
|
||
z = y.dimshuffle(("x", 1, 2, 0))
|
||
|
||
print z
|
||
print z.ndim
|
||
|
||
```
|
||
|
||
```py
|
||
DimShuffle{x,1,2,0}.0
|
||
4
|
||
|
||
```
|
||
|
||
### .flatten
|
||
|
||
`flatten(ndim=1)` 返回原始变量的一个 `view`,将变量降为 `ndim` 维:
|
||
|
||
In [11]:
|
||
|
||
```py
|
||
z = x.flatten(ndim=2)
|
||
|
||
print z.ndim
|
||
|
||
```
|
||
|
||
```py
|
||
2
|
||
|
||
```
|
||
|
||
### .ravel
|
||
|
||
与 `flatten` 一样。
|
||
|
||
### .T
|
||
|
||
转置,注意,一维数组或者变量的转置是其本身,要想将行列向量互相转换,需要使用 `reshape` 或者 `dimshuffle`。
|
||
|
||
### 其他方法
|
||
|
||
In [12]:
|
||
|
||
```py
|
||
print filter(lambda t: t.isalpha(), dir(x))
|
||
|
||
```
|
||
|
||
```py
|
||
['T', 'all', 'any', 'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctanh', 'argmax', 'argmin', 'argsort', 'astype', 'broadcastable', 'ceil', 'choose', 'clip', 'clone', 'compress', 'conj', 'conjugate', 'copy', 'cos', 'cosh', 'cumprod', 'cumsum', 'diagonal', 'dimshuffle', 'dot', 'dtype', 'eval', 'exp', 'fill', 'flatten', 'floor', 'imag', 'index', 'log', 'max', 'mean', 'min', 'name', 'ndim', 'nonzero', 'norm', 'owner', 'prod', 'ptp', 'ravel', 'real', 'repeat', 'reshape', 'round', 'shape', 'sin', 'sinh', 'size', 'sort', 'sqrt', 'squeeze', 'std', 'sum', 'swapaxes', 'tag', 'take', 'tan', 'tanh', 'trace', 'transpose', 'trunc', 'type', 'var']
|
||
|
||
```
|
||
|
||
## 模块函数
|
||
|
||
为了与 `numpy` 兼容,`tensor`
|
||
|
||
### T.shape
|
||
|
||
`shape(x)` 返回一个存储变量 `x` 形状的变量:
|
||
|
||
In [13]:
|
||
|
||
```py
|
||
print T.shape(x)
|
||
|
||
```
|
||
|
||
```py
|
||
Shape.0
|
||
|
||
```
|
||
|
||
### T.shape_padleft, T.shape_padright
|
||
|
||
在最左边/右边加上 n 个大小为 1 的 1 个维度:
|
||
|
||
In [14]:
|
||
|
||
```py
|
||
x = T.tensor3()
|
||
|
||
print T.shape_padleft(x)
|
||
print T.shape_padright(x)
|
||
|
||
```
|
||
|
||
```py
|
||
DimShuffle{x,0,1,2}.0
|
||
DimShuffle{0,1,2,x}.0
|
||
|
||
```
|
||
|
||
### T.shape_padaxis
|
||
|
||
在指定位置插入大小为 1 的 1 个维度:
|
||
|
||
In [15]:
|
||
|
||
```py
|
||
print T.shape_padaxis(x, 1)
|
||
print T.shape_padaxis(x, 0)
|
||
print T.shape_padaxis(x, -1)
|
||
|
||
```
|
||
|
||
```py
|
||
DimShuffle{0,x,1,2}.0
|
||
DimShuffle{x,0,1,2}.0
|
||
DimShuffle{0,1,2,x}.0
|
||
|
||
```
|
||
|
||
插入这些大小为 `1` 的维度,主要目的是 `broadcast` 化。
|
||
|
||
### T.unbroadcast
|
||
|
||
可以使用 `unbroadcast(x, *axes)` 使得 `x` 的某些维度不可 `broadcast`。
|
||
|
||
### T.tile
|
||
|
||
`tile(x, reps)` 按照规则重复 `x`
|
||
|
||
## 产生张量
|
||
|
||
### T.zeros_like(x), T.ones_like(x)
|
||
|
||
产生一个与 x 形状相同的全 0 或全 1 变量
|
||
|
||
### T.fill(a, b)
|
||
|
||
使用 `b` 的值去填充 `a`,`b` 是一个数值或者 `theano scalar`。
|
||
|
||
### T.alloc(value, *shape)
|
||
|
||
返回指定形状的变量,并初始化为 `value`
|
||
|
||
### T.eye(n, m=None, k=0, dtype=theano.config.floatX)
|
||
|
||
单位矩阵
|
||
|
||
### T.basic.choose(a, choices)
|
||
|
||
`a` 是一个 `index` 数组变量,对应于 `choices` 中的位置。
|
||
|
||
## 降维
|
||
|
||
### T.max(x), T.argmax(x), T.max_and_argmax(x)
|
||
|
||
最大值,最大值位置,最大值和最大值位置。
|
||
|
||
### T.min(x), T.argmin(x)
|
||
|
||
最小值,最小值位置。
|
||
|
||
### T.sum(x), T.prod(x), T.mean(x), T.var(x), T.std(x)
|
||
|
||
和,积,均值,方差,标准差
|
||
|
||
### T.all(x), T.any(x) |