我正在嘗試加載預先訓練的嵌入模型,但無論我給出什么形狀,我都找不到輸入形狀。這似乎是一個非常受歡迎的選擇,但我在張量流中心頁面上找不到任何關于要使用什么輸入形狀的指示符。輸入序列應該具有可變長度,因此我使用無輸入形狀。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))
1 回答

HUH函數
TA貢獻1836條經驗 獲得超4個贊
您可以通過將輸入形狀參數保留為空元組來將功能 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)
你的模型看起來像這樣:
添加回答
舉報
0/150
提交
取消