-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss_cce.py
27 lines (25 loc) · 910 Bytes
/
loss_cce.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from loss import Loss
import numpy as np
class LossCategoricalCrossentropy ( Loss ):
def forward ( self , y_pred , y_true ):
samples = len (y_pred)
y_pred_clipped = np.clip(y_pred, 1e-7 , 1 - 1e-7 )
if len (y_true.shape) == 1 :
correct_confidences = y_pred_clipped[
range (samples),
y_true
]
elif len (y_true.shape) == 2 :
correct_confidences = np.sum(
y_pred_clipped * y_true,
axis = 1
)
negative_log_likelihoods = - np.log(correct_confidences)
return negative_log_likelihoods
def backward ( self , dvalues , y_true ):
samples = len (dvalues)
labels = len (dvalues[ 0 ])
if len (y_true.shape) == 1 :
y_true = np.eye(labels)[y_true]
self.dinputs = - y_true / dvalues
self.dinputs = self.dinputs / samples