【问题标题】:Tensorflow Hub shape not definedTensorFlow Hub 形状未定义
【发布时间】:2020-09-03 10:13:33
【问题描述】:

我正在尝试加载预训练的嵌入模型,但无论我给出什么形状,我都找不到输入形状。这似乎是一个非常受欢迎的选择,但我在 tensorflow 中心页面上找不到任何关于使用什么输入形状的指标。输入序列应该具有可变长度,因此我使用无输入形状。 Keras 自动提供批量大小

embedding_url = 'https://tfhub.dev/google/universal-sentence-encoder-large/5'


embedding_layer = hub.KerasLayer(embedding_url)
premises = tf.keras.Input(shape=(None,))
conclusion = tf.keras.Input(shape=(None,))
x1 = embedding_layer(premises)
x2 = embedding_layer(conclusion) 


model = tf.keras.Model(inputs=[premises, conclusion], outputs=[x1, x2])

这是我得到的错误

ValueError: Python inputs incompatible with input_signature:
      inputs: (
        Tensor("input_5:0", shape=(None, None), dtype=float32))
      input_signature: (
        TensorSpec(shape=<unknown>, dtype=tf.string, name=None))

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    您可以通过将输入形状参数保留为空元组来将功能 API 与 KerasLayer 一起使用。

    代码:

    import tensorflow_hub as hub
    import tensorflow as tf
    embedding_url = 'https://tfhub.dev/google/universal-sentence-encoder-large/5'
    
    premises = tf.keras.layers.Input(shape=(), name="Input1", dtype=tf.string)
    conclusion = tf.keras.layers.Input(shape=(), name="Input2", dtype=tf.string)
    embedding_layer = hub.KerasLayer(embedding_url)
    x1 = embedding_layer(premises)
    x2 = embedding_layer(conclusion)
    
    model = tf.keras.Model(inputs=[premises, conclusion], outputs=[x1, x2])
    tf.keras.utils.plot_model(model, 'my_first_model.png', show_shapes=True)
    

    您的模型如下所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多