# 将numpy的Array转换为torch的Tensor import numpy as np a = np.ones(5) b = torch.from_numpy(a) np.add(a, 1, out=a) print(a) print(b)
# 另外除了CharTensor之外,所有的tensor都可以在CPU运算和GPU预算之间相互转换 # 使用CUDA函数来将Tensor移动到GPU上 # 当CUDA可用时会进行GPU的运算 if torch.cuda.is_available(): x = x.cuda() y = y.cuda() x + y
import torch.nn as nn import torch.nn.functional as F
classNet(nn.Module): def__init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 6, 5) # 1 input image channel, 6 output channels, 5x5 square convolution kernel self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16*5*5, 120) # 16*5*5: input, 120: output self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) defforward(self, x):# x is input x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2)) # Max pooling over a (2, 2) window; x -> relu -> maxPool x = F.max_pool2d(F.relu(self.conv2(x)), 2) # If the size is a square you can only specify a single number x = x.view(-1, self._num_flat_features(x)) # need to flaten the x for the fc layer x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x # now return the output def_num_flat_features(self, x):# for flaten 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() net
'''神经网络的输出结果是这样的 Net ( (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1)) (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1)) (fc1): Linear (400 -> 120) (fc2): Linear (120 -> 84) (fc3): Linear (84 -> 10) ) '''
注意: torch.nn 接收的数据的第一个维度为batch size 整个torch.nn包只接受那种小批量样本的数据,而非单个样本。 例如,nn.Conv2d能够结构一个四维的TensornSamples x nChannels x Height x Width。 如果你拿的是单个样本,使用input.unsqueeze(0)来加一个假维度就可以了。
import torch.optim as optim # create your optimizer optimizer = optim.SGD(net.parameters(), lr = 0.01) # 把网络的参数绑定到optimizer上
# in your training loop: optimizer.zero_grad() # zero the gradient buffers output = net(input) # 网络输出 loss = criterion(output, target) # 计算loss loss.backward() # 反馈计算grad optimizer.step() # 它就会自动根据parameters,以及各自已经被更新好的grad,来更新parameters了
defforward(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()
3. 定义代价函数和优化器
1 2
criterion = nn.CrossEntropyLoss() # use a Classification Cross-Entropy loss optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
# the outputs are energies for the 10 classes. # Higher the energy for a class, the more the network # thinks that the image is of the particular class
# So, let's get the index of the highest energy _, predicted = torch.max(outputs.data, 1)
print('Predicted: ', ' '.join('%5s'% classes[predicted[j][0]] for j inrange(4)))
'''输出结果为 Predicted: cat plane car plane '''
结果看起来挺好。
看看神经网络在整个数据集上的表现结果如何。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
correct = 0 total = 0 for data in testloader: images, labels = data outputs = net(Variable(images)) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
'''输出结果为 Accuracy of the network on the 10000 test images: 54 % '''
class_correct = list(0.for i inrange(10)) class_total = list(0.for i inrange(10)) for data in testloader: images, labels = data outputs = net(Variable(images)) _, predicted = torch.max(outputs.data, 1) c = (predicted == labels).squeeze() for i inrange(4): label = labels[i] class_correct[label] += c[i] class_total[label] += 1
for i inrange(10): print('Accuracy of %5s : %2d %%' % (classes[i], 100 * class_correct[i] / class_total[i]))
'''输出结果为 Accuracy of plane : 73 % Accuracy of car : 70 % Accuracy of bird : 52 % Accuracy of cat : 27 % Accuracy of deer : 34 % Accuracy of dog : 37 % Accuracy of frog : 62 % Accuracy of horse : 72 % Accuracy of ship : 64 % Accuracy of truck : 53 % '''