ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
loss_functions.py
(1045B)
1 from __future__ import division
2 import numpy as np
3 from mlfromscratch.utils import accuracy_score
4 from mlfromscratch.deep_learning.activation_functions import Sigmoid
5
6 class Loss(object):
7 def loss(self, y_true, y_pred):
8 return NotImplementedError()
9
10 def gradient(self, y, y_pred):
11 raise NotImplementedError()
12
13 def acc(self, y, y_pred):
14 return 0
15
16 class SquareLoss(Loss):
17 def __init__(self): pass
18
19 def loss(self, y, y_pred):
20 return 0.5 * np.power((y - y_pred), 2)
21
22 def gradient(self, y, y_pred):
23 return -(y - y_pred)
24
25 class CrossEntropy(Loss):
26 def __init__(self): pass
27
28 def loss(self, y, p):
29 # Avoid division by zero
30 p = np.clip(p, 1e-15, 1 - 1e-15)
31 return - y * np.log(p) - (1 - y) * np.log(1 - p)
32
33 def acc(self, y, p):
34 return accuracy_score(np.argmax(y, axis=1), np.argmax(p, axis=1))
35
36 def gradient(self, y, p):
37 # Avoid division by zero
38 p = np.clip(p, 1e-15, 1 - 1e-15)
39 return - (y / p) + (1 - y) / (1 - p)
40
41