Build siamese network with two inputs as a string

Viewed 128

I'm trying to build a siamese network that receives two input strings. I'm using string type because I loaded a USE layer that I want to train.

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])

When I try

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>]

If I try with floats the model can see the two inputs correctly.

import numpy as np

x1 = np.zeros((1, 2))
model.predict(x=[x1,x1])

Any idea of whats going on? Im sure it has to do with the shape of the input

0 Answers
Related