jeffheaton / t81_558_deep_learning

T81-558: Keras - Applications of Deep Neural Networks @Washington University in St. Louis
https://sites.wustl.edu/jeffheaton/t81-558/
Other
5.71k stars 3.04k forks source link

Would it possible to extend the logloss example to include a multi-class classification? #56

Closed gthaker closed 3 years ago

gthaker commented 4 years ago

The videos and the code are all wonderful. Thank you so much for taking so much time and trouble.

I was wondering if it would be possible to do a hand example for the following calculations for a multi-class classification. I can't figure out by hand and get the same calculations as:

from tensorflow.python.keras.utils import losses_utils cce = tf.keras.losses.CategoricalCrossentropy(reduction=losses_utils.ReductionV2.AUTO) truth = tf.constant([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) predictions = tf.constant([[.9, .05, .05], [.05, .89, .06], [.05, .01, .94]]) print('truth', truth) print('predictions', predictions) loss = cce(truth, predictions) print('CategorialCrossenthropy Loss: ', loss.numpy()) # Loss: 0.0945

what is equivalent hand calculation?

jeffheaton commented 4 years ago

Yes, I could add that, and will do that the next time I revamp that module. For multi-class essentially it boils down to calculating the log-loss on the class that was correct.

For each data item, just do the normal log-loss calculation on whichever output neuron corresponds to the correct prediction. For example, if the predictions are an array of 5 probabilities, summing to 1.0 and the correct answer was the first class (the first neuron) then calculate the logloss on the first array element (the probability of the first class) and the y (expected) of 1.0 (because it is correct classification).

The other probabilities do not matter and cancel out.