Prediction W2 Assignment

In the following code we have the prediction for the number in the exemple, but I didn’t get all clear about the number in the array(predicting Two).

image_of_two = X[1015]

prediction = model.predict(image_of_two.reshape(1,400)) # prediction

print(f" predicting a Two: \n{prediction}“)
print(f” Largest Prediction index: {np.argmax(prediction)}")

The Output:

predicting a Two:
[[ -7.99 -2.23 0.77 -2.41 -11.66 -11.15 -9.53 -3.36 -4.42 -7.17]]
Largest Prediction index: 2

Is this the probability of the number? Why is it negative?

Thank you in advance!

They aren’t probabilities. The important thing is the positive number 0.77 in position [2] of the array. np.argmax finds the index with the largest value. We have trained the neural network to return smaller numbers if it doesn’t match and larger numbers if it does match. That’s why there are a bunch of negative numbers.

In the next step of the code it does the same thing using softmax which does return probabilities. If you add up all the numbers in the array prediction_p they all add up to 1.

Just to add, since the sigmoid-then-softmax functions are monotonic, it doesn’t matter whether it is included when you are trying to find the output with the highest value.

Hello @Alexandre_Leal,

Some other things that I want to add is that those numbers are called logits, and they can be positive or negative. Softmax transforms logits to probabilities. The model keeps logits as output to take advantage of the from_logits=True option which has been explained in this video by Andrew.


Thank you for all the explanations. Much appreciated!