我正在尝试在我的模型中应用带有 hub.KerasLayer 的 nnlm-en-dim50 预训练模块来进行文本分类和序列标记。该模型适用于普通的 tf.keras.layers.Embedding,但是当切换到 KerasLayers 时:
text_input_layer = tf.keras.Input(shape=(None, ), dtype=tf.string, name='Input_1')
hub_layer = hub.KerasLayer("https://tfhub.dev/google/tf2-preview/nnlm-en-dim50-with-normalization/1",
input_shape=[],
output_shape=[50],
dtype=tf.string,
name='hub_keras')
input_embedding = hub_layer(text_input_layer)
output, forward_h, forward_c, backward_h, backward_c = tf.keras.layers.Bidirectional(
tf.keras.layers.LSTM(
rnn_units,
stateful=False,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform'
)
)(input_embedding)
它抛出这个ValueError:
WARNING:tensorflow:AutoGraph could not transform <tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x1313d4f60> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: Python inputs incompatible with input_signature:
inputs: (
Tensor("Input_1:0", shape=(None, None), dtype=string))
input_signature: (
TensorSpec(shape=(None,), dtype=tf.string, name=None))
WARNING:tensorflow:AutoGraph could not transform <tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x1313d4f60> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: Python inputs incompatible with input_signature:
inputs: (
Tensor("Input_1:0", shape=(None, None), dtype=string))
input_signature: (
TensorSpec(shape=(None,), dtype=tf.string, name=None))
Traceback (most recent call last):
File "application.py", line 169, in <module>
input_embedding = hub_layer(text_input_layer)
File "/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 773, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/autograph/impl/api.py", line 237, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:
/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_hub/keras_layer.py:166 call *
result = f()
/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/saved_model/load.py:438 _call_attribute
return instance.__call__(*args, **kwargs)
/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py:568 __call__
result = self._call(*args, **kwds)
/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py:606 _call
results = self._stateful_fn(*args, **kwds)
/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py:2362 __call__
graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py:2661 _maybe_define_function
*args, **kwargs)
/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py:2185 canonicalize_function_inputs
self._flat_input_signature)
/Users/dimitrs/anaconda3/envs/deep_nlu/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py:2252 _convert_inputs_to_signature
format_error_message(inputs, input_signature))
ValueError: Python inputs incompatible with input_signature:
inputs: (
Tensor("Input_1:0", shape=(None, None), dtype=string))
input_signature: (
TensorSpec(shape=(None,), dtype=tf.string, name=None))
我使用 tf.data.Dataset.padded_batch 来输入列表,例如:['Add', 'Bronislau', 'Kaper', 'to', 'the', 'drive', 'playlist.']
并且使用 keras.layers.Embedding 我使用 ID 而不是字符串。一批 2 个训练样本如下所示:
[(<tf.Tensor: shape=(2, 8), dtype=string, numpy=
array([[b'Add', b'a', b'tune', b'to', b'my', b'elrow', b'Guest', b'List'],
[b'Add', b'Bronislau', b'Kaper', b'to', b'the', b'drive',
b'playlist.', b'']], dtype=object)>, {'dense': <tf.Tensor: shape=(2, 8), dtype=int32, numpy=
array([[1, 1, 2, 1, 4, 6, 6, 6],
[1, 3, 3, 1, 1, 6, 1, 0]], dtype=int32)>, 'dense_1': <tf.Tensor: shape=(2, 1), dtype=int32, numpy=
array([[1],
[1]], dtype=int32)>})
整个脚本在这里