为什么我们需要 Theano 重塑?
Why do we need Theano reshape?
我不明白为什么 Theano 中需要 tensor.reshape()
函数。文档中说:
Returns a view of this tensor that has been reshaped as in
numpy.reshape.
据我了解,theano.tensor.var.TensorVariable
是一些用于创建计算图的实体。而且它完全独立于形状。例如,当您创建函数时,您可以将矩阵 2x2 或矩阵 100x200 传递给那里。正如我所想的那样,重塑以某种方式限制了这种多样性。但事实并非如此。假设以下示例:
X = tensor.matrix('X')
X_resh = X.reshape((3, 3))
Y = X_resh ** 2
f = theano.function([X_resh], Y)
print(f(numpy.array([[1, 2], [3, 4]])))
据我所知,它应该给出一个错误,因为我传递的矩阵是 2x2 而不是 3x3,但它可以完美地计算元素方块。
那么theano张量变量的shape是什么,我们应该在哪里使用呢?
提供的代码中存在错误,尽管 Theano 未能指出这一点。
而不是
f = theano.function([X_resh], Y)
你真的应该使用
f = theano.function([X], Y)
使用原始代码,您实际上是在重塑 之后提供张量 ,因此重塑命令永远不会执行。这可以通过添加
看到
theano.printing.debugprint(f)
打印
Elemwise{sqr,no_inplace} [id A] '' 0
|<TensorType(float64, matrix)> [id B]
请注意,此编译执行图中没有重塑操作。
如果更改代码,使用 X
而不是 X_resh
作为输入,那么 Theano 会抛出一个错误,包括消息
ValueError: total size of new array must be unchanged Apply node that
caused the error: Reshape{2}(X, TensorConstant{(2L,) of 3})
这是预料之中的,因为不能将形状为 (2, 2)
(即 4 个元素)的张量重塑为形状为 (3, 3)
(即 9 个元素)的张量。
为了解决更广泛的问题,我们可以在目标形状中使用符号表达式,这些表达式可以是输入张量符号形状的函数。下面是一些示例:
import numpy
import theano
import theano.tensor
X = theano.tensor.matrix('X')
X_vector = X.reshape((X.shape[0] * X.shape[1],))
X_row = X.reshape((1, X.shape[0] * X.shape[1]))
X_column = X.reshape((X.shape[0] * X.shape[1], 1))
X_3d = X.reshape((-1, X.shape[0], X.shape[1]))
f = theano.function([X], [X_vector, X_row, X_column, X_3d])
for output in f(numpy.array([[1, 2], [3, 4]])):
print output.shape, output
我不明白为什么 Theano 中需要 tensor.reshape()
函数。文档中说:
Returns a view of this tensor that has been reshaped as in numpy.reshape.
据我了解,theano.tensor.var.TensorVariable
是一些用于创建计算图的实体。而且它完全独立于形状。例如,当您创建函数时,您可以将矩阵 2x2 或矩阵 100x200 传递给那里。正如我所想的那样,重塑以某种方式限制了这种多样性。但事实并非如此。假设以下示例:
X = tensor.matrix('X')
X_resh = X.reshape((3, 3))
Y = X_resh ** 2
f = theano.function([X_resh], Y)
print(f(numpy.array([[1, 2], [3, 4]])))
据我所知,它应该给出一个错误,因为我传递的矩阵是 2x2 而不是 3x3,但它可以完美地计算元素方块。
那么theano张量变量的shape是什么,我们应该在哪里使用呢?
提供的代码中存在错误,尽管 Theano 未能指出这一点。
而不是
f = theano.function([X_resh], Y)
你真的应该使用
f = theano.function([X], Y)
使用原始代码,您实际上是在重塑 之后提供张量 ,因此重塑命令永远不会执行。这可以通过添加
看到theano.printing.debugprint(f)
打印
Elemwise{sqr,no_inplace} [id A] '' 0
|<TensorType(float64, matrix)> [id B]
请注意,此编译执行图中没有重塑操作。
如果更改代码,使用 X
而不是 X_resh
作为输入,那么 Theano 会抛出一个错误,包括消息
ValueError: total size of new array must be unchanged Apply node that caused the error: Reshape{2}(X, TensorConstant{(2L,) of 3})
这是预料之中的,因为不能将形状为 (2, 2)
(即 4 个元素)的张量重塑为形状为 (3, 3)
(即 9 个元素)的张量。
为了解决更广泛的问题,我们可以在目标形状中使用符号表达式,这些表达式可以是输入张量符号形状的函数。下面是一些示例:
import numpy
import theano
import theano.tensor
X = theano.tensor.matrix('X')
X_vector = X.reshape((X.shape[0] * X.shape[1],))
X_row = X.reshape((1, X.shape[0] * X.shape[1]))
X_column = X.reshape((X.shape[0] * X.shape[1], 1))
X_3d = X.reshape((-1, X.shape[0], X.shape[1]))
f = theano.function([X], [X_vector, X_row, X_column, X_3d])
for output in f(numpy.array([[1, 2], [3, 4]])):
print output.shape, output