我有一个张量,X
,形状为 (T, n, k)
。
如果我事先知道形状,就很容易 reshape ,tf.reshape(X, (T * n, k))
其中T, n, k
是整数,不是张量。但是如果我不知道这些形状,有没有办法做到这一点。似乎获取像 shape = tf.shape(X)
这样的形状并 reshape 是行不通的。也就是说,
tf.reshape(X, (tf.shape[0] * tf.shape[1], tf.shape[2]))
有什么想法吗?在我的应用程序中,T
和 k
在运行前已知,而 n
仅在运行时已知。
最佳答案
看看这个:
import tensorflow as tf
a, b, c = 2, 3, 4
x = tf.Variable(tf.random_normal([a, b, c], mean=0.0, stddev=1.0, dtype=tf.float32))
s = tf.shape(x)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
v1, v2, v3 = sess.run(s)
y = tf.reshape(x, [v1 * v2, v3])
shape = tf.shape(y)
print sess.run(y)
print sess.run(shape)
我在初始化后获取变量的形状,然后稍后使用它。另请查看 this answer ,因为它处理的是类似的事情。
关于python - 使用标量张量 reshape TensorFlow 中的张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705404/