-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlosses.py
25 lines (22 loc) · 986 Bytes
/
losses.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
import numpy as np, math
import activations
def quadratic(prediction, label, key):
if key == 'normal':
#print(prediction, label)
return np.sum(0.5*(label - prediction)**2)
elif key == 'derivative':
return prediction - label
#elif key == 'delta':
# return np.einsum('ij, j->i', (prediction - label), activations.sigmoid(inputs, 'derivative'))
else:
print('Incorrect key: use either normal or derivative')
def cross_entropy(prediction, label, key):
if key == 'normal':
#print(label, prediction, np.log(prediction))
#print(np.sum(prediction))
#return (np.multiply(label, np.log(prediction)) + np.multiply(1-label), np.log(1-prediction))
return (np.argmax(label) * math.log(np.max(prediction)) + (1 - np.argmax(label)) * math.log(1 - np.max(prediction)))
elif key == 'derivative':
return prediction - label
else:
print('Incorrect key: use either normal or derivative')