Thursday, 9 June 2022

Cannot set tensor: Dimension mismatch

I'm a little new to Tensor Flow and would like to understand why the following codes does not accept my input and how to resolve it. Prior to this, I was using mode_save but I have now converted this model to TFLite and would like to use it to predict the category of the inputted text.

I first load TFLite model and allocate tensors.

interpreter = tf.lite.Interpreter(model_path="/model.tflite")
interpreter.allocate_tensors()

Then get input and output tensors.

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

Then tokenized my input data. I take my text and use texts_to_sequences which is just tokenizer_from_json in Tensorflow, then pad it as I do in my model training. So my input is something like this...

[[144 122 557 136   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0]]

This is the shape I would except as I used the same one for my model training.

Now I just want to use tensor flow lite predict, however running the code below gives the following error:

input_shape = input_details[0]['shape']
text = 'We know what we are, but know not what we may be.'
seq = self.tokeniser.texts_to_sequences(text)
input_tensor= np.array(seq, dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_tensor)

Cannot set tensor: Dimension mismatch. Got 60 but expected 1 for dimension 1 of input 0.

Why?



from Cannot set tensor: Dimension mismatch

No comments:

Post a Comment