0

I have a Keras-related problem:

Jupiter notebook's screenshot with code and error

code:

new_model.predict(x_test[0:1])

output:

array([[0.03558755, 0.03124422, 0.12950344, 0.29694492, 0.08063059,
            0.13757695, 0.18792078, 0.01830993, 0.07450092, 0.0077808 ]],
          dtype=float32)

code:

new_model.predict_classes(x_test[0:1])

output:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-2ab62853e974> in <module>()
      1 # Output the prediction result.
----> 2 new_model.predict_classes(x_test[0:1])

NameError: name 'new_model' is not defined

But in my guidebook the output is array([3])?

If you can please help to correct it. Thank you!

Timus
  • 10,974
  • 5
  • 14
  • 28

1 Answers1

0

The error in your screen shot is different from the error in your code snippet.

The first error may be related to the model you are using. How did you define it? Checkout this question. The main point is that predict_classes method is only available for Sequence models. Not sure if is your case.

That said you can get back your class with simply this:

import numpy as np

y_pred = new_model.predict(X_test)
y_classes = np.argmax(y_pred, axis=1)

For the second case when you got the NameError: name 'new_model' is not defined, the error is pretty clear: this object is not defined. Make sure your kernel is still alive and didn't get restarted between first and second call to the object, and make sure you didn't call del new_model anywhere before running this cell.

Rodrigo Laguna
  • 1,796
  • 1
  • 26
  • 46