-
Notifications
You must be signed in to change notification settings - Fork 1
/
backpropagation_with_dropout.py
133 lines (108 loc) · 3.8 KB
/
backpropagation_with_dropout.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 8 12:00:23 2020
@author: Dell
"""
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
def sigmoid_der(x):
w, h = x.shape
z = 1/(1+np.exp(-x))
z = z*(1-z)
assert (z.shape == (w, h))
return (z)
def relu_der(x):
w, h = x.shape
x[x<=0] = 0
x[x>0] = 1
assert (x.shape == (w, h))
return x
def sigmoid_backward(dA, cache, batch_norm):
if batch_norm:
Z, _, a, b, g = cache
else:
Z = cache
s = 1/(1+np.exp(-Z))
dZ = dA * s * (1-s)
assert (dZ.shape == Z.shape)
return dZ
def relu_backward(dA, cache, batch_norm):
if batch_norm:
Z, _, a, b, g = cache
else:
Z = cache
dZ = np.array(dA, copy=True) # just converting dz to a correct object.
# When z <= 0, you should set dz to 0 as well.
dZ[Z <= 0] = 0
assert (dZ.shape == Z.shape)
return dZ
def linear_backward(dZ, cache, keep_prob):
"""
dZ -> Gradient of cost w.r.t linear output
cache -> (A_prev, W, b)
dW -> gradient of cost w.r.t W
db -> gradient of cost w.r.t b
dA_prev -> gradient of cost w.r.t activation of previous layer
"""
A_prev, W, b, D = cache
m = A_prev.shape[1]
t = np.dot(dZ, A_prev.T)
dW = np.multiply(1/m, t)
db = (1/m)*np.sum(dZ, axis=1, keepdims=True)
dA_prev = np.dot(W.T, dZ)
dA_prev = np.multiply(dA_prev, D)
dA_prev = dA_prev / keep_prob
return dA_prev, dW, db
def linear_activation_backward(dA, cache, batch_norm, keep_prob, activation):
"""
dA -> post activation gradient
cache -> (linear_cache, activation_cache)
activation -> sigmoid or relu
"""
from batch_norm import forward_prop, batch_norm_init, back_prop
linear_cache, activation_cache = cache
if activation == 'relu':
dZ = relu_backward(dA, activation_cache, batch_norm)
#Z, Z_norm, Z_centered, std, gamma = activation_cache
if batch_norm:
dZ, dgamma, dbeta = back_prop(dZ, activation_cache)
else:
dgamma, dbeta = 0, 0
dZ = np.multiply(dA, np.int64(dA>0))
dA_prev, dW, db = linear_backward(dZ, linear_cache, keep_prob)
elif activation == 'sigmoid':
dZ = sigmoid_backward(dA, activation_cache, batch_norm)
#Z, mu, sigma, Z_norm, gamma, beta = activation_cache
if batch_norm:
dZ, dgamma, dbeta = back_prop(dZ, activation_cache)
else:
dgamma, dbeta = 0, 0
#assert (dZ.shape == (1, 8000))
dA_prev, dW, db = linear_backward(dZ, linear_cache, keep_prob)
return dA_prev, dW, db, dgamma, dbeta
def L_model_backward(AL, Y, caches, batch_norm, keep_prob):
from batch_norm import forward_prop, batch_norm_init, back_prop
grads = {}
L = len(caches)
m = AL.shape[1]
Y = Y.reshape(AL.shape)
dAL = -(np.divide(Y, AL) - np.divide(1-Y, 1-AL))
assert (dAL.shape == AL.shape)
# Lth layer
current_cache = caches[L-1]
grads["dA"+str(L-1)], grads["dW"+str(L)], grads["db"+str(L)], grads["dgamma"+str(L)], grads["dbeta"+str(L)] = linear_activation_backward(dAL, current_cache, batch_norm, keep_prob, activation='sigmoid')
# loop from L-2 to 0
for l in reversed(range(L-1)):
# Relu -> Linear
current_cache = caches[l]
dA_prev_temp, dW_temp, db_temp, dgamma_temp, dbeta_temp = linear_activation_backward(grads["dA"+str(l+1)], current_cache, batch_norm, keep_prob, activation='relu')
grads["dA"+str(l)] = dA_prev_temp
grads["dW"+str(l+1)] = dW_temp
grads["db"+str(l+1)] = db_temp
grads["dgamma"+str(l+1)] = dgamma_temp
grads["dbeta"+str(l+1)] = dbeta_temp
return grads