-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDense.py
30 lines (20 loc) · 849 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
26
27
28
29
30
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 24 15:41:33 2022
@author: ASUS
"""
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.biases = np.random.randn(output_size,1)
def forward(self, input):
self.input = input
return (np.dot(self.weights, input) + self.biases)
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
self.biases -= learning_rate * output_gradient
return input_gradient