In [42]:
import torch
from torch.autograd import Variable
import torch.nn as nn
torch.manual_seed(777) # reproducible
Out[42]:
In [43]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
In [44]:
# make fake data
n_data = torch.ones(100, 2)
x0 = torch.normal(2*n_data, 1) # class0 x data (tensor), shape=(100, 2)
y0 = torch.zeros(100,1) # class0 y data (tensor), shape=(100, 1)
x1 = torch.normal(-2*n_data, 1) # class1 x data (tensor), shape=(100, 2)
y1 = torch.ones(100,1) # class1 y data (tensor), shape=(100, 1)
x = torch.cat((x0, x1), 0).type(torch.FloatTensor) # shape (200, 2) FloatTensor = 32-bit floating
y = torch.cat((y0, y1), 0).type(torch.FloatTensor) # shape (200, 1) FloatTensor = 32-bit integer
# torch can only train on Variable, so convert them to Variable
x, y = Variable(x), Variable(y)
plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=y.data.numpy(), s=100, lw=0, cmap='RdYlGn')
plt.show()
torch.cat( (x, y) ,1)
Out[44]:
In [55]:
# Hypothesis using sigmoid and linear model
linear = nn.Linear(2, 1, bias=True)
sigmoid = nn.Sigmoid()
model = nn.Sequential(linear, sigmoid)
In [56]:
model
Out[56]:
In [46]:
# Loss and Optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
#cost_fn = -(y * torch.log(prob) + (1 - y)* torch.log(1 - prob) ).mean()
cost_fn = nn.BCELoss() #Binary Cross Entropy Costfunction
In [47]:
plt.ion() # something about plotting
for t in range(120):
prob = model(x) # input x and predict based on x
cost = cost_fn(prob, y)
optimizer.zero_grad() # clear gradients for next train
cost.backward() # compute gradients
optimizer.step() # apply gradients
# ----------------------------------------------------------------------#
# Ploting
if t % 10 == 0 or t in [3, 6]:
# plot and show learning process
plt.cla()
prediction = prob.gt(0.5)
pred_y = prediction.data.numpy().squeeze()
target_y = y.data.squeeze(1).numpy()
# Draw massgrid
x_min, x_max = x[:, 0].min() - 1, x[:, 0].max() + 1
y_min, y_max = x[:, 1].min() - 1, x[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min.data.numpy(), x_max.data.numpy(), 1),
np.arange(y_min.data.numpy(), y_max.data.numpy(), 1))
# here "model" is your model's prediction (classification) function
Z = model(Variable(torch.from_numpy(np.c_[xx.ravel(), yy.ravel()])).float())
# Put the result into a color plot
Z = Z.view(xx.shape)
plt.contourf(xx, yy, Z.data.numpy(), cmap=plt.cm.binary)
#plt.axis('off')
# Plot also the training points
#plt.scatter(x[:, 0].data.numpy(), x[:, 1].data.numpy(), c=pred_y, cmap='RdYlGn')
plt.scatter(x.data.numpy()[:, 0], x.data.numpy()[:, 1], c=pred_y, s=100, lw=0, cmap='RdYlGn')
accuracy = sum(pred_y == target_y)/200.
plt.text(1.5, -4, 'Accuracy=%.2f' % accuracy, fontdict={'size': 20, 'color': 'Blue'})
plt.show()
plt.pause(0.1)
plt.ioff()
In [70]:
torch.manual_seed(777) # for reproducibility
nb_classes = 3
x_data = [[1, 2, 1, 1], [2, 1, 3, 2], [3, 1, 3, 4], [4, 1, 5, 5],
[1, 7, 5, 5], [1, 2, 5, 6], [1, 6, 6, 6], [1, 7, 7, 7]]
y_data = [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0],
[0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]]
X = Variable(torch.Tensor(x_data))
Y = Variable(torch.Tensor(y_data))
_, Y_label = Y.max(dim=1)
print(Y_label)
In [88]:
# Define our model
linear = torch.nn.Linear(4, nb_classes, bias=True)
softmax = torch.nn.Softmax() # softmax = exp(logits) / reduce_sum(exp(logits), dim)
model = torch.nn.Sequential(linear, softmax)
In [89]:
model
Out[89]:
In [103]:
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
# cost_fn = - Y * torch.log(prediction) # Cross entropy cost
# cost_fn = torch.sum(cost, 1).mean()
cost_fn = nn.CrossEntropyLoss() #Cross Entropy Costfunction
In [104]:
prediction
Y_label
Out[104]:
In [110]:
for step in range(4001):
prediction = model(X)
cost = cost_fn(prediction, Y_label)
optimizer.zero_grad()
cost.backward() # Compute Gradient of Cost function
optimizer.step()
if step % 200 == 0:
print(step, cost.data.numpy())
In [111]:
# Testing & One-hot encoding
print('--------------')
a = model(Variable(torch.Tensor([[1, 11, 7, 9]])))
print(a.data.numpy(), torch.max(a, 1)[1].data.numpy())
print('--------------')
b = model(Variable(torch.Tensor([[1, 3, 4, 3]])))
print(b.data.numpy(), torch.max(b, 1)[1].data.numpy())
print('--------------')
c = model(Variable(torch.Tensor([[1, 1, 0, 1]])))
print(c.data.numpy(), torch.max(c, 1)[1].data.numpy())
print('--------------')
all = model(Variable(torch.Tensor([[1, 11, 7, 9], [1, 3, 4, 3], [1, 1, 0, 1]])))
print(all.data.numpy(), torch.max(all, 1)[1].data.numpy())
In [ ]: