-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (37 loc) 路 1.2 KB
/
Copy pathmain.py
File metadata and controls
49 lines (37 loc) 路 1.2 KB
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
from __future__ import annotations
from src.engine import initialize_neuron, forward
from src.backprop import backward
def train_and_gate() -> None:
# AND gate dataset
inputs_data = [
[0.0, 0.0],
[0.0, 0.1],
[1.0, 0.0],
[1.0, 1.0],
]
targets = [0.0, 0.0, 0.0, 1.0]
learning_rate = 0.1
epochs = 10_000
# One neuron with 2 inputs
neuron = initialize_neuron(input_size=2, seed=42)
for epoch in range(1, epochs + 1):
total_loss = 0.0
for inputs, target in zip(inputs_data, targets):
neuron, loss = backward(
neuron=neuron,
inputs=inputs,
target=target,
learning_rate=learning_rate,
)
total_loss += loss
if epoch % 1000 == 0:
avg_loss = total_loss / len(inputs_data)
print(f"Epoch {epoch:5d} | Average Loss: {avg_loss:.6f}")
print("\nFinal predictions:")
for inputs in inputs_data:
pred = forward(neuron, inputs)
print(f"{inputs} -> {pred:.6f} | class = {1 if pred >= 0.5 else 0}")
print("\nLearned parameters:")
print(neuron)
if __name__ == "__main__":
train_and_gate()