-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlayers.py
More file actions
41 lines (31 loc) · 846 Bytes
/
Copy pathlayers.py
File metadata and controls
41 lines (31 loc) · 846 Bytes
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
import numpy as np
def linear_forward(x, w, b):
N = x.shape[0]
D = np.prod(x.shape[1:])
x2 = np.reshape(x, (N, D))
out = np.dot(x2, w) + b
cache = (x, w, b)
return out, cache
def linear_backward(dout, cache):
x, w, b = cache
# print("x,b,w: ",x,b,w)
dx = np.dot(dout, w.T).reshape(x.shape)
dw = dout.T.dot(x.reshape(x.shape[0], np.product(x.shape[1:]))).T
db = np.sum(dout, axis=0)
return dx, dw/x.shape[0], db/x.shape[0]
def relu_forward(x):
out = np.maximum(0, x)
cache = x
return out, cache
def relu_backward(dout, cache):
x = cache
dx = np.array(dout, copy=True)
dx[x <= 0] = 0
return dx
def sigmoid_forward(x):
out = 1.0/(1 + np.exp(-x))
cache = out*(1-out)
return out, cache
def sigmoid_backward(dout, cache):
x = cache
return x*dout