Tensorflow Keras不适用于参差不齐的张量输入


问题内容

我正在使用Tensorflow 2.3。

如果我使用普通的tf张量输入,则以下示例可以正常工作:

import tensorflow as tf
text_input = tf.keras.Input([None], dtype=tf.string, name="text_input", ragged=False)
predictions = tf.gather(text_input, 0, axis=-1)
model = tf.keras.Model(inputs=[text_input], outputs=[predictions])
model(tf.constant([['A1', 'A2', 'A3'], ['B1', 'B2', 'B3']]))

<tf.Tensor: shape=(2,), dtype=string, numpy=array([b'A1', b'B1'], dtype=object)>

但是,如果将输入更改为参差不齐的张量,则在尝试创建模型时会出错。

import tensorflow as tf
ragged_input = tf.keras.Input([None], dtype=tf.string, name="ragged_input", ragged=True)
padded_input = ragged_input.to_tensor('')
predictions = tf.gather(padded_input, 0, axis=-1)
model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-201-9adaf4aae2b5> in <module>()
      3 padded_input = ragged_input.to_tensor('')
      4 predictions = tf.gather(padded_input, 0, axis=-1)
----> 5 model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])

13 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  You must feed a value for placeholder tensor 'Placeholder_38' with dtype int64 and shape [?]
     [[node Placeholder_38 (defined at <ipython-input-201-9adaf4aae2b5>:5) ]] [Op:__inference_keras_scratch_graph_136790]

Function call stack:
keras_scratch_graph

问题答案:

对我来说似乎是个错误,因为RaggedTensor对Keras的支持不是最好的(请参见例如此处)。我不确定是什么原因造成的,但是占位符转换失败。

如果可以的话,最好先使用所有RaggedTensor功能, 然后
再将其作为输入和设置传递ragged=False。如果您只想使用它进行方便的填充,并且所有图形操作都基于无参差的张量(在您的示例中就是这种情况),那么这不是问题:

import tensorflow as tf
ragged_input = tf.keras.Input([None], dtype=tf.string, name="ragged_input", ragged=False)
# padded_input = ragged_input.to_tensor('')
predictions = tf.gather(ragged_input, 0, axis=-1)

model = tf.keras.Model(inputs=[ragged_input], outputs=[predictions])
padded_input = tf.ragged.constant([['A1', 'A2'], ['B1', 'B2', 'B3']]).to_tensor('')
result = model(padded_input)
print(result)
# >>> tf.Tensor([b'A1' b'B1'], shape=(2,), dtype=string)