tensorflow 如何将先前转换为标准Tensor的具有嵌套粗糙维的粗糙Tensor转换回

bogh5gae  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(150)

RaggedTensor具有方法to_tensor()from_tensor()。但是,如果ragged_tensor具有嵌套维,则应用tf.RaggedTensor.from_tensor(ragged_tensor.to_tensor(), padding=0)似乎会失败
示例:

data = tf.ragged.constant([
    [[4,35,6,33], [7,2], [89,56,12]],
    [[2,11], [9]]
    ])

tf.RaggedTensor.from_tensor(data.to_tensor(), padding=0)

返回错误

Traceback (most recent call last):
    File "./src/tppmodel.py", line 34, in <module>
     tf.RaggedTensor.from_tensor(data.to_tensor(), padding=0)
    File "/opt/conda/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in    error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/opt/conda/lib/python3.8/site-packages/tensorflow/python/framework/tensor_shape.py", line 1307, in assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes () and (4,) are incompatible

预期值

tf.RaggedTensor.from_tensor(data.to_tensor(),..., padding=0) = data
insrf1ej

insrf1ej1#

我自己可能已经找到了答案。以防万一的帖子对某些人很有用:不使用padding参数,而是传递原始Tensor的嵌套行长度
第一个

相关问题