-
Notifications
You must be signed in to change notification settings - Fork 0
/
elementary_functions.py
93 lines (64 loc) · 1.71 KB
/
elementary_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
"""
The following are a few elementary functions needed to implement the Deep ANN.
"""
import numpy as np
"""
SET1
The first set of functions return the said values along with a cache (activation_cache : Z).
These are used for forward propagation
"""
def sigmoid(Z):
A = 1/(1+np.exp(np.float32(-Z)))
cache = Z
return A, cache
def relu(Z):
A = np.maximum(Z,0)
cache = Z
return A, cache
def tanh(Z):
A = (np.exp(Z) - np.exp(-Z) ) /(np.exp(Z) + np.exp(-Z))
cache = Z
return A, cache
"""
SET2
These are the set of functions return the derivatives of the said functions.
They are used in backward propagation.
"""
def sigmoid_backwards(dA, activation_cache):
Z = activation_cache
A , cache = sigmoid(Z)
g_dashZ = np.multiply(A, 1 - A)
dZ = np.multiply(Z, g_dashZ)
return dZ
def relu_backwards(dA, activation_cache):
Z = activation_cache
g_dashZ = np.multiply(Z, Z>0)
dZ = np.multiply(dA, g_dashZ)
return dZ
def tanh_backwards(dA, activation_cache):
Z = activation_cache
g_dashZ = 1 - (tanh(Z)**2)
dZ = np.multiply(dA, g_dashZ)
return dZ
"""
SET3
These functions compute the cost and return the same.
"""
def compute_cost(AL, Y, parameters, lambd = 0):
m = Y.shape[1]
L = len(parameters)//2
cost1 = np.dot(Y, (np.log(AL)).T )
cost2 = np.dot(1-Y, (np.log(1-AL)).T)
# print "cost 1 : " , cost1
# print "cost 2 : " , cost2
# cost = -(cost1 + cost2)/(m)
cost = -(np.dot( Y , (np.log(AL)).T ) + np.dot( 1-Y, (np.log(1-AL)).T ) )
cost = np.squeeze(cost)
cost = cost/m
if lambd != 0:
L2_regularization_cost = 0
for l in xrange(L):
L2_regularization_cost += np.sum(np.square(parameters["W" + str(l+1)]))
L2_regularization_cost *= (lambd/(2*m))
cost += L2_regularization_cost
return cost