Skip to content

Instantly share code, notes, and snippets.

@user01
Created April 1, 2017 01:53
Show Gist options
  • Save user01/68514db1127eb007f24d28bfd11dd60e to your computer and use it in GitHub Desktop.
Save user01/68514db1127eb007f24d28bfd11dd60e to your computer and use it in GitHub Desktop.
Toy XOR network with pyTorch
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
EPOCHS_TO_TRAIN = 50000
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(2, 3, True)
self.fc2 = nn.Linear(3, 1, True)
def forward(self, x):
x = F.sigmoid(self.fc1(x))
x = self.fc2(x)
return x
net = Net()
inputs = list(map(lambda s: Variable(torch.Tensor([s])), [
[0, 0],
[0, 1],
[1, 0],
[1, 1]
]))
targets = list(map(lambda s: Variable(torch.Tensor([s])), [
[0],
[1],
[1],
[0]
]))
criterion = nn.MSELoss()
optimizer = optim.SGD(net.parameters(), lr=0.01)
print("Training loop:")
for idx in range(0, EPOCHS_TO_TRAIN):
for input, target in zip(inputs, targets):
optimizer.zero_grad() # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step() # Does the update
if idx % 5000 == 0:
print("Epoch {: >8} Loss: {}".format(idx, loss.data.numpy()[0]))
print("")
print("Final results:")
for input, target in zip(inputs, targets):
output = net(input)
print("Input:[{},{}] Target:[{}] Predicted:[{}] Error:[{}]".format(
int(input.data.numpy()[0][0]),
int(input.data.numpy()[0][1]),
int(target.data.numpy()[0]),
round(float(output.data.numpy()[0]), 4),
round(float(abs(target.data.numpy()[0] - output.data.numpy()[0])), 4)
))
@RealPixelRobot
Copy link

Noticed an error in line 48:
IndexError: too many indices for array
Just change loss.data.numpy()[0] to loss.data.numpy()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment