Tensorflow Hub shape not defined

I am trying to load in a pre-trained embedding model but no matter what shape I give, I cannot find the input shape. It seems like a pretty popular choice, but I cannot find any indicator on the tensorflow hub page on what input shape to use. The input sequence is supposed to have variable-length, so I use an input shape of none. Keras automatically provides the batch size

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

Here is the error I get

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

Answers

You can use Functional API with KerasLayer by keeping the input shape parameter as an empty tuple.

Code:

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)

Your model looks like this: Model

Posted on by Aniket Bote