#!/usr/bin/env python # coding: utf-8 # In[1]: import torch import torch.nn as nn import torch.nn.functional as F import matplotlib.pyplot as plt # for plottingp torch.manual_seed(1) # set the random seed # Fully-connected Neural Network class Pigeon(nn.Module): def __init__(self): super(Pigeon, self).__init__() self.layer1 = nn.Linear(28 * 28, 30) self.layer2 = nn.Linear(30, 1) def forward(self, img): flattened = img.view(-1, 28 * 28) activation1 = self.layer1(flattened) activation1 = F.relu(activation1) activation2 = self.layer2(activation1) return activation2 pigeon = Pigeon() # In[13]: for w in pigeon.layer2.parameters(): print(w) # In[14]: # Download the MNIST dataset from torchvision import datasets, transforms mnist_train = datasets.MNIST('data', train=True, download=True) # In[15]: # Use just the first 2000 data points today: mnist_train = list(mnist_train)[:2000] # In[16]: len(mnist_train) # In[18]: # Look at a few data points: mnist_train[0][0] # In[19]: mnist_train[0] # In[20]: mnist_train[1][0] # In[21]: mnist_train[1] # In[22]: # plot the first 18 images in the training data for k, (image, label) in enumerate(mnist_train[:18]): plt.subplot(3, 6, k+1) plt.imshow(image) # In[23]: pigeon # In[24]: image # In[25]: type(image) # In[26]: img_to_tensor = transforms.ToTensor() # In[27]: inval = img_to_tensor(image) # In[29]: inval.shape # In[32]: logit = pigeon(inval) # In[33]: torch.sigmoid(logit) # In[40]: for image, label in mnist_train[:2]: logit = pigeon(img_to_tensor(image)) prob = torch.sigmoid(logit) print(float(prob)) # In[41]: # We use choose "loss function" to # compare the ground truth vs. the prediction criterion = nn.BCEWithLogitsLoss() # In[43]: for image, label in mnist_train[:2]: logit = pigeon(img_to_tensor(image)) prob = torch.sigmoid(logit) actual = (label < 3).type(torch.FloatTensor).reshape([1,1]) loss = criterion(logit, actual) print(float(loss)) # In[45]: # optimizer computes the changes to the weights & biases # required to take a step towards reducing the loss import torch.optim as optim optimizer = optim.SGD(pigeon.parameters(), lr=0.005, momentum=0.9) # In[46]: for image, label in mnist_train[:1000]: logit = pigeon(img_to_tensor(image)) prob = torch.sigmoid(logit) actual = (label < 3).type(torch.FloatTensor).reshape([1,1]) loss = criterion(logit, actual) loss.backward() # computes, for each weight/bias, the change # to be made for that weight/bias optimizer.step() # actually updates the weights/biases optimizer.zero_grad() # does some cleanup # In[47]: image # In[50]: logit = pigeon(img_to_tensor(image)) prob = torch.sigmoid(logit) print(prob) # In[51]: image, label = mnist_train[3] # In[52]: image # In[53]: logit = pigeon(img_to_tensor(image)) prob = torch.sigmoid(logit) print(prob) # In[54]: error = 0 for (image, label) in mnist_train[:1000]: #<--- 1000 images prob = torch.sigmoid(pigeon(img_to_tensor(image))) if (prob < 0.5 and label < 3) or (prob >= 0.5 and label >= 3): error += 1 print("Training Error Rate:", error/1000) print("Training Accuracy:", 1 - error/1000) # In[55]: error = 0 for (image, label) in mnist_train[1000:2000]: prob = torch.sigmoid(pigeon(img_to_tensor(image))) if (prob < 0.5 and label < 3) or (prob >= 0.5 and label >= 3): error += 1 print("Test Error Rate:", error/1000) print("Test Accuracy:", 1 - error/1000) # In[60]: pigeon2 = Pigeon() optim2 = optim.SGD(pigeon2.parameters(), lr=0.005, momentum=0.9) # training using 10 images for k in range(1): for image, label in mnist_train[:10]: # <--- logit = pigeon2(img_to_tensor(image)) prob = torch.sigmoid(logit) actual = (label < 3).type(torch.FloatTensor).reshape([1,1]) loss = criterion(logit, actual) loss.backward() # computes, for each weight/bias, the change # to be made for that weight/bias optim2.step() # actually updates the weights/biases optim2.zero_grad() # does some cleanup # In[61]: error = 0 for (image, label) in mnist_train[:10]: prob = torch.sigmoid(pigeon2(img_to_tensor(image))) if (prob < 0.5 and label < 3) or (prob >= 0.5 and label >= 3): error += 1 print("Training Error Rate:", error/10) print("Training Accuracy:", 1 - error/10) # In[62]: error = 0 for (image, label) in mnist_train[1000:2000]: prob = torch.sigmoid(pigeon2(img_to_tensor(image))) if (prob < 0.5 and label < 3) or (prob >= 0.5 and label >= 3): error += 1 print("Test Error Rate:", error/1000) print("Test Accuracy:", 1 - error/1000) # In[ ]: