【问题标题】:Tensor has shape [?, 0] -- how to reshape to [?,]张量的形状为 [?, 0] - 如何重塑为 [?,]
【发布时间】:2019-01-06 06:35:42
【问题描述】:

src 的形状为[?] 时,tf.gather(src, tf.where(src != 0)) 返回一个形状为[?, 0] 的张量。我不确定维度如何具有大小 0,我尤其不确定如何将张量改回来。我也没有在文档中找到任何解释这一点的内容。

我尝试tf.transpose(tensor)[0],但是转置张量的第一维大小为0,无法访问!怎么了?

【问题讨论】:

  • 你能发布一个可重现的代码示例吗?

标签: python python-3.x tensorflow reshape tensorflow-datasets


【解决方案1】:

我认为您应该使用tf.not_equal 对张量进行逐元素比较。

src = tf.constant([0, 1, 1, 0], dtype=tf.int8)
tf.gather(src, tf.where(tf.not_equal(src, 0))).eval(session=tf.Session())

array([[1],
       [1]], dtype=int8)

您也可以稍微缩短一下,使用tf.boolean_mask 代替tf.wheretf.gather

tf.boolean_mask(src, tf.not_equal(src, 0)).eval(session=tf.Session())
array([1, 1], dtype=int8)

注意输出形状的差异。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    相关资源
    最近更新 更多