Python: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future

Arslan picture Arslan · Jun 15, 2017 · Viewed 34.8k times · Source

While trying out the udacity course deep learning assignment, I came across a problem with comparing the predictions of my model with the labels of training set. The arrays I'm using have shapes:

Training set (200000, 28, 28) (200000,)
Validation set (10000, 28, 28) (10000,)
Test set (10000, 28, 28) (10000,)

However, when checking the accuracy with the function:

def accuracy(predictions, labels):
    return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
          / predictions.shape[0])

It's giving me:

C:\Users\Arslan\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future. """

And it gives the accuracy as 0% for all datasets.

I think we cannot compare the arrays using '=='. How could I compare the arrays in the right way instead?

Answer

hpaulj picture hpaulj · Jun 15, 2017

I assume the error occurs in this expression:

np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))

can you tell us something about the 2 arrays, predictions, labels? The usual stuff - dtype, shape, some sample values. Maybe go the extra step and show the np.argmax(...) for each.

In numpy you can compare arrays of the same size, but it has become pickier about comparing arrays that don't match in size:

In [522]: np.arange(10)==np.arange(5,15)
Out[522]: array([False, False, False, False, False, False, False, False, False, False], dtype=bool)
In [523]: np.arange(10)==np.arange(5,14)
/usr/local/bin/ipython3:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.
  #!/usr/bin/python3
Out[523]: False