python - Tensorflow Hub 形状未定义

标签 python tensorflow keras

我正在尝试加载预先训练的嵌入模型,但无论我给出什么形状,我都找不到输入形状。这似乎是一个非常受欢迎的选择,但我在 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))

最佳答案

您可以通过将输入形状参数保留为空元组来将功能 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)

您的模型如下所示: Model

关于python - Tensorflow Hub 形状未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63715048/

相关文章:

python - 当我更改分配的数字时,为什么值没有改变?

python - 为什么我的模型在第二个时期过度拟合?

python - 联邦学习: convert my own image dataset into tff simulation Clientdata

tensorflow - Adam Optimizer减轻体重的正确方法是什么

python - 使用 ImageDataGenerator 时的 Keras 分割训练测试集

python - 在从 JavaScript 接收多个长轮询请求时,如何同时轮询另一台服务器?

python - 如何根据不同的列名合并两个数据框

python - 迭代器作为 bool 语句?

python - Keras:微调 Inception 时精度下降

python - 迁移学习,错误的密集层形状