Skip to content

Instantly share code, notes, and snippets.

@sprintr
Last active April 7, 2020 00:14
Show Gist options
  • Save sprintr/57cda923493fb91e01f2bfe94364ab21 to your computer and use it in GitHub Desktop.
Save sprintr/57cda923493fb91e01f2bfe94364ab21 to your computer and use it in GitHub Desktop.
PyTorch
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.optim as optim
x_train = torch.Tensor([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
y_train = torch.Tensor([
[0],
[1],
[1],
[1]
])
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__();
self.fc1 = nn.Linear(in_features=2, out_features=3)
self.fc2 = nn.Linear(in_features=3, out_features=1)
def forward(self, x):
out = torch.sigmoid(self.fc1(x))
out = torch.sigmoid(self.fc2(out))
return out
model = Net()
criterion = nn.BCELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
for epoch in range(5000):
optimizer.zero_grad()
out = model(x_train)
loss = criterion(out, y_train)
loss.backward()
optimizer.step()
if epoch % 1000 == 0:
print("epoch={}, loss={}".format(epoch, loss.data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment