張量操作
# 引包
import torch
# 創(chuàng)建張量
torch.empty(5, 3) # 創(chuàng)建5x3未初始化的張量
torch.rand(5, 3) # 隨機初始化張量
torch.zeros(5, 3, dtype=torch.long) # 零張量
torch.tensor([5.5, 3]) # 列表轉(zhuǎn)張量
x.new_ones(5, 3, dtype=torch.double) # 新的張量
torch.randn_like(x, dtype=torch.float) # 像x的張量,可以通過設(shè)置dtype等屬性修改張量的參數(shù)
x.size() # 獲取大小
# 加法
z = x+y
add(x,y[,out=z])
y.add_(x) # 在位加法,相當(dāng)于y+=x
# reshape,用view()
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
x.size():torch.Size([4, 4]);y.size():torch.Size([16]);z.size():torch.Size([2, 8])
# get one element
x.item() # 當(dāng)張量只有一個元素,使用item函數(shù)獲取該元素的值,可以視作python基本類型
# numpy轉(zhuǎn)張量
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
a:[2. 2. 2. 2. 2.];b:tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
神經(jīng)網(wǎng)絡(luò)
# 典型的神經(jīng)網(wǎng)絡(luò)訓(xùn)練過程是:定義網(wǎng)絡(luò)設(shè)定參數(shù),迭代輸入數(shù)據(jù)集,前向推導(dǎo),計算誤差,反向傳播梯度,更新網(wǎng)絡(luò)權(quán)重。
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 3x3 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 3)
self.conv2 = nn.Conv2d(6, 16, 3)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 6 * 6, 120) # 6*6 from image dimension
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
# 可學(xué)習(xí)的參數(shù)可以用net.parameters()返回
params = list(net.parameters())
print(len(params))
print(params[0].size()) # conv1's .weight
# 手動前向推導(dǎo)
input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)
# torch.nn只接受mini-batch輸入,不能是single sample
# 即接受圖片時,應(yīng)該是(1x64x64x3)張量,而不能是(64x64x3)張量。
# 如果有single sample使用input.unsqueeze(0)去增加虛假的維度
# loss定義
output = net(input)
target = torch.randn(10) # a dummy target, for example
target = target.view(1, -1) # make it the same shape as output
criterion = nn.MSELoss()
loss = criterion(output, target)
print(loss)
# 優(yōu)化器
import torch.optim as optim
optimizer = optim.SGD(net.parameters(), lr=0.01)
# in your training loop:
optimizer.zero_grad() # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step() # Does the update
# 如果想觀察梯度值,需要手動設(shè)置到0,因為梯度在反向傳播中是累積的。
optimizer.zero_grad()
訓(xùn)練圖片分類器
# 訓(xùn)練分類器包括以下步驟:使用torchvision加載和歸一化CIFAR10訓(xùn)練和測試集,定義CNN,定義LOSS,在訓(xùn)練集上訓(xùn)練,在測試集上測試
import torch
import torchvision
import torchvision.transforms as transforms
# 加載數(shù)據(jù),將[0,1]的數(shù)據(jù)歸一化到[-1,1]
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)
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')
# 可以展示一下數(shù)據(jù)集
import matplotlib.pyplot as plt
import numpy as np
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
# 定義網(wǎng)絡(luò)
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()
# 定義loss和optimizer
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
# 訓(xùn)練網(wǎng)絡(luò)
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
# 測試網(wǎng)絡(luò)
dataiter = iter(testloader)
images, labels = dataiter.next()
# print images
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
outputs = net(images)
_, predicted = torch.max(outputs, 1)
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))
# 評估網(wǎng)絡(luò)
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels).squeeze()
for i in range(4):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
for i in range(10):
print('Accuracy of %5s : %2d %%' % (classes[i], 100 * class_correct[i] / class_total[i]))
# 在gpu上訓(xùn)練
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)
inputs, labels = data[0].to(device), data[1].to(device)
多GPU訓(xùn)練
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# Parameters and DataLoaders
input_size = 5
output_size = 2
batch_size = 30
data_size = 100
# 產(chǎn)生隨機數(shù)據(jù)集
class RandomDataset(Dataset):
def __init__(self, size, length):
self.len = length
self.data = torch.randn(length, size)
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return self.len
rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),batch_size=batch_size, shuffle=True)
# 定義簡單網(wǎng)絡(luò),注意看print方法
class Model(nn.Module):
def __init__(self, input_size, output_size):
super(Model, self).__init__()
self.fc = nn.Linear(input_size, output_size)
def forward(self, input):
output = self.fc(input)
print("\tIn Model: input size", input.size(),"output size", output.size())
return output
# 創(chuàng)建模型和數(shù)據(jù)并行DataParallel
model = Model(input_size, output_size)
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
model = nn.DataParallel(model)
model.to(device)
# 運行模型
for data in rand_loader:
input = data.to(device)
output = model(input)
print("Outside: input size", input.size(),"output_size", output.size())
# 總結(jié)
# DataParallel自動將數(shù)據(jù)切分并送到多個GPU中,在每一個模型都完成了工作后,DataParallel收集和合并所有的結(jié)果,然后返回。
參考
張量
神經(jīng)網(wǎng)絡(luò)
訓(xùn)練圖片分類器
DataParallel
多GPU示例