我正在尝试构建一个接收两个输入字符串的连体网络。我正在使用字符串类型,因为我加载了一个我想要训练的 USE 层。
def siamese_model():
left = Input(shape=(1,), dtype=tf.string)
right = Input(shape=(1,), dtype=tf.string)
module_url = "https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3"
embedding_layer = hub.KerasLayer(module_url, trainable=True)
left_encoded = embedding_layer(tf.squeeze(tf.cast(left, tf.string)))
right_encoded = embedding_layer(tf.squeeze(tf.cast(right, tf.string)))
cosine_sim = Dot(axes=1, normalize=True)([left_encoded, right_encoded])
siamese_net = Model(inputs=[left,right],outputs=cosine_sim)
return siamese_net
model = siamese_model()
model.compile(loss="mse",optimizer="adam", metrics=[pearson])
当我尝试
model.predict(x=[[["test"]], [["test"]]])
ValueError: Layer model_1 expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 1, None) dtype=string>]
如果我尝试使用浮点数,模型可以正确看到两个输入。
import numpy as np
x1 = np.zeros((1, 2))
model.predict(x=[x1,x1])
知道发生了什么吗?我确定这与输入的形状有关