-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDense.py
25 lines (18 loc) · 816 Bytes
/
Dense.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
# Dense layer is a class inheriting from Layer and implements
# the dense fully connected layer
from Layer import Layer
import numpy as np
class Dense(Layer):
def __init__(self, input_size, output_size):
self.weights = np.random.randn(output_size, input_size)
self.bias = np.random.randn(output_size, 1)
def forward(self, input):
self.input = input
return np.dot(self.weights, self.input) + self.bias
def backward(self, output_gradient, learning_rate):
input_gradient = np.dot(self.weights.T, output_gradient)
weights_gradient = np.dot(output_gradient, self.input.T)
self.weights -= learning_rate * weights_gradient
bias_gradient = output_gradient
self.bias -= learning_rate * bias_gradient
return input_gradient