-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAffine_layer.py
More file actions
57 lines (43 loc) · 1.6 KB
/
Copy pathAffine_layer.py
File metadata and controls
57 lines (43 loc) · 1.6 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
51
52
53
54
55
56
57
import numpy as np
def affine_forward(x, w, b):
"""Computes the forward pass for an affine (fully connected) layer.
The input x has shape (N, d_1, ..., d_k) and contains a minibatch of N
examples, where each example x[i] has shape (d_1, ..., d_k). We will
reshape each input into a vector of dimension D = d_1 * ... * d_k, and
then transform it to an output vector of dimension M.
Inputs:
- x: A numpy array containing input data, of shape (N, d_1, ..., d_k)
- w: A numpy array of weights, of shape (D, M)
- b: A numpy array of biases, of shape (M,)
Returns a tuple of:
- out: output, of shape (N, M)
- cache: (x, w, b)
"""
out = None
# reshape the input
N = x.shape[0]
x_flat = x.reshape(N,-1)
out = x_flat.dot(w) + b
cache = (x,w,b)
return out,cache
def affine_backward(dout, cache):
"""Computes the backward pass for an affine (fully connected) layer.
Inputs:
- dout: Upstream derivative, of shape (N, M)
- cache: Tuple of:
- x: Input data, of shape (N, d_1, ... d_k)
- w: Weights, of shape (D, M)
- b: Biases, of shape (M,)
Returns a tuple of:
- dx: Gradient with respect to x, of shape (N, d1, ..., d_k)
- dw: Gradient with respect to w, of shape (D, M)
- db: Gradient with respect to b, of shape (M,)
"""
dx, dw, db = None, None, None
x, w, b = cache
N = x.shape[0]
x_flat = x.reshape(N,-1)
dw = x_flat.T.dot(dout)
db = np.sum(dout,axis=0)
dx = dout.dot(w.T).reshape(x.shape)
return dx, dw, db