-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural4.py
126 lines (98 loc) · 4.18 KB
/
neural4.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
import nnfs
from nnfs.datasets import spiral_data
nnfs.init()
class Layer_Dense:
def __init__(self, n_inputs, n_neurons):
self.weights = 0.10 * np.random.randn(n_inputs, n_neurons)
self.biases = np.zeros((1, n_neurons))
def forward(self, inputs):
self.inputs = inputs # save input data
self.output = np.dot(inputs, self.weights) + self.biases
def backward(self, dvalues):
# Gradients of weights
self.dweights = np.dot(self.inputs.T, dvalues)
# Gradients of biases
self.dbiases = np.sum(dvalues, axis=0, keepdims=True)
# Gradients of inputs
self.dinputs = np.dot(dvalues, self.weights.T)
class Activation_ReLU:
def forward(self, inputs):
self.output = np.maximum(0, inputs)
class Activation_Softmax:
def forward(self, inputs):
exp_values = np.exp(inputs - np.max(inputs, axis=1, keepdims=True))
probabilities = exp_values / np.sum(exp_values, axis=1, keepdims=True)
self.output = probabilities
def backward(self, dvalues):
# Create uninitialized array
self.dinputs = np.empty_like(dvalues)
# Enumerate outputs and gradients
for index, (single_output, single_dvalues) in enumerate(zip(self.output, dvalues)):
# Flatten output array
single_output = single_output.reshape(-1, 1)
# Calculate Jacobian matrix of the output and the input
jacobian_matrix = np.diagflat(single_output) - np.dot(single_output, single_output.T)
# Calculate sample-wise gradient and add it to the array of gradients
self.dinputs[index] = np.dot(jacobian_matrix, single_dvalues)
X, y = spiral_data(samples=100, classes=3)
dense1 = Layer_Dense(2, 3)
activation1 = Activation_ReLU()
dense2 = Layer_Dense(3, 3)
activation2 = Activation_Softmax()
dense1.forward(X)
activation1.forward(dense1.output)
dense2.forward(activation1.output)
activation2.forward(dense2.output)
class Loss_CategoricalCrossentropy:
def forward(self, y_pred, y_true):
samples = len(y_pred)
y_pred_clipped = np.clip(y_pred, 1e-7, 1 - 1e-7)
if len(y_true.shape) == 1:
correct_confidences = y_pred_clipped[range(samples), y_true]
elif len(y_true.shape) == 2:
correct_confidences = np.sum(y_pred_clipped * y_true, axis=1)
negative_log_likelihoods = -np.log(correct_confidences)
return np.mean(negative_log_likelihoods)
def backward(self, dvalues, y_true):
samples = len(dvalues)
labels = len(dvalues[0])
if len(y_true.shape) == 1:
y_true = np.eye(labels)[y_true]
self.dinputs = -y_true / dvalues / samples
self.dinputs = self.dinputs / np.sum(self.dinputs, axis=1, keepdims=True)
class Optimizer_SGD:
def __init__(self, learning_rate=1.0):
self.learning_rate = learning_rate
def update_params(self, layer):
layer.weights -= self.learning_rate * layer.dweights
layer.biases -= self.learning_rate * layer.dbiases
loss_function = Loss_CategoricalCrossentropy()
optimizer = Optimizer_SGD(learning_rate=0.1)
for epoch in range(1000):
# Shuffle the data
indices = np.arange(len(X))
np.random.shuffle(indices)
X = X[indices]
y = y[indices]
# Forward pass
dense1.forward(X)
activation1.forward(dense1.output)
dense2.forward(activation1.output)
activation2.forward(dense2.output)
# Calculate loss and accuracy
loss = loss_function.forward(activation2.output, y)
predictions = np.argmax(activation2.output, axis=1)
accuracy = np.mean(predictions == y)
# Backward pass
loss_function.backward(activation2.output, y)
activation2.backward(loss_function.dinputs)
dense2.backward(activation2.dinputs)
activation1.backward(dense2.dinputs)
dense1.backward(activation1.dinputs)
# Update weights and biases
optimizer.update_params(dense1)
optimizer.update_params(dense2)
# Print progress
if epoch % 100 == 0:
print(f"epoch: {epoch}, loss: {loss:.4f}, accuracy: {accuracy:.4f}")