.. _110PerceptronIrisrst: ================================== 110 - First percepton with pytorch ================================== .. only:: html **Links:** :download:`notebook <110_Perceptron_Iris.ipynb>`, :downloadlink:`html <110_Perceptron_Iris2html.html>`, :download:`PDF <110_Perceptron_Iris.pdf>`, :download:`python <110_Perceptron_Iris.py>`, :downloadlink:`slides <110_Perceptron_Iris.slides.html>`, :githublink:`GitHub|_doc/notebooks/101/110_Perceptron_Iris.ipynb|*` Implement the forward (prediction) and backward (training) algorithm with `pytorch `__. **Note:** install `tqdm `__ if not installed: ``!pip install tqdm`` .. code:: ipython3 import time import numpy as np import pandas as pd import matplotlib.pyplot as plt import torch import torch.nn as nn import torch as F import torch.optim as optim from torch.autograd import Variable print("torch", torch.__version__) from torchvision import datasets, transforms from tqdm import tqdm from sklearn.datasets import load_iris from sklearn.preprocessing import LabelBinarizer .. parsed-literal:: torch 1.5.0+cpu .. code:: ipython3 %matplotlib inline .. code:: ipython3 X, Y = load_iris(return_X_y=True) X = X.astype("float32") X.shape, Y.shape .. parsed-literal:: ((150, 4), (150,)) .. code:: ipython3 ftrain = np.arange(X.shape[0]) % 4 != 0 Xtrain, Ytrain = X[ftrain, :], Y[ftrain] Xtest, Ytest = X[~ftrain, :], Y[~ftrain] Xtrain.shape, Ytrain.shape, Xtest.shape, Ytest.shape .. parsed-literal:: ((112, 4), (112,), (38, 4), (38,)) .. code:: ipython3 BATCH_SIZE = 64 TEST_BATCH_SIZE = 64 N_EPOCHS = 1000 .. code:: ipython3 class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.fc1 = nn.Linear(4, 20) self.fc2 = nn.Linear(20, 3) def forward(self, x): x = F.tanh(self.fc1(x)) x = self.fc2(x) return F.log_softmax(x, dim=-1) .. code:: ipython3 model = Net() #optimizer = optim.SGD(model.parameters(), lr=1e-1, momentum=0.8) optimizer = optim.Adam(model.parameters()) loss_fn = nn.NLLLoss() .. code:: ipython3 Xtrain_ = Variable(torch.from_numpy(Xtrain)) Xtest_ = Variable(torch.from_numpy(Xtest)) Ytrain_ = Variable(torch.from_numpy(Ytrain.astype(np.int64))) Ytest_ = Variable(torch.from_numpy(Ytest.astype(np.int64))) perfs = [] for t in range(1, N_EPOCHS + 1): # Before the backward pass, use the optimizer object to zero all of the # gradients for the variables it will update (which are the learnable weights # of the model) optimizer.zero_grad() # Forward pass: compute predicted y by passing x to the model. Ypred = model(Xtrain_) # Compute and print loss. loss = loss_fn(Ypred , Ytrain_) # Backward pass: compute gradient of the loss with respect to model # parameters loss.backward() # Calling the step function on an Optimizer makes an update to its # parameters optimizer.step() Ypred_test = model(Xtest_) loss_test = loss_fn(Ypred_test, Ytest_) pred = Ypred_test.data.max(1, keepdim=True)[1] # get the index of the max log-probability accuracy = pred.eq(Ytest_.data.view_as(pred)).cpu().sum().item() / Ytest.size perfs.append([t, loss.item(), loss_test.data.item(), accuracy]) .. code:: ipython3 df_perfs = pd.DataFrame(perfs, columns=["epoch", "train_loss", "test_loss", "accuracy"]).set_index("epoch") f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4)) print("Last accuracy %.3f" % df_perfs.accuracy.iloc[-1]) print("Best accuracy %.3f" % df_perfs.accuracy.max()) print("Last test loss %.4f" % df_perfs.test_loss.iloc[-1]) df_perfs[["train_loss", "test_loss"]].plot(ax=ax1); df_perfs[["accuracy"]].plot(ax=ax2); plt.ylim(ymin=0.7); .. parsed-literal:: Last accuracy 0.974 Best accuracy 0.974 Last test loss 0.0548 .. image:: 110_Perceptron_Iris_10_1.png