-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloss.py
26 lines (17 loc) · 836 Bytes
/
loss.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
import numpy as np
class BinaryCrossEntropy:
def __init__(self):
pass
def __call__(self, y_pred, y_true):
ix_zeros = np.arange(0, y_true.shape[0])[y_true.reshape(-1) == 0]
ix_ones = np.arange(0, y_true.shape[0])[y_true.reshape(-1) == 1]
y_zero = np.log(1 - y_pred[ix_zeros] + 1e-10).sum()
y_one = np.log(y_pred[ix_ones] + 1e-10).sum()
return -1 * (y_zero + y_one)
def grad_input(self, X, y_true):
ix_zeros = np.arange(0, y_true.shape[0])[y_true.reshape(-1) == 0]
ix_ones = np.arange(0, y_true.shape[0])[y_true.reshape(-1) == 1]
grad = np.empty((y_true.shape[0], 1, 1), dtype="float")
grad[ix_zeros] = 1/(1-X[:, ix_zeros].reshape(-1,1,1))
grad[ix_ones] = -1/X[:, ix_ones].reshape(-1,1,1)
return grad