0

I'm learning TensorFlow and LSTM and I'm wondering why my prediction output has multiple values when I'm training it to return one. My goal is to get a single value between 0 and 1 after training it with arrays for sentiment analysis.

The training input data looks like:

[[59, 21, ... 118, 194], ... [12, 110, ... 231, 127]]

All input arrays are of the same length padded with 0. The training target data looks like:

[1.0, 0.5, 0.0, 1.0, 0.0 ...]

Model:

model = Sequential()
model.add(Embedding(input_length, 64, mask_zero=True))
model.add(LSTM(100))
model.add(Dense(1, activation=tf.nn.sigmoid))

Why does the prediction seem to evaluate each individual value at a time instead of an array as a whole?

model.predict([192])
# Returns [[0.5491102]]
model.predict([192, 25])
# Returns [[0.5491102, 0.4923803]]
model.predict([192, 25, 651])
# Returns [[0.5491102, 0.4923803, 0.53853387]]

I don't want to take an average of the output because the relationships between the values in the input arrays matter for sentiment analysis. If I'm training to predict a single value I'm not understanding why a single value isn't output. I'm new to TensorFlow, Keras, and layered neural networks, so I'm sure I'm missing something obvious.

2
  • Have you tried model.predict([[192, 25]])? Commented Dec 30, 2018 at 16:15
  • @pooyan Yes, same output. Commented Dec 30, 2018 at 16:18

1 Answer 1

8

When you write:

model.predict([192, 25, 651])

it is if you are giving the model three input samples and therefore in return you would get three outputs, one for each input sample. Instead, if by [192, 25, 651] you really mean one input sample, then you wrap it in two lists:

model.predict([[[192, 25, 651]]])

The reason: the most outer list corresponds to the list of all the input data for all the input layers of the model, which is one here. The second list corresponds to the data for the first (and only) input layer and the third list corresponds the one input sample. That's the case with list inputs, since multi-input (and multi-output) Keras models should take a list of input arrays as input. One better way is to use a numpy array instead:

model.predict(np.array([[192, 25, 651]]))

np.array([[192, 25, 651]]) has a shape of (1,3) which means one sample of length 3.

Sign up to request clarification or add additional context in comments.

4 Comments

I get the same results.
@MattS Put it in another list. I am editing my answer.
@MattS Edited my answer.
WOW that was it! Makes sense. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.