-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfully_connected_layer.py
More file actions
32 lines (24 loc) · 923 Bytes
/
fully_connected_layer.py
File metadata and controls
32 lines (24 loc) · 923 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
import numpy as np
class FullyConnectedLayer:
def __init__(self, num_inputs, num_outputs, learning_rate, name):
self.weights = 0.01*np.random.rand(num_inputs, num_outputs)
self.bias = np.zeros((num_outputs, 1))
self.lr = learning_rate
self.name = name
def forward(self, inputs):
self.inputs = inputs
return np.dot(self.inputs, self.weights) + self.bias.T
def backward(self, dy):
if dy.shape[0] == self.inputs.shape[0]:
dy = dy.T
dw = dy.dot(self.inputs)
db = np.sum(dy, axis=1, keepdims=True)
dx = np.dot(dy.T, self.weights.T)
self.weights -= self.lr * dw.T
self.bias -= self.lr * db
return dx
def extract(self):
return {self.name+'.weights':self.weights, self.name+'.bias':self.bias}
def feed(self, weights, bias):
self.weights = weights
self.bias = bias