import numpy as np
import torch
import torchvision
import tensorflow as tf
from torch.autograd import Variable
import matplotlib.pyplot as plt
%matplotlib inline
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
a = np.random.rand(2,3)
b = torch.from_numpy(a)
# Q4.1 Display the contents of a, b
print('contents of a:\n', a)
print('contents of b:\n', b)
contents of a:
[[0.52635868 0.07391379 0.49334843]
[0.11214044 0.60364118 0.60633161]]
contents of b:
tensor([[0.5264, 0.0739, 0.4933],
[0.1121, 0.6036, 0.6063]], dtype=torch.float64)
A = torch.rand(2,2)
b = torch.rand(2,1)
x = torch.rand(2,1, requires_grad=True)
y = torch.matmul(A,x) + b
print(y)
z = y.sum()
print(z)
z.backward()
print(x.grad)
print(x)
tensor([[0.8421],
[0.7508]], grad_fn=<AddBackward0>)
tensor(1.5929, grad_fn=<SumBackward0>)
tensor([[0.4243],
[0.4147]])
tensor([[0.9571],
[0.0671]], requires_grad=True)
trainingdata = torchvision.datasets.FashionMNIST('./FashionMNIST/',train=True,download=True,transform=torchvision.transforms.ToTensor())
testdata = torchvision.datasets.FashionMNIST('./FashionMNIST/',train=False,download=True,transform=torchvision.transforms.ToTensor())
# Q4.2 How many training and testing data points are there in the dataset?
# What is the number of features in each data point?
print('Training data points: ', len(trainingdata))
print('Test data points: ', len(testdata))
img = trainingdata.data[0]
img = img.reshape(28, 28)
img = img.flatten()
print('Number of features in each data point: ', len(img))
Training data points: 60000
Test data points: 10000
Number of features in each data point: 784
randnums= np.random.randint(1,60000,15)
# Q4.3 Assuming each sample is an image of size 28x28, show it in matplotlib.
plt.figure(figsize=(10,10))
for i in range(15):
image, label = trainingdata[randnums[i]]
image = image.reshape(28, 28)
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.imshow(image, cmap=plt.cm.binary)
plt.xlabel(label)
plt.show()
trainDataLoader = torch.utils.data.DataLoader(trainingdata, batch_size=64, shuffle=True)
testDataLoader = torch.utils.data.DataLoader(testdata, batch_size=64, shuffle=False)
trainDataIter = iter(trainDataLoader)
images, labels = trainDataIter.next()
print(images.size(), labels)
torch.Size([64, 1, 28, 28]) tensor([9, 5, 6, 9, 9, 9, 4, 8, 2, 2, 4, 8, 9, 3, 4, 6, 1, 9, 5, 3, 8, 1, 4, 6,
7, 6, 0, 0, 6, 6, 9, 2, 8, 3, 9, 6, 2, 8, 2, 6, 2, 6, 4, 0, 1, 7, 2, 7,
8, 9, 0, 7, 0, 4, 2, 8, 4, 7, 6, 6, 7, 2, 8, 7])
# Q4.4 Visualize the first 10 images of the first minibatch
# returned by testDataLoader.
testDataIter = iter(testDataLoader)
test_images, test_labels = testDataIter.next()
plt.figure(figsize=(10,10))
for i in range(10):
image = test_images[i].reshape(28, 28)
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.imshow(image, cmap=plt.cm.binary)
plt.xlabel(test_labels[i])
plt.show()
class LinearReg(torch.nn.Module):
def __init__(self):
super(LinearReg, self).__init__()
self.linear = torch.nn.Linear(28*28,10)
def forward(self, x):
x = x.view(-1,28*28)
transformed_x = self.linear(x)
return transformed_x
net = LinearReg().cuda()
Loss = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.01)
train_loss_history = []
test_loss_history = []
# Q4.5 Write down a for-loop that trains this network for 20 epochs,
# and print the train/test losses.
# Save them in the variables above. If done correctly, you should be able to
# execute the next code block.
num_epochs = 20
for epoch in range(num_epochs):
epoch_train_loss = 0
current_epoch_train_loss = 0
count = 0
for images, labels in trainDataLoader:
# Transfering images and labels to GPU if available
images, labels = images.to(device), labels.to(device)
# Forward pass
outputs = net(images)
loss = Loss(outputs, labels)
# Initializing the gradient as 0 so there is no mixing of gradient among the batches
optimizer.zero_grad()
#Backward propagation of error
loss.backward()
# Optimizing the parameters
optimizer.step()
epoch_train_loss += loss.data
count += 1
current_epoch_train_loss = epoch_train_loss/count
train_loss_history.append(current_epoch_train_loss)
if count % 5 == 0:
print("Epoch: {}, Train Loss: {}".format(epoch, current_epoch_train_loss))
for epoch in range(num_epochs):
epoch_test_loss = 0
current_epoch_test_loss = 0
count = 0
for test_images, test_labels in testDataLoader:
test_images, test_labels = test_images.to(device), test_labels.to(device)
predictions = net(test_images)
epoch_test_loss += Loss(predictions, test_labels)
count += 1
current_epoch_test_loss = epoch_test_loss/count
test_loss_history.append(current_epoch_test_loss)
if count % 5 == 0:
print("Epoch: {}, Train Loss: {}".format(epoch, current_epoch_test_loss))
plt.plot(range(20),train_loss_history,'-',linewidth=3,label='Train error')
plt.plot(range(20),test_loss_history,'-',linewidth=3,label='Test error')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.grid(True)
plt.legend()
predicted_output = net(images)
print(torch.max(predicted_output, 1))
fit = Loss(predicted_output, labels)
print(labels)
torch.return_types.max(
values=tensor([10.1978, 3.9584, 11.4591, 8.3057, 7.0489, 6.6634, 4.6683, 9.9826,
7.5679, 11.9292, 2.8555, 7.8536, 9.4171, 7.0888, 3.1008, 3.3746,
7.4952, 10.6381, 4.1637, 11.1065, 9.4257, 9.5470, 11.9308, 8.7406,
6.8875, 9.3849, 6.3503, 7.1337, 5.5389, 10.6015, 13.0025, 3.5875],
device='cuda:0', grad_fn=<MaxBackward0>),
indices=tensor([9, 5, 9, 4, 3, 4, 5, 8, 3, 8, 1, 0, 1, 8, 5, 0, 0, 7, 6, 0, 0, 8, 9, 2,
7, 8, 7, 9, 8, 1, 1, 3], device='cuda:0'))
tensor([9, 5, 9, 4, 3, 4, 5, 8, 3, 8, 0, 6, 1, 8, 5, 1, 0, 7, 6, 0, 0, 8, 9, 2,
7, 8, 7, 9, 8, 1, 1, 3], device='cuda:0')
def evaluate(dataloader):
# Q4.6 Implement a function here that evaluates training and testing accuracy.
# Here, accuracy is measured by probability of successful classification.
accuracy = 0
for images, labels in dataloader:
images, labels = images.to(device), labels.to(device)
predictions = net(images)
predicted_labels = predictions.max(1).indices
accuracy += (predicted_labels == labels).sum(dtype=torch.float32)
return accuracy/len(dataloader.dataset)
print('Train acc = %0.2f, test acc = %0.2f' % (evaluate(trainDataLoader), evaluate(testDataLoader)))
Train acc = 0.85, test acc = 0.83