【问题标题】:padding in tf.data.Dataset in tensorflow张量流中的 tf.data.Dataset 中的填充
【发布时间】:2020-08-05 18:37:10
【问题描述】:

代码:

a=training_dataset.map(lambda x,y: (tf.pad(x,tf.constant([[13-int(tf.shape(x)[0]),0],[0,0]])),y))

给出以下错误:

TypeError: in user code:

<ipython-input-32-b25101c2110a>:1 None  *
    a=training_dataset.map(lambda x,y: (tf.pad(tensor=x,paddings=tf.constant([[13-int(tf.shape(x)[0]),0],[0,0]]),mode="CONSTANT"),y))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:264 constant  **
    allow_broadcast=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:282 _constant_impl
    allow_broadcast=allow_broadcast))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py:456 make_tensor_proto
    _AssertCompatible(values, dtype)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py:333 _AssertCompatible
    raise TypeError("Expected any non-tensor type, got a tensor instead.")

TypeError: Expected any non-tensor type, got a tensor instead.

但是,当我使用:

a=training_dataset.map(lambda x,y: (tf.pad(x,tf.constant([[1,0],[0,0]])),y))

上面的代码工作正常。 这使我得出结论:13-tf.shape(x)[0] 出了点问题,但无法理解是什么。 我尝试将tf.shape(x)[0] 转换为int(tf.shape(x)[0]),但仍然出现同样的错误。

我希望代码做什么: 我有一个tf.data.Dataset 对象,该对象具有大小为(None,128) 的可变长度序列,其中第一个维度(无)小于13。我想填充序列以使每个集合的大小为13,即(13,128)。 有没有替代方法(如果上述问题无法解决)?

【问题讨论】:

    标签: python-3.x tensorflow2.0 tensorflow-datasets language-translation data-pipeline


    【解决方案1】:

    有效的解决方案:

    使用:

    paddings = tf.concat(([[13-tf.shape(x)[0],0]], [[0,0]]), axis=0)

    而不是使用:

    paddings = tf.constant([[13-tf.shape(x)[0],0],[0,0]])

    为我工作。 但是,我仍然无法弄清楚为什么后一个不起作用。

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2023-03-07
      相关资源
      最近更新 更多