forked from NeuroSyn-AI-Club/simple-deep-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectives.py
More file actions
50 lines (38 loc) · 1.18 KB
/
Copy pathObjectives.py
File metadata and controls
50 lines (38 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
45
46
47
48
49
50
import numpy as np
class Objective:
def cost(self, AL, Y):
pass
def cost_prime(self, AL, Y):
pass
class MeanSquaredError:
def cost(self, AL, Y):
cost = np.squeeze(np.sum(np.mean(np.square(AL - Y), axis=1, keepdims=False))) / AL.shape[0]
cost = np.squeeze(cost)
return cost
def cost_prime(self, AL, Y):
m = Y.shape[1]
cost_derivative = -2 / m * (Y - AL)
return cost_derivative
class CrossEntropyLoss:
def __init__(self, use_softmax=True):
self.use_softmax = use_softmax
def softmax(self, A):
e_A = np.exp(A)
return e_A / np.sum(e_A, axis=0, keepdims=False)
def cost(self, AL, Y):
A = self.softmax(AL) if self.use_softmax else AL
m = Y.shape[1]
cost = -np.sum(Y*np.log(A)) / m
cost = np.squeeze(cost)
return cost
def cost_prime(self, AL, Y):
m = Y.shape[1]
if self.use_softmax:
return AL - Y
else:
return -Y/(AL * m)
def compute_frobenius_norm(W, lambd, m):
if lambd == 0:
return 0
frobenius_norm = np.sum([ np.sum(np.square(w)) for w in W])
return frobenius_norm * lambd / m