-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
178 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,49 @@ | ||
# (c) 2025 Mario "Neo" Sieg. <[email protected]> | ||
|
||
from magnetron import Tensor | ||
from magnetron.layer import DenseLayer | ||
from magnetron.model import SequentialModel, HyperParams | ||
import matplotlib.pyplot as plt | ||
from magnetron import Tensor, Module, Linear | ||
from magnetron.optim import SGD, mse_loss | ||
|
||
EPOCHS: int = 10000 | ||
RATE: float = 0.1 | ||
|
||
# Inputs: shape (4, 2) | ||
inputs = Tensor.const([ | ||
class XOR(Module): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.l1 = Linear(2, 2) | ||
self.l2 = Linear(2, 1) | ||
|
||
def forward(self, x: Tensor) -> Tensor: | ||
x = self.l1(x).tanh() | ||
x = self.l2(x).tanh() | ||
return x | ||
|
||
model = XOR() | ||
optim = SGD(model.parameters(), lr=1e-1) | ||
|
||
x = Tensor.const([ | ||
[0, 0], | ||
[0, 1], | ||
[1, 0], | ||
[1, 1] | ||
]) | ||
], name='x') | ||
|
||
# Targets: shape (4, 1) | ||
targets = Tensor.const([ | ||
y = Tensor.const([ | ||
[0], | ||
[1], | ||
[1], | ||
[0] | ||
]) | ||
|
||
params = HyperParams(lr=RATE, epochs=EPOCHS) | ||
mlp = SequentialModel(params, [ | ||
DenseLayer(2, 4), | ||
DenseLayer(4, 1) | ||
]) | ||
|
||
# Train | ||
losses = mlp.train(inputs, targets) | ||
|
||
# Inference | ||
test_points = [ | ||
(0, 0), | ||
(0, 1), | ||
(1, 0), | ||
(1, 1), | ||
] | ||
|
||
for (x_val, y_val) in test_points: | ||
result = mlp.forward(Tensor.const([[x_val, y_val]]))[0] | ||
print(f"{x_val} XOR {y_val} => {result:.4f}") | ||
|
||
# Plot MSE loss | ||
plt.plot(list(range(0, EPOCHS)), losses) | ||
plt.xlabel('Epochs') | ||
plt.ylabel('MSE Loss') | ||
plt.title('XOR Problem') | ||
plt.show() | ||
], name='y') | ||
|
||
epochs: int = 2000 | ||
|
||
y_hat = model(x) | ||
print(y_hat) | ||
for epoch in range(epochs): | ||
y_hat = model(x) | ||
loss = mse_loss(y_hat, y) | ||
loss.backward() | ||
optim.step() | ||
optim.zero_grad() | ||
if epoch % 1000 == 0: | ||
print(f'Epoch: {epoch}, Loss: {loss.item()}') | ||
|
||
y_hat = model(x) | ||
print(y_hat) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ | |
__url__ = 'https://github.com/MarioSieg/magnetron' | ||
|
||
from .core import * | ||
from .module import * | ||
from .optim import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.