-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfc.py
47 lines (39 loc) · 1.1 KB
/
fc.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
import numpy as np
def affine_forward(x, w, b):
"""
:param x:(N, C1,C2,...Cn)
:param w: (C1*C2*...*Cn, C_out)
:param b: (C_out)
:return: output (N, C_out)
"""
rex = x.reshape((x.shape[0], -1))
output = np.dot(rex, w) + b
cache = (x, w, b)
return output, cache
def affine_backward(dout, cache):
"""
:param dout: Upstream derivative, of shape(N, C_out)
:param cache: Tuple of:
- x: Input data, of shape(N, C1,C2,...Cn)
- w: Weights, of shape(C1*C2*...*Cn, C_out)
- b: bias, of shape(C_out,)
:return:
a tuple of:
- dx: Gradient with respect to x, of shape (N, C1,C2,...Cn)
- dw: Gradient with respect to w, of shape (C1*C2*...*Cn, C_out)
- db: Gradient with respect to b, of shape (C_out, )
"""
x, w, b = cache
x_shape = x.shape
rex = x.reshape((x_shape[0], -1))
'''
out = x*w + b
dw = dout*x
dx = dout*w
db = dout
'''
dw = np.dot(rex.T, dout)
dx = np.dot(dout, w.T)
db = np.sum(dout, axis=0)
dx = np.reshape(dx, x_shape)
return dx, dw, db