-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
43 lines (24 loc) · 938 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
27
28
29
30
31
32
33
34
import numpy as np
from utils import sigmoid_prime
class MSELoss():
@staticmethod
def fn(y_hat, y):
return 0.5*np.linalg.norm(y_hat - y)**2
@staticmethod
def delta(z, y_hat, y):
"""Returns the error delta from the output layer."""
return (y_hat, y) * sigmoid_prime(z)
class BCELoss():
"""Binary cross-entropy loss."""
@staticmethod
def fn(y_hat, y):
return np.sum(np.nan_to_num(-y * np.log(y_hat) - (1 - y) * np.log(1 -
y_hat)))
@staticmethod
def delta(z, y_hat, y):
"""Return the error delta from the output layer. Note that the
parameter ``z`` is not used by the method. It is included in
the method's parameters in order to make the interface
consistent with the delta method for other cost classes.
"""
return (y_hat - y)