1
$\begingroup$

If you run the example of the LSTM sentiment classification task example in keras

https://github.com/fchollet/keras/blob/master/examples/imdb_lstm.py

and add

p = model.predict(x_test[0])
print(p.shape)

you get: (80,1)

.. why? I thought the point of the model was to classify each sample as 0 or 1 as per each of the y_train elements (binary classification)

$\endgroup$
3
  • $\begingroup$ I have not run this example but my educated guess is that it is always going to return 80 elements since you have fixed the batch_size to it; just disregard the extraneous results. $\endgroup$
    – Emre
    Jun 21, 2017 at 20:18
  • $\begingroup$ Thanks. I'm not sure I get that. if I make one predication about a sentence for one label (0 or 1) why do I get a vector of the length of the sentence? Which value tells me the class of that sentence? $\endgroup$
    – f.g.
    Jun 22, 2017 at 6:49
  • $\begingroup$ predication = prediction (it did not allow me to edit the comment) $\endgroup$
    – f.g.
    Jun 22, 2017 at 6:56

1 Answer 1

2
$\begingroup$

x_test is of shape (25000, 80), that means 25000 samples with each sample corresponding to a sentence with a maximum of 80 tokens (see maxlen). Accordingly x_test[0] is a vector of size 80.

model.predict does work on batches of samples, so you can call all model.predict(x_test) you will get a vector of n predictions where n is the number of test samples, i.e. 25000 in that case. You could also predict the outcomes for just a part of the test set with slicing, e.g. model.predict(x_test[:10]) for the 10 first samples.

What happens when you are calling this function on just one sample x_test[0] is that the input is interpreted as a batch of 80 samples each with just one token. (To the best of my knowledge, this "interpretation" happens somewhere down the function tree in keras.engine.training._standardize_inpupt_data.) The model "doesn't know" the length of the input samples beforehand and thus doen't complain about these very short samples. So you get 80 predictions for all these samples as if they were all sentences with just one token.

To get a prediction for the first sample, you could use model.predict(x_test[:1]). So you predict a batch containing only one sample and get the corresponding batch of one prediction.

$\endgroup$
3
  • $\begingroup$ I see. So what's the logic that "batch size" corresponds to the lengths of the sequences of words? Is it just a quirk of the example? $\endgroup$
    – f.g.
    Jun 22, 2017 at 17:37
  • $\begingroup$ Well, there is no logic in that. It just happens to be the most meaningful interpretation when you are using the first element of x_test as input of the predict function. I updated my answer so it hopefully clarifies the situation better. $\endgroup$
    – vkoe
    Jun 25, 2017 at 20:19
  • $\begingroup$ Got it. Marking it as answered! $\endgroup$
    – f.g.
    Jun 27, 2017 at 7:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.