From 530bcae7e8b3916286e8e0498a1cbbacae75b470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joseph=20Hopfm=C3=BCller?= Date: Mon, 17 Oct 2022 17:01:17 +0200 Subject: [PATCH] finish chapter 13 --- 11_01_softmax.py | 7 +++- 13_feedforward.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 13_feedforward.py diff --git a/11_01_softmax.py b/11_01_softmax.py index 42b2cb7..c426d75 100644 --- a/11_01_softmax.py +++ b/11_01_softmax.py @@ -14,4 +14,9 @@ print('softmax numpy:', outputs) x = torch.tensor([2., 1., .1]) outputs = torch.softmax(x, dim=0) print('inputs: ', x) -print('softmax numpy:', outputs) \ No newline at end of file +print('softmax numpy:', outputs) + +outputs2 = torch.softmax(outputs, dim=0) +print('inputs: ', outputs) +print('softmax numpy:', outputs2) + diff --git a/13_feedforward.py b/13_feedforward.py new file mode 100644 index 0000000..eb5d580 --- /dev/null +++ b/13_feedforward.py @@ -0,0 +1,105 @@ +''' +put the last few chapters together to classify digits from MNIST dataset +-> MNIST +-> DataLoader, Transformation +-> Loss + Optimizer +-> Training Loop with batch training +-> Model Evaluation +-> GPU Support +''' + +import torch +import torch.nn as nn +import torchvision +import torchvision.transforms as transforms +import matplotlib.pyplot as plt + +# device config +device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') +print(f'Device is {device}') + +# hyper parameters +input_size = 784 # 28x28 pixel images +hidden_size = 100 # PLAY WITH THIS +num_classes = 10 # digits 0..9 +num_epochs = 2 # PLAY WITH THIS +batch_size = 100 +learning_rate = .001 + +# MNIST +train_dataset = torchvision.datasets.MNIST(root='./data', train=True, + download=True, transform=transforms.ToTensor()) + +test_dataset = torchvision.datasets.MNIST(root='./data', train=False, + transform=transforms.ToTensor()) + +train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, + shuffle=True) + +test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size) + +examples = iter(train_loader) +samples, labels = examples.next() +print(samples.shape, labels.shape) + +for i in range(6): + plt.subplot(2, 3, i+1) + plt.imshow(samples[i][0], cmap='gray') +# plt.show() + +class NeuralNet(nn.Module): + def __init__(self, input_size, hidden_size, num_classes): + super(NeuralNet, self).__init__() + self.layers = nn.Sequential( + nn.Linear(input_size, hidden_size), + nn.ReLU(), + nn.Linear(hidden_size, num_classes) + # no softmax because its included in the CE loss function + ) + + def forward(self, x): + return self.layers(x) + +model = NeuralNet(input_size, hidden_size, num_classes).to(device) +print(model) + +# loss and optimizer +criterion = nn.CrossEntropyLoss() +optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) + +# training loop +num_total_steps = len(train_loader) +for epoch in range(num_epochs): + for batch, (images, labels) in enumerate(train_loader): + # reshape 100, 1, 28, 28 -> 100, 784 + images = images.reshape(-1, 28*28).to(device) # reshape and send to gpu if available + labels = labels.to(device) + + # forward + outputs = model(images) + loss = criterion(outputs, labels) + + # backward + update + optimizer.zero_grad() + loss.backward() + optimizer.step() + + if (batch+1) % 100 == 0: + print(f'Epoch {epoch+1}/{num_epochs}, step {batch+1}/{num_total_steps}, loss = {loss.item():.4f}') + +# test +with torch.no_grad(): + n_correct = 0 + n_samples = 0 + for images, labels in test_loader: + images = images.reshape(-1, 28*28).to(device) + labels = labels.to(device) + outputs = model(images) + + # value, index (index is class label) + _, predictions = torch.max(outputs, 1) + n_samples += labels.shape[0] + n_correct += (predictions==labels).sum().item() + + acc = 100.*n_correct/n_samples + print(f'Accuracy = {acc}%') \ No newline at end of file