-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss_functions.py
executable file
·84 lines (62 loc) · 2.34 KB
/
loss_functions.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import numpy as np
def cross_entropy(p, t):
"""
Cross entropy function, which functions as a loss function when dealing with probabilities, target
needs to be a one hot encoding or have as many coulmns as prediction has rows
"""
loss_vector = np.zeros(p.shape[1])
# Iterating over the cases in the prediction (columns in p)
for i in range(p.shape[1]):
cross_entropy = 0
pred = p[:, i]
target = t[i]
# Iterating over the individual outputs from the neurons and applying the cross-entropy function
for j in range(len(pred)):
cross_entropy += target[j] * np.log2(pred[j])
cross_entropy *= -1
loss_vector[i] = cross_entropy
return loss_vector.mean()
def cross_entropy_der(p, t):
"""
Cross entropy function derivated
"""
result = np.zeros((p.shape[0], p.shape[1]))
# Iterating over the cases in the prediction (columns in p)
for i in range(p.shape[1]):
cross_entropy_derived = 0
pred = p[:, i]
target = t[i]
# Iterating over the individual outputs from the neurons and applying the cross-entropy function
for j in range(len(pred)):
cross_entropy_derived = -target[j] * (1 / pred[j] * np.log(2))
result[j, i] = cross_entropy_derived
return result
def mse(p, t):
"""
Mean-squared error function, target needs have as many coulmns as prediction has rows
"""
loss_vector = np.zeros(p.shape[1])
t_transposed = t.T
# Iterating over the cases in the prediction (columns in p)
for i in range(p.shape[1]):
pred = p[:, i]
target = t_transposed[:, i]
mse = ((pred - target)**2).mean()
loss_vector[i] = mse
return loss_vector.mean()
def mse_der(p, t):
"""
Mean-squared error derivative function, target needs to have as many coulmns as prediction has rows
"""
num_cases = p.shape[1]
num_outputs = p.shape[0]
result = np.empty((num_cases, num_outputs))
t_transposed = t.T
# Iterating over the cases in the prediction (columns in p)
for case in range(num_cases):
pred_case = p[:, case]
target = t_transposed[:, case]
for i in range(num_outputs):
mse_der = 2 * (1 / num_outputs) * (pred_case[i] - target[i])
result[case, i] = mse_der
return result