moinsy
moinsy

Reputation: 83

TF Hub Universal Sentence Encoder Fine-tuning for sentence similarity

I'm working on fine-tuning the USE v4 model from tf hub. The dataset used is a sentence pair with target label [0,1].

Following is my code,

model = tf.keras.models.Sequential()
model.add(hub.KerasLayer('https://tfhub.dev/google/universal-sentence-encoder/4', 
                        input_shape=[2,], 
                        dtype=tf.string, 
                        trainable=True))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.summary()

resulting in error,

ValueError: Shape must be rank 1 but is rank 2 for '{{node text_preprocessor/tokenize/StringSplit/StringSplit}} = StringSplit[skip_empty=true](text_preprocessor/StaticRegexReplace_1, text_preprocessor/tokenize/StringSplit/Const)' with input shapes: [?,2], [].

It would be great if someone can help me understand where I have gone wrong.

Upvotes: 0

Views: 490

Answers (1)

wenxi
wenxi

Reputation: 133

As @qmeeus mentioned, input_shape need to be [], or you can skip specify the input_shape all together. So something like the following:

use_url = "https://tfhub.dev/google/universal-sentence-encoder-large/4"


feature_extractor_layer = hub.KerasLayer(use_url, input_shape=[], trainable=True)
model = tf.keras.Sequential([
    feature_extractor_layer,
    layers.Dense(1, activation='sigmoid')
])

This github issue might be helpful.

In order to pass in a pair of sentences, you can reuse the feature_extractor_layer in a Siamese network.

Upvotes: 1

Related Questions