代码之家  ›  专栏  ›  技术社区  ›  chaosink

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

  •  1
  • chaosink  · 技术社区  · 8 年前

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

    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 回到占位符的形状 a .

    1 回复  |  直到 8 年前
        1
  •  3
  •   P-Gn    8 年前

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

    所以在你的例子中,你可以使用。

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