Created
November 15, 2018 07:03
-
-
Save sinkingsugar/96a6e26bad4ffef5e227e0d0ca8ce3a5 to your computer and use it in GitHub Desktop.
iOS-xor
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
import strformat | |
import torch | |
import torch/[nn, optim] | |
let inputs = torch.tensor([ | |
[0.0, 0.0], | |
[0.0, 1.0], | |
[1.0, 0.0], | |
[1.0, 1.0], | |
]) | |
let targets = torch.tensor([ | |
[0.0], | |
[1.0], | |
[1.0], | |
[0.0], | |
]) | |
proc xorTraining() {.exportc.} = | |
let | |
fc1 = nn.Linear(2, 4) | |
fc2 = nn.Linear(4, 1) | |
loss_fn = nn.MSELoss() | |
optimizer = optim.SGD(fc1.parameters & fc2.parameters , lr = 0.01, momentum = 0.1) | |
for i in 0 ..< 50000: | |
optimizer.zero_grad() | |
let predictions = inputs.fc1.relu.fc2.sigmoid | |
let loss = loss_fn(predictions, targets) | |
loss.backward() | |
optimizer.step() | |
if i mod 5000 == 0: | |
echo fmt"Episode {i}, Loss: {loss.toFloat32()}" | |
echo "" | |
echo "Layer 1:" | |
echo fc1.weight | |
echo "" | |
echo "Layer 2:" | |
echo fc2.weight |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment