Jeremiah_

my_model_fc

Sep 12th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. import torch
  2. from torch import nn
  3. import torch.nn.functional as F
  4.  
  5. class Network(nn.Module):
  6.     def __init__(self, input_size, output_size, hidden_layers, drop_p = 0.2):
  7.         super().__init__()
  8.  
  9.         self.hidden_layers = nn.ModuleList([nn.Linear(input_size, hidden_layers[0])])
  10.  
  11.         self.layers_size = zip(hidden_layers[:-1], hidden_layers[1:])
  12.  
  13.         self.hidden_layers.extend([nn.Linear(h1, h2) for h1, h2 in self.layers_size])
  14.  
  15.         self.output = nn.Linear(hidden_layers[-1], output_size)
  16.  
  17.         self.dropout = nn.Dropout(p = drop_p)
  18.  
  19.  
  20.     def forward(self, x):
  21.         x = x.view(x.shape[0], -1)
  22.  
  23.         for each in self.hidden_layers:
  24.             x = each(x)
  25.             x = F.relu(x)
  26.             x = self.dropout(x)
  27.  
  28.         x = self.output(x)
  29.  
  30.         return F.log_softmax(x, dim=1)
  31.  
  32. def validation(model, testloader, criterion):
  33.  
  34.     accuracy = 0
  35.     test_loss = 0
  36.  
  37.     for images, labels in testloader:
  38.         log_ps = model.forward(images)
  39.         test_loss += criterion(log_ps, labels)
  40.         ps = torch.exp(log_ps)
  41.  
  42.         top_ps, top_class = ps.topk(1, dim=1)
  43.  
  44.         equals  = top_class == labels.view(*top_class.shape)
  45.  
  46.         accuracy += torch.mean(equals.type(torch.FloatTensor))
  47.    
  48.     return test_loss, accuracy
  49.  
  50.    
  51. def train(model, trainloader, testloader, criterion, optimizer, epochs=2):
  52.  
  53.     train_losses, test_losses = [], []
  54.     for e in range (epochs):
  55.         train_loss = 0
  56.         for images, labels in trainloader:
  57.  
  58.             optimizer.zero_grad()
  59.            
  60.             log_ps = model.forward(images)
  61.             loss = criterion(log_ps, labels)
  62.             loss.backward()
  63.             optimizer.step()
  64.  
  65.             train_loss += loss.item()
  66.         else:
  67.             with torch.no_grad():
  68.                 model.eval()
  69.                
  70.                 test_loss, accuracy = validation(model, testloader, criterion)
  71.                
  72.             model.train()
  73.  
  74.             train_losses.append(train_loss/len(trainloader))
  75.             test_losses.append(test_loss/len(testloader))
  76.  
  77.             print(f'Epoch: {e+1}/{epochs}')
  78.             print('Training Loss: {:.3f}'.format(train_loss/len(trainloader)))
  79.             print('Test Loss: {:.3f}'.format(test_loss/len(testloader)))
  80.             print('Accuracy: {:.3f}'.format(accuracy/len(testloader)))
  81.             print()
Advertisement
Add Comment
Please, Sign In to add comment