Tomáš Bezouška
Tomáš Bezouška

Reputation: 1499

Tensor 'embedding_input' has invalid shape '[None, None]'

I am trying to create a tensorflow lite text multi-class classification model. I mostly copied the code from here: https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/keras/basic_text_classification.ipynb

Everything seems to work fine in tensorflow, but when I try to convert the save h5 model to Tensorflow Lite I get this error:

ValueError: None is only supported in the 1st dimension. Tensor 'embedding_input' has invalid shape '[None, None]'.

This is what my code looks like:

vocab_size = 15000 # of words in dictionary

model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(7, activation=tf.nn.sigmoid))

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit(...)
keras.models.save_model(model, graphFile)

converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file(graphFile)
tflite_model = converter.convert()
open("converted.tflite", "wb").write(tflite_model)

I'm guessing the issue is with the Embedding layer? What can I do to fix it?

Upvotes: 2

Views: 1782

Answers (1)

miaout17
miaout17

Reputation: 4875

The convert requires to know the shape of input tensor. Only the 1st dimension (batch) can be unknown (None). In some case, Keras doesn't annotate the known tensor shape. You can specify the input shape by passing the input_shapes optional argument:

converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file(
    graphFile,
    input_shapes={'embedding_input': [1, vocab_size]}
)

See also a similar issue: Tensorflow - h5 model to tflite conversion error

Upvotes: 1

Related Questions