-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradientFuncs.py
More file actions
44 lines (30 loc) · 1.18 KB
/
Copy pathgradientFuncs.py
File metadata and controls
44 lines (30 loc) · 1.18 KB
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
import numpy as np
from numba import jit
@jit(nopython=True) # Set "nopython" mode for best performance, equivalent to @njit
def norm_calc(output):
delta = []
for row in output:
delta.append(np.linalg.norm(row))
return np.array(delta)
def categorical_crossentropy(targets, outputs):
samples = len(targets)
classes = len(targets[0])
for s in range(samples):
for c in range(classes):
if targets[s,c] == 1:
outputs[s,c] -= 1
return norm_calc(outputs)
def binary_crossentropy(targets, outputs):
samples = len(targets)
classes = len(targets[0])
for s in range(samples):
for c in range(classes):
if targets[s,c] == 1:
outputs[s,c] -= 1
return norm_calc(outputs)
gradient_func_dict = { 'binary_crossentropy' : binary_crossentropy,
'categorical_crossentropy': categorical_crossentropy }
# 'mean_squared_error' : mean_squared_error,
# 'mean_absolute_error' : mean_absolute_error }
def get_loss_func(identifier): # function to return gradient function corresponding to
return gradient_func_dict[identifier]