Google的TensorFlow中的Theano Dimshuffle等效吗? [英] Theano Dimshuffle equivalent in Google's TensorFlow?
问题描述
我已经看到转置和重塑可以一起使用,但是我不知道如何使用.
I have seen that transpose and reshape together can help but I don't know how to use.
例如. dimshuffle(0,'x')
Eg. dimshuffle(0, 'x')
使用转置和整形等效于什么?或者,还有更好的方法? 谢谢.
What is its equivalent by using transpose and reshape? or is there a better way? Thank you.
推荐答案
在TensorFlow中实现Theano的dimshuffle
的三个相关操作:
There are three relevant ops for implementing Theano's dimshuffle
in TensorFlow:
-
tf.transpose()
用于替换张量的尺寸.如果dimshuffle
的参数中指定的模式是输入张量尺寸的排列(即没有'x'
或缺少尺寸),则可以使用tf.transpose()
来实现dimshuffle()
.
tf.transpose()
is used to permute the dimensions of a tensor. If the pattern specified in the arguments todimshuffle
is a permutation of the input tensor's dimensions (i.e. there is no'x'
or missing dimension) you can usetf.transpose()
to implementdimshuffle()
.
tf.expand_dims()
用于添加一个或更大的张量大小为1的尺寸.这可以处理将'x'
指定为dimshuffle()
模式的一部分的情况,但不会对现有尺寸重新排序.
tf.expand_dims()
is used to add one or more size-1 dimensions to a tensor. This handles the case where 'x'
is specified as part of the dimshuffle()
pattern, but does not reorder the existing dimensions.
tf.squeeze()
用于删除一个或更多张量的size-1尺寸.这可以处理以下情况:从dimshuffle()
模式中省略尺寸,但不对现有尺寸重新排序.
tf.squeeze()
is used to remove one or more size-1 dimensions from a tensor. This handles the case where a dimension is omitted from a dimshuffle()
pattern, but it does not reorder the existing dimensions.
假设输入是向量,则示例(dimshuffle(0, 'x')
)只能使用tf.expand_dims()
表示:
Assuming that the input is a vector, your example (dimshuffle(0, 'x')
) can be expressed using tf.expand_dims()
only:
input = tf.placeholder(tf.float32, [None]) # Defines an arbitrary-sized vector.
result = tf.expand_dims(input, 1)
print result.get_shape() # ==> TensorShape([Dimension(None), Dimension(1)])
举一个更复杂的例子,将dimshuffle(1, 'x', 0)
应用于矩阵将是:
Taking a more complicated example, dimshuffle(1, 'x', 0)
applied to a matrix would be:
input = tf.placeholder(tf.float32, [128, 32]) # Defines a matrix.
output = tf.expand_dims(tf.transpose(input, [1, 0]), 1)
print output.get_shape()
# ==> TensorShape([Dimension(32), Dimension(1), Dimension(128)])
这篇关于Google的TensorFlow中的Theano Dimshuffle等效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!