-
Notifications
You must be signed in to change notification settings - Fork 0
/
P4-Layers-with-OOP.py
54 lines (36 loc) · 1.14 KB
/
P4-Layers-with-OOP.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
44
45
46
47
48
49
50
51
52
53
54
'''
Generalizing layers with a class
'''
import numpy as np
np.random.seed(0)
'''
X: input, 4 samples each has 3 fetures
shape: 3x4
x1: [ 1, 2, -1.5].T
x2: [2.0, 5.0, 2.7].T
x3: [3, -1, 3.3].T
x4: [2.5, 2, -0.8].T
X: [x1,x3,x3,x4]
'''
#shape: 3x4
X = [[ 1, 2, 3, 2.5],
[2.0, 5.0, -1.0, 2.0],
[-1.5, 2.7, 3.3, -0.8]]
class Layer_Dense:
def __init__(self, n_neurons, n_inputs):
#intialize the weights randmoly
self.weights = 0.10 * np.random.randn(n_neurons, n_inputs)
#intialize the biases with zeros
self.biases = np.zeros((n_neurons, 1))
def forward(self, inputs):
#forword path
self.output = np.dot(self.weights, inputs) + self.biases
# Layer1 with 5 neurons and 3 inputs features
layer1 = Layer_Dense(5,3)
# Layer2 with 2 neurons and 5 inputs (5 inputs because this is the output from the previous Layer1),
layer2 = Layer_Dense(1,5)
layer1.forward(X)
print(layer1.output, layer1.output.shape)
layer2.forward(layer1.output)
print('-------------------------------------')
print(layer2.output, layer2.output.shape)