1
1
import numpy as np
2
2
3
+
3
4
class SimpleANN :
4
5
"""
5
6
Simple Artificial Neural Network (ANN)
@@ -9,7 +10,13 @@ class SimpleANN:
9
10
- Example demonstrates solving the XOR problem.
10
11
"""
11
12
12
- def __init__ (self , input_size : int , hidden_size : int , output_size : int , learning_rate : float = 0.1 ) -> None :
13
+ def __init__ (
14
+ self ,
15
+ input_size : int ,
16
+ hidden_size : int ,
17
+ output_size : int ,
18
+ learning_rate : float = 0.1 ,
19
+ ) -> None :
13
20
"""
14
21
Initialize the neural network with random weights and biases.
15
22
@@ -84,11 +91,15 @@ def feedforward(self, inputs: np.ndarray) -> np.ndarray:
84
91
"""
85
92
self .hidden_input = np .dot (inputs , self .weights_input_hidden ) + self .bias_hidden
86
93
self .hidden_output = self .sigmoid (self .hidden_input )
87
- self .final_input = np .dot (self .hidden_output , self .weights_hidden_output ) + self .bias_output
94
+ self .final_input = (
95
+ np .dot (self .hidden_output , self .weights_hidden_output ) + self .bias_output
96
+ )
88
97
self .final_output = self .sigmoid (self .final_input )
89
98
return self .final_output
90
99
91
- def backpropagation (self , inputs : np .ndarray , targets : np .ndarray , outputs : np .ndarray ) -> None :
100
+ def backpropagation (
101
+ self , inputs : np .ndarray , targets : np .ndarray , outputs : np .ndarray
102
+ ) -> None :
92
103
"""
93
104
Perform backpropagation to adjust the weights and biases.
94
105
@@ -109,13 +120,21 @@ def backpropagation(self, inputs: np.ndarray, targets: np.ndarray, outputs: np.n
109
120
hidden_error = output_gradient .dot (self .weights_hidden_output .T )
110
121
hidden_gradient = hidden_error * self .sigmoid_derivative (self .hidden_output )
111
122
112
- self .weights_hidden_output += self .hidden_output .T .dot (output_gradient ) * self .learning_rate
113
- self .bias_output += np .sum (output_gradient , axis = 0 , keepdims = True ) * self .learning_rate
123
+ self .weights_hidden_output += (
124
+ self .hidden_output .T .dot (output_gradient ) * self .learning_rate
125
+ )
126
+ self .bias_output += (
127
+ np .sum (output_gradient , axis = 0 , keepdims = True ) * self .learning_rate
128
+ )
114
129
115
130
self .weights_input_hidden += inputs .T .dot (hidden_gradient ) * self .learning_rate
116
- self .bias_hidden += np .sum (hidden_gradient , axis = 0 , keepdims = True ) * self .learning_rate
131
+ self .bias_hidden += (
132
+ np .sum (hidden_gradient , axis = 0 , keepdims = True ) * self .learning_rate
133
+ )
117
134
118
- def train (self , inputs : np .ndarray , targets : np .ndarray , epochs : int = 10000 ) -> None :
135
+ def train (
136
+ self , inputs : np .ndarray , targets : np .ndarray , epochs : int = 10000
137
+ ) -> None :
119
138
"""
120
139
Train the neural network on the given input and target data.
121
140
@@ -135,7 +154,7 @@ def train(self, inputs: np.ndarray, targets: np.ndarray, epochs: int = 10000) ->
135
154
self .backpropagation (inputs , targets , outputs )
136
155
if epoch % 1000 == 0 :
137
156
loss = np .mean (np .square (targets - outputs ))
138
- print (f' Epoch { epoch } , Loss: { loss } ' )
157
+ print (f" Epoch { epoch } , Loss: { loss } " )
139
158
140
159
def predict (self , inputs : np .ndarray ) -> np .ndarray :
141
160
"""
0 commit comments