首页 > 解决方案 > 如何将张量重塑为占位符的形状?

问题描述

如何将张量重塑为占位符的形状?

a = tf.placeholder(shape=[None, None, 48])
h, w, c = a.shape
b = tf.reshape(a, shape=[-1, 4, 4, 3]) # flatten the first two dimension and expand the last dimension
# do something
c = tf.reshape(b, shape=[h, w, 3]) # reshape back to the shape of a, but error occurs

我想将张量b恢复为 placeholder 的形状a

标签: tensorflow

解决方案


a具有在图构建时未知的动态形状,因此您需要转向张量流操作,更具体地说,tf.shape它会(动态地)返回张量的形状。

所以在你的例子中你可以使用例如

s = tf.shape(a)
c = tf.reshape(b, shape=[s[0], s[1], 3])

推荐阅读