# How to Inference With Keras Sequential Models (Text Classification)

I have the following LSTM model and I can't make inference with it:

print("Define LSTM model")

rnnmodel=Sequential()

rnnmodel.compile(loss="binary_crossentropy",
metrics=["accuracy"])

rnnmodel.fit(X_train, y_train,
batch_size=256,
epochs=1,
validation_data=(x_val, y_val))

score, acc=rnnmodel.evaluate(test_data, test_labels, batch_size=128)
print(f"Test accuracy with RNN: {acc}")


(epoch is 1 to test) I want to make an inference with the text, let's say

text=["the product was horrible"]


I check the documentation of tf.keras.Sequential and it states I should use the predict function and the input should be "A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs)."

So what I did is:

text=["the product was horrible"]
inference_sequence=tokenizer.texts_to_sequences(text)

predictions=rnnmodel.predict(inference_data)

print(predictions)


and it gives me the result [[0.63219154 0.33410403]]

However I've given only one sentence. Why it gives me two results? I checked the sigmoid documentation from here and for an confirmed it should return only one result. So what's the problem here?

I also tried other approaches to make inference like mentioned https://stackoverflow.com/questions/61443543/how-to-make-prediction-on-keras-text-classification

So I did:

text=["the product was horrible"]
rnnmodel.predict(text)


and it gives me the warning: WARNING:tensorflow:Model was constructed with shape (None, 1000) for input Tensor("embedding_input:0", shape=(None, 1000), dtype=float32), but it was called on an input with incompatible shape (None, 1). and stuck forever.

What should I do I just can't make an inference.

I checked the sigmoid documentation from here and for a confirmed it should return only one result. So what's the problem here?

You have used 2 Neurons at the output layer. So, each is responsible for one output.
Either, change the neuron count to 1 and y_true to the 1-D array.
Or change the activation function to Softmax.

text=["the product was horrible"]
rnnmodel.predict(text)


This will not work.
The embedding layer expects Label encoded data. So the first approach is correct.

• yep you're correct. Switched to softmax working now. Thank you. Aug 26 at 8:36