【发布时间】:2016-11-19 05:04:27
【问题描述】:
假设我们有
a = tf.placeholder(tf.float32, shape=(None, 3072))
b = a.get_shape()[0]
如何转换 b 以便在进一步计算中使用它,例如对于给定的张量 T 我将能够创建一个新的张量,比如
newT = T / b
【问题讨论】:
标签: python tensorflow
假设我们有
a = tf.placeholder(tf.float32, shape=(None, 3072))
b = a.get_shape()[0]
如何转换 b 以便在进一步计算中使用它,例如对于给定的张量 T 我将能够创建一个新的张量,比如
newT = T / b
【问题讨论】:
标签: python tensorflow
你必须使用 Graph 操作:
a = tf.placeholder(tf.float32, shape=(None, 3072))
b = tf.shape(a)[0]
返回
<tf.Tensor 'strided_slice:0' shape=() dtype=int32>
而b = a.get_shape()[0]
返回
Dimension(None)
【讨论】:
你目前的方式已经奏效了。我用下面的代码试了一下,效果很好:
x = [[1,2,3],[4,5,6], [7,8,9]]
x = tf.constant(x)
size = x.get_shape()[0]
x /= size
with googlelog.Capture():
p_op = tf.Print(x, [x], "output: ", summarize=10)
sess.run(p_op)
有输出:
output: [0 0 1 1 1 2 2 2 3]
【讨论】: