Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. import torch
  2. from torch.autograd import Variable
  3. import torch.optim as optim
  4. import torch.nn as nn
  5. import torch.nn.functional as F
  6.  
  7. import torchvision
  8. import torchvision.transforms as transforms
  9. # The output of torchvision datasets are PILImage images of range [0, 1].
  10. # We transform them to Tensors of normalized range [-1, 1]
  11. transform=transforms.Compose([transforms.ToTensor(),
  12.                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
  13.                              ])
  14. trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
  15. trainloader = torch.utils.data.DataLoader(trainset, batch_size=1,
  16.                                           shuffle=True, num_workers=2)
  17.  
  18. testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
  19. testloader = torch.utils.data.DataLoader(testset, batch_size=1,
  20.                                           shuffle=False, num_workers=2)
  21. classes = ('plane', 'car', 'bird', 'cat',
  22.            'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
  23.  
  24.  
  25.  
  26.  
  27.  
  28. class Net(nn.Module):
  29.     def __init__(self):
  30.         super(Net, self).__init__()
  31.  
  32.         self.dconv1 = nn.Conv2d(3, 6, 5)
  33.         self.dpool  = nn.MaxPool2d(2,2)
  34.         self.dconv2 = nn.Conv2d(6, 16, 5)
  35.         self.dfc1   = nn.Linear(16*5*5, 16*6*5*5)
  36.  
  37.         self.conv1 = nn.Conv2d(3, 6, 5)
  38.         self.pool  = nn.MaxPool2d(2,2)
  39.         self.fc1   = nn.Linear(16*5*5, 120)
  40.         self.fc2   = nn.Linear(120, 84)
  41.         self.fc3   = nn.Linear(84, 10)
  42.  
  43.     def forward(self, x):
  44.         y = x.clone()
  45.         x = self.pool(F.relu(self.dconv1(x)))
  46.         x = self.pool(F.relu(self.dconv2(x)))
  47.         x = x.view(-1, 16*5*5)
  48.         x = F.relu(self.dfc1(x))
  49.         x=x.view(16,6,5,5)
  50.  
  51.         conv2 = nn.Conv2d(6,16,5)
  52.         conv2.weight.copy_(x)
  53.         conv2.bias.data.zero_()
  54.  
  55.         y = self.pool(F.relu(self.conv1(y)))
  56.         y = self.pool(F.relu(conv2(y)))
  57.         y = y.view(-1, 16*5*5)
  58.         y = F.relu(self.fc1(y))
  59.         y = F.relu(self.fc2(y))
  60.         y = self.fc3(y)
  61.         return y
  62.  
  63. net = Net()
  64.  
  65.  
  66.  
  67.  
  68. criterion = nn.CrossEntropyLoss() # use a Classification Cross-Entropy loss
  69. optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
  70.  
  71. for epoch in range(2): # loop over the dataset multiple times
  72.  
  73.     running_loss = 0.0
  74.     for i, data in enumerate(trainloader, 0):
  75.         # get the inputs
  76.         inputs, labels = data
  77.  
  78.         # wrap them in Variable
  79.         inputs, labels = Variable(inputs), Variable(labels)
  80.  
  81.         # zero the parameter gradients
  82.         optimizer.zero_grad()
  83.  
  84.         # forward + backward + optimize
  85.         outputs = net(inputs)
  86.         loss = criterion(outputs, labels)
  87.         loss.backward()
  88.         optimizer.step()
  89.  
  90.         # print statistics
  91.         running_loss += loss.data[0]
  92.         if i % 2000 == 1999: # print every 2000 mini-batches
  93.             print('[%d, %5d] loss: %.3f' % (epoch+1, i+1, running_loss / 2000))
  94.             running_loss = 0.0
  95. print('Finished Training')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement