Last active
April 20, 2018 11:01
-
-
Save conormm/ed372ebceceacafdfbd9cf3ee2f7638b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from sklearn.datasets import make_moons | |
import torch as tr | |
from torch.autograd import Variable | |
import torch.nn as nn | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from keras.utils import np_utils | |
%matplotlib inline | |
X, y_ = make_moons(n_samples=1000, noise=.1) | |
y = np_utils.to_categorical(y_) | |
#plt.scatter(X[:, 0], X[:, 1], c=y_, alpha=.4) | |
def torch_acc(preds, true_y): | |
"Computes accuracy - presumes inputs are already torch.Tensors/Vars" | |
n = preds.size()[0] | |
acc = np.round(( | |
tr.eq(preds.max(1)[1], true_y).sum().data[0] / n), 3) | |
return acc | |
class MoonsModel(nn.Module): | |
def __init__(self): | |
super(MoonsModel, self).__init__() | |
self.hidden_1 = nn.Linear(in_features=2, out_features=50) | |
self.relu_h1 = nn.Tanh() | |
self.dropout_05 = nn.Dropout(p=0.5) | |
self.out_layer = nn.Linear(in_features=50, out_features=2) | |
self.sig = nn.Sigmoid() | |
def forward(self, X): | |
out = self.hidden_1(X) | |
out = self.relu_h1(out) | |
out = self.dropout_05(out) | |
out = self.out_layer(out) | |
out = self.sig(out) | |
return out | |
m = MoonsModel() | |
cost_func = nn.BCELoss() | |
optimizer = tr.optim.Adam(params=m.parameters(), lr=0.05) | |
num_epochs = 500 | |
for e in range(num_epochs): | |
#========torchify inputs/target============================ | |
X_ = Variable(tr.from_numpy(X), requires_grad=False).float() | |
y_ = Variable(tr.from_numpy(y), requires_grad=False).float() | |
#========forward pass===================================== | |
yhat = m(X_) | |
loss = cost_func(yhat, y_) # loss is probabilty that predicted==1 | |
acc = tr.eq(yhat.round(), y_).float().mean() | |
#=======backward pass===================================== | |
optimizer.zero_grad() | |
loss.backward() | |
optimizer.step() | |
if e % 50 == 0: | |
print("[{}/{}], loss: {} acc: {}".format(e, | |
num_epochs, np.round(loss.data[0], 3), np.round(acc.data[0], 3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[0/500], loss: 0.227 acc: 0.9
[50/500], loss: 0.171 acc: 0.928
[100/500], loss: 0.128 acc: 0.942
[150/500], loss: 0.135 acc: 0.947
[200/500], loss: 0.125 acc: 0.947
[250/500], loss: 0.126 acc: 0.95
[300/500], loss: 0.161 acc: 0.939
[350/500], loss: 0.132 acc: 0.95
[400/500], loss: 0.133 acc: 0.944
[450/500], loss: 0.136 acc: 0.941