相关疑难解决方法(0)

tf.zeros()的动态大小(用于无维度的占位符)

请考虑以下代码:

x = tf.placeholder("float", shape=[42, 4])
y = tf.zeros([42, 4], "float")
xy_stacked = tf.concat(1, [x, y])

print(x.get_shape())
print(y.get_shape())
print(xy_stacked.get_shape())
Run Code Online (Sandbox Code Playgroud)

这将产生以下输出,如预期的那样:

TensorShape([Dimension(42), Dimension(4)])
TensorShape([Dimension(42), Dimension(4)])
TensorShape([Dimension(42), Dimension(8)])
Run Code Online (Sandbox Code Playgroud)

但是,如果占位符具有动态维度,该维度在运行时由传递给的值确定feed_dict=,如占位符通常那样:

x = tf.placeholder("float", shape=[None, 4])
y = tf.zeros([None, 4], "float")
xy_stacked = tf.concat(1, [x, y])
Run Code Online (Sandbox Code Playgroud)

这会产生错误tf.zeros([None, 4], "float").显然Dimension(None)不允许tf.zeros:

TypeError                                 Traceback (most recent call last)
<ipython-input-24-277eca38a392> in <module>()
      2 
      3 x = tf.placeholder("float", shape=[None, 4])
----> 4 y = tf.zeros([None, 4], "float")
      5 xy_stacked = tf.concat(1, …
Run Code Online (Sandbox Code Playgroud)

tensorflow

17
推荐指数
1
解决办法
1万
查看次数

标签 统计

tensorflow ×1