Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
deeplearningzerotoall
GitHub Repository: deeplearningzerotoall/PyTorch
Path: blob/master/CNN/lab-10-4-mnist_nn_deep.ipynb
618 views
Kernel: Python 3
# Lab 10 MNIST and softmax import torch import torchvision.datasets as dsets import torchvision.transforms as transforms import random
device = 'cuda' if torch.cuda.is_available() else 'cpu' # for reproducibility random.seed(777) torch.manual_seed(777) if device == 'cuda': torch.cuda.manual_seed_all(777)
# parameters learning_rate = 0.001 training_epochs = 15 batch_size = 100
# MNIST dataset mnist_train = dsets.MNIST(root='MNIST_data/', train=True, transform=transforms.ToTensor(), download=True) mnist_test = dsets.MNIST(root='MNIST_data/', train=False, transform=transforms.ToTensor(), download=True)
# dataset loader data_loader = torch.utils.data.DataLoader(dataset=mnist_train, batch_size=batch_size, shuffle=True, drop_last=True)
# nn layers linear1 = torch.nn.Linear(784, 512, bias=True) linear2 = torch.nn.Linear(512, 512, bias=True) linear3 = torch.nn.Linear(512, 512, bias=True) linear4 = torch.nn.Linear(512, 512, bias=True) linear5 = torch.nn.Linear(512, 10, bias=True) relu = torch.nn.ReLU()
# xavier initialization torch.nn.init.xavier_uniform_(linear1.weight) torch.nn.init.xavier_uniform_(linear2.weight) torch.nn.init.xavier_uniform_(linear3.weight) torch.nn.init.xavier_uniform_(linear4.weight) torch.nn.init.xavier_uniform_(linear5.weight)
Parameter containing: tensor([[-0.0565, 0.0423, -0.0155, ..., 0.1012, 0.0459, -0.0191], [ 0.0772, 0.0452, -0.0638, ..., 0.0476, -0.0638, 0.0528], [ 0.0311, -0.1023, -0.0701, ..., 0.0412, -0.1004, 0.0738], ..., [ 0.0334, 0.0187, -0.1021, ..., 0.0280, -0.0583, -0.1018], [-0.0506, -0.0939, -0.0467, ..., -0.0554, -0.0325, 0.0640], [-0.0183, -0.0123, 0.1025, ..., -0.0214, 0.0220, -0.0741]], requires_grad=True)
# model model = torch.nn.Sequential(linear1, relu, linear2, relu, linear3).to(device)
# define cost/loss & optimizer criterion = torch.nn.CrossEntropyLoss().to(device) # Softmax is internally computed. optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
total_batch = len(data_loader) for epoch in range(training_epochs): avg_cost = 0 for X, Y in data_loader: # reshape input image into [batch_size by 784] # label is not one-hot encoded X = X.view(-1, 28 * 28).to(device) Y = Y.to(device) optimizer.zero_grad() hypothesis = model(X) cost = criterion(hypothesis, Y) cost.backward() optimizer.step() avg_cost += cost / total_batch print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost)) print('Learning finished')
Epoch: 0001 cost = 0.283860594 Epoch: 0002 cost = 0.089265838 Epoch: 0003 cost = 0.056718789 Epoch: 0004 cost = 0.041850876 Epoch: 0005 cost = 0.030926639 Epoch: 0006 cost = 0.024389934 Epoch: 0007 cost = 0.021937676 Epoch: 0008 cost = 0.019161038 Epoch: 0009 cost = 0.016852187 Epoch: 0010 cost = 0.014415207 Epoch: 0011 cost = 0.013022121 Epoch: 0012 cost = 0.010289547 Epoch: 0013 cost = 0.015175694 Epoch: 0014 cost = 0.008412631 Epoch: 0015 cost = 0.008151450 Learning finished
# Test the model using test sets with torch.no_grad(): X_test = mnist_test.test_data.view(-1, 28 * 28).float().to(device) Y_test = mnist_test.test_labels.to(device) prediction = model(X_test) correct_prediction = torch.argmax(prediction, 1) == Y_test accuracy = correct_prediction.float().mean() print('Accuracy:', accuracy.item()) # Get one and predict r = random.randint(0, len(mnist_test) - 1) X_single_data = mnist_test.test_data[r:r + 1].view(-1, 28 * 28).float().to(device) Y_single_data = mnist_test.test_labels[r:r + 1].to(device) print('Label: ', Y_single_data.item()) single_prediction = model(X_single_data) print('Prediction: ', torch.argmax(single_prediction, 1).item())
Accuracy: 0.9818999767303467 Label: 8 Prediction: 8