cifar10-多GPU-pytorch0.3
#version1
import torch import torchvision import torchvision.transforms as transforms transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))]) trainset = torchvision.datasets.CIFAR10(root='../../data',train=True,download=False,transform=transform) testset = torchvision.datasets.CIFAR10(root='../../data',train=False,download=False,transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=128,shuffle=True,num_workers=2) testloader = torch.utils.data.DataLoader(testset, batch_size=128,shuffle=False,num_workers=2) classes = ('plane','car','bird','deer','dog','frog','horse','ship','truck') print type(classes), trainloader, testloader import sys dataiter = iter(trainloader) images, labels = dataiter.next() print images.shape # 2. Define a Convolution Nerual Network import torch.nn as nn import torch.nn.functional as F class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(3,6,5) self.pool = nn.MaxPool2d(2,2) self.conv2 = nn.Conv2d(6,16,5) self.fc1 = nn.Linear(16 * 5 * 5,120) self.fc2 = nn.Linear(120,84) self.fc3 = nn.Linear(84,10) def forward(self,x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1,16*5*5) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x net = Net() device_ids = [2,3] net = nn.DataParallel(net, device_ids=device_ids) net = net.cuda(device_ids[0]) import torch.optim as optim from torch.autograd import Variable criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9,weight_decay=1e-3) for epoch in range(100): running_loss = 0.0 for i,data in enumerate(trainloader,0): inputs,labels=data inputs = Variable(inputs,requires_grad=True).cuda(device_ids[0]) labels = Variable(labels).cuda(device_ids[0]) optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.data[0] if i%100==0: print('[%d,%5d] loss: %.3f' % (epoch+1, i+1,running_loss/2000)) running_loss = 0.0 correct = 0 total = 0 for data in testloader: images, labels = data outputs = net(Variable(images).cuda(device_ids[0])) _, predicted = torch.max(outputs.data,1) total += labels.size(0) correct += (predicted == labels.cuda(device_ids[0])).sum() print('Accuracy of the network on the 10000 test images: %d %%' % ( 100 * correct / total))
#version2
#!/usr/bin/env python # -*- coding: utf-8 -*- ''''''''''''''''''''''''''''''''' # @Time : 2018/4/15 16:51 # @Author : Awiny # @Site : # @File : cifar10.py # @Software: PyCharm # @Github : https://github.com/FingerRec # @Blog : http://fingerrec.github.io ''''''''''''''''''''''''''''''''' import scipy.io import os import torch import torchvision import torchvision.transforms as transforms import numpy as np import matplotlib.pyplot as plt #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #close the warning #---------------------------------------------------download and load dataset--------------------------------- #正则化 transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) #均值,标准差 trainset = torchvision.datasets.CIFAR10(root='../../data', train=True, download=True, transform=transform) #The output of torchvision datasets are PILImage images of range [0, 1]. #We transform them to Tensors of normalized range [-1, 1]. trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2) testset = torchvision.datasets.CIFAR10(root='../../data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') #---------------------------------------------------functions to show an image---------------------------- # get some random training images dataiter = iter(trainloader) images, labels = dataiter.next() #----------------------------------------------------define an convolutional neural network--------------------- from torch.autograd import Variable import torch.nn as nn import torch.nn.functional as F class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): y = x x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1, 16 * 5 * 5) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x net = Net() #net.cuda() #--------------------------------------------------Define a Loss function and optimizer------------------------------ import torch.optim as optim criterion = nn.CrossEntropyLoss() #交叉熵 optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) #-------------------------------------------------Training on GPU------------------------------------- #you transfer the neural net onto the GPU. This will recursively go over all modules and convert their parameters and buffers to CUDA tensors: #net.cuda() #have to send the inputs and targets at every step to the GPU too: #inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda()) #----------------------------------------------------Training on Multiple GPU------------------- if torch.cuda.device_count() > 1: print("Let's use", torch.cuda.device_count(), "GPUs!") # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs net = nn.DataParallel(net) if torch.cuda.is_available(): net.cuda() #pytorch中CrossEntropyLoss是通过两个步骤计算出来的,第一步是计算log softmax,第二步是计算cross entropy(或者说是negative log likehood) #---------------------------------------------------Training the network------------------------------------------------ for epoch in range(2): # loop over the dataset multiple times # 0, 1 running_loss = 0.0 for i, data in enumerate(trainloader, 0): # get the inputs inputs, labels = data # wrap them in Variable inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda()) # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = net(inputs) # forward loss = criterion(outputs, labels) loss.backward() # backward optimizer.step() # print statistics #print("Outside: input size", images.size(), "output_size", outputs.size()) running_loss += loss.data[0] if i % 10 == 0: # print every 2000 mini-batches print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000)) running_loss = 0.0 print('Finished Training')
爱上一个人,就像突然有了盔甲,也突然有了软肋。

浙公网安备 33010602011771号