繁体   English   中英

在 TF 2.1 Functional API 中使用 tf.hub.KerasLayer 会抛出 ValueError:Python 输入与 input_signature 不兼容:

[英]Using tf.hub.KerasLayer in TF 2.1 Functional API throws ValueError: Python inputs incompatible with input_signature:

我正在尝试在我的模型中应用带有 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)>})

整个脚本在这里

遇到类似的问题,经过一番挣扎,终于解决了这个问题。

输入层必须定义如下:

input = tf.keras.layers.Input(shape=(), name="Input", dtype=tf.string)

有关更多详细信息,请参阅此线程: https : //github.com/tensorflow/hub/issues/483

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM