Last active
September 10, 2024 16:23
-
-
Save devanshuDesai/9f06681d8939afd04f8fab5ac5f5dbf8 to your computer and use it in GitHub Desktop.
Revisions
-
devanshuDesai revised this gist
Dec 1, 2019 . No changes.There are no files selected for viewing
-
devanshuDesai revised this gist
Nov 9, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ import torch from torch import nn from tqdm import tqdm_notebook class CNN(nn.Module): def __init__(self, input_size, num_classes): -
devanshuDesai revised this gist
Nov 9, 2019 . 1 changed file with 39 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ import torch from torch import nn class CNN(nn.Module): def __init__(self, input_size, num_classes): """ @@ -7,7 +10,7 @@ def __init__(self, input_size, num_classes): num_classes: 10 """ super(CNN, self).__init__() self.layer1 = nn.Sequential( nn.Conv2d(input_size[0], 32, kernel_size=5), nn.ReLU(), @@ -19,7 +22,6 @@ def __init__(self, input_size, num_classes): nn.MaxPool2d(kernel_size=2)) self.fc1 = nn.Linear(4 * 4 * 64, num_classes) def forward(self, x): @@ -28,10 +30,42 @@ def forward(self, x): Args: x: (Nx1x28x28) tensor """ x = self.layer1(x) x = self.layer2(x) x = x.reshape(x.size(0), -1) x = self.fc1(x) return x model = CNN((1, 28, 28), 10) opts = { 'lr': 1e-3, 'epochs': 20, 'batch_size': 64 } optimizer = torch.optim.Adam(model.parameters(), opts['lr']) criterion = torch.nn.CrossEntropyLoss() # loss function train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=opts['batch_size'], shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=opts['batch_size'], shuffle=True) for epoch in range(opts['epochs']): train_loss = [] for i, (data, labels) in tqdm_notebook(enumerate(train_loader), total=len(train_loader)): # pass data through network outputs = model(data) loss = criterion(outputs, labels) optimizer.zero_grad() loss.backward() optimizer.step() train_loss.append(loss.item()) test_loss = [] test_accuracy = [] for i, (data, labels) in enumerate(test_loader): # pass data through network outputs = model(data) _, predicted = torch.max(outputs.data, 1) loss = criterion(outputs, labels) test_loss.append(loss.item()) test_accuracy.append((predicted == labels).sum().item() / predicted.size(0)) print('epoch: {}, train loss: {}, test loss: {}, test accuracy: {}'.format(epoch, np.mean(train_loss), np.mean(test_loss), np.mean(test_accuracy))) -
devanshuDesai created this gist
Nov 9, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ class CNN(nn.Module): def __init__(self, input_size, num_classes): """ init convolution and activation layers Args: input_size: (1,28,28) num_classes: 10 """ super(CNN, self).__init__() ### YOUR CODE HERE self.layer1 = nn.Sequential( nn.Conv2d(input_size[0], 32, kernel_size=5), nn.ReLU(), nn.MaxPool2d(kernel_size=2)) self.layer2 = nn.Sequential( nn.Conv2d(32, 64, kernel_size=5), nn.ReLU(), nn.MaxPool2d(kernel_size=2)) self.fc1 = nn.Linear(4 * 4 * 64, num_classes) ### END OF CODE def forward(self, x): """ forward function describes how input tensor is transformed to output tensor Args: x: (Nx1x28x28) tensor """ ### YOUR CODE HERE x = self.layer1(x) x = self.layer2(x) x = x.reshape(x.size(0), -1) x = self.fc1(x) ### END OF CODE return x