forked from cs231n/cs231n.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer_utils.py
174 lines (141 loc) · 5.76 KB
/
layer_utils.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from .layers import *
from .fast_layers import *
def affine_relu_forward(x, w, b):
"""Convenience layer that performs an affine transform followed by a ReLU.
Inputs:
- x: Input to the affine layer
- w, b: Weights for the affine layer
Returns a tuple of:
- out: Output from the ReLU
- cache: Object to give to the backward pass
"""
a, fc_cache = affine_forward(x, w, b)
out, relu_cache = relu_forward(a)
cache = (fc_cache, relu_cache)
return out, cache
def affine_relu_backward(dout, cache):
"""Backward pass for the affine-relu convenience layer.
"""
fc_cache, relu_cache = cache
da = relu_backward(dout, relu_cache)
dx, dw, db = affine_backward(da, fc_cache)
return dx, dw, db
# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
def affine_bn_relu_forward(x, w, b, gamma, beta, bn_param):
"""Convenience layer that performs an affine transform followed by bn then by a ReLU.
Inputs:
- x: Input to the affine layer
- w, b: Weights for the affine layer
- gamma, beta: Batch normalization weights
- bn_param: Batch normalization parameter
Returns a tuple of:
- out: Output from the ReLU
- cache: Object to give to the backward pass
"""
y, affine_cache = affine_forward(x, w, b)
z, bn_cache = batchnorm_forward(y, gamma, beta, bn_param)
out, relu_cache = relu_forward(z)
cache = (affine_cache, bn_cache, relu_cache)
return out, cache
def affine_bn_relu_backward(dout, cache):
"""Backward pass for the affine-bn-relu convenience layer.
"""
fc_cache, bn_cache, relu_cache = cache
dz = relu_backward(dout, relu_cache)
dy, dgamma, dbeta = batchnorm_backward_alt(dz, bn_cache)
dx, dw, db = affine_backward(dy, fc_cache)
return dx, dw, db, dgamma, dbeta
def affine_ln_relu_forward(x, w, b, gamma, beta, ln_param):
"""Convenience layer that performs an affine transform followed by ln then by a ReLU.
Inputs:
- x: Input to the affine layer
- w, b: Weights for the affine layer
- gamma, beta: Layer normalization weights
- ln_param: Layer normalization parameter
Returns a tuple of:
- out: Output from the ReLU
- cache: Object to give to the backward pass
"""
y, affine_cache = affine_forward(x, w, b)
z, ln_cache = layernorm_forward(y, gamma, beta, ln_param)
out, relu_cache = relu_forward(z)
cache = (affine_cache, ln_cache, relu_cache)
return out, cache
def affine_ln_relu_backward(dout, cache):
"""Backward pass for the affine-ln-relu convenience layer.
"""
fc_cache, ln_cache, relu_cache = cache
dz = relu_backward(dout, relu_cache)
dy, dgamma, dbeta = layernorm_backward(dz, ln_cache)
dx, dw, db = affine_backward(dy, fc_cache)
return dx, dw, db, dgamma, dbeta
# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
def conv_relu_forward(x, w, b, conv_param):
"""A convenience layer that performs a convolution followed by a ReLU.
Inputs:
- x: Input to the convolutional layer
- w, b, conv_param: Weights and parameters for the convolutional layer
Returns a tuple of:
- out: Output from the ReLU
- cache: Object to give to the backward pass
"""
a, conv_cache = conv_forward_fast(x, w, b, conv_param)
out, relu_cache = relu_forward(a)
cache = (conv_cache, relu_cache)
return out, cache
def conv_relu_backward(dout, cache):
"""Backward pass for the conv-relu convenience layer.
"""
conv_cache, relu_cache = cache
da = relu_backward(dout, relu_cache)
dx, dw, db = conv_backward_fast(da, conv_cache)
return dx, dw, db
def conv_bn_relu_forward(x, w, b, gamma, beta, conv_param, bn_param):
"""Convenience layer that performs a convolution, a batch normalization, and a ReLU.
Inputs:
- x: Input to the convolutional layer
- w, b, conv_param: Weights and parameters for the convolutional layer
- pool_param: Parameters for the pooling layer
- gamma, beta: Arrays of shape (D2,) and (D2,) giving scale and shift
parameters for batch normalization.
- bn_param: Dictionary of parameters for batch normalization.
Returns a tuple of:
- out: Output from the pooling layer
- cache: Object to give to the backward pass
"""
a, conv_cache = conv_forward_fast(x, w, b, conv_param)
an, bn_cache = spatial_batchnorm_forward(a, gamma, beta, bn_param)
out, relu_cache = relu_forward(an)
cache = (conv_cache, bn_cache, relu_cache)
return out, cache
def conv_bn_relu_backward(dout, cache):
"""Backward pass for the conv-bn-relu convenience layer.
"""
conv_cache, bn_cache, relu_cache = cache
dan = relu_backward(dout, relu_cache)
da, dgamma, dbeta = spatial_batchnorm_backward(dan, bn_cache)
dx, dw, db = conv_backward_fast(da, conv_cache)
return dx, dw, db, dgamma, dbeta
def conv_relu_pool_forward(x, w, b, conv_param, pool_param):
"""Convenience layer that performs a convolution, a ReLU, and a pool.
Inputs:
- x: Input to the convolutional layer
- w, b, conv_param: Weights and parameters for the convolutional layer
- pool_param: Parameters for the pooling layer
Returns a tuple of:
- out: Output from the pooling layer
- cache: Object to give to the backward pass
"""
a, conv_cache = conv_forward_fast(x, w, b, conv_param)
s, relu_cache = relu_forward(a)
out, pool_cache = max_pool_forward_fast(s, pool_param)
cache = (conv_cache, relu_cache, pool_cache)
return out, cache
def conv_relu_pool_backward(dout, cache):
"""Backward pass for the conv-relu-pool convenience layer.
"""
conv_cache, relu_cache, pool_cache = cache
ds = max_pool_backward_fast(dout, pool_cache)
da = relu_backward(ds, relu_cache)
dx, dw, db = conv_backward_fast(da, conv_cache)
return dx, dw, db