Mark As Completed Discussion

Loss function

While training our neural network, we will need to calculate the loss depending on the prediction of the network, and target labels. We can simply use a function to implement the loss function equation of categorical cross-entropy.

PYTHON
1def categorical_crossentropy(target, output):
2    output /= output.sum(axis=-1, keepdims=True)
3    output = np.clip(output, 1e-7, 1 - 1e-7) # So that weights dont become 0
4    return np.sum(target * -np.log(output), axis=-1, keepdims=False)