-
Notifications
You must be signed in to change notification settings - Fork 0
/
pch4.py
44 lines (32 loc) · 1.15 KB
/
pch4.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
import numpy as np
import nnfs
from nnfs.datasets import spiral_data
nnfs.init()
# Dense Layer
class Layer_Dense:
# Layer Initialization
def __init__(self, n_inputs, n_neurons):
# Initialize weights and biases
self.weights = 0.01 * np.random.randn(n_inputs, n_neurons)
self.biases = np.zeros((1, n_neurons))
# Forward Pass
def forward(self, inputs):
# Calculate output values from inputs, weights and biases
self.output = np.dot(inputs, self.weights) + self.biases
class Activation_ReLU:
def forward(self, inputs):
self.output = np.maximum(0, inputs)
# Create dataset
X, y = spiral_data(samples=100, classes=3)
# Create Dense layer with 2 input features and 3 output values
dense1 = Layer_Dense(2, 3)
# Perform a forward pass of our training data through this layer
dense1.forward(X)
# Make forward pass activation (to be used with Dense layer):
activation1 = Activation_ReLU()
# Let's see output of the first few samples:
print(dense1.output[:5])
# Forward pass through activation function
# Takes in output from previous layer
activation1.forward(dense1.output)
print(activation1.output[:5])