4
4
- Uses Gradient Descent for backpropagation and Mean Squared Error (MSE) as the loss function.
5
5
- Example demonstrates solving the XOR problem.
6
6
"""
7
+
7
8
import numpy as np
8
9
9
10
@@ -31,7 +32,9 @@ def feedforward(self, X):
31
32
self .hidden_output = self .sigmoid (self .hidden_input )
32
33
33
34
# Output layer
34
- self .final_input = np .dot (self .hidden_output , self .weights_hidden_output ) + self .bias_output
35
+ self .final_input = (
36
+ np .dot (self .hidden_output , self .weights_hidden_output ) + self .bias_output
37
+ )
35
38
self .final_output = self .sigmoid (self .final_input )
36
39
37
40
return self .final_output
@@ -45,18 +48,24 @@ def backpropagation(self, X, y, output):
45
48
hidden_error = output_gradient .dot (self .weights_hidden_output .T )
46
49
hidden_gradient = hidden_error * self .sigmoid_derivative (self .hidden_output )
47
50
# Update weights and biases
48
- self .weights_hidden_output += self .hidden_output .T .dot (output_gradient ) * self .learning_rate
49
- self .bias_output += np .sum (output_gradient , axis = 0 , keepdims = True ) * self .learning_rate
51
+ self .weights_hidden_output += (
52
+ self .hidden_output .T .dot (output_gradient ) * self .learning_rate
53
+ )
54
+ self .bias_output += (
55
+ np .sum (output_gradient , axis = 0 , keepdims = True ) * self .learning_rate
56
+ )
50
57
self .weights_input_hidden += X .T .dot (hidden_gradient ) * self .learning_rate
51
- self .bias_hidden += np .sum (hidden_gradient , axis = 0 , keepdims = True ) * self .learning_rate
58
+ self .bias_hidden += (
59
+ np .sum (hidden_gradient , axis = 0 , keepdims = True ) * self .learning_rate
60
+ )
52
61
53
62
def train (self , X , y , epochs = 10000 ):
54
63
for epoch in range (epochs ):
55
64
output = self .feedforward (X )
56
65
self .backpropagation (X , y , output )
57
66
if epoch % 1000 == 0 :
58
67
loss = np .mean (np .square (y - output ))
59
- print (f' Epoch { epoch } , Loss: { loss } ' )
68
+ print (f" Epoch { epoch } , Loss: { loss } " )
60
69
61
70
def predict (self , X ):
62
71
return self .feedforward (X )
0 commit comments