需要理解过程以及每个模块的作用
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
class Net(nn.Module):
def __init__(self,n_feature, n_hidden, n_output):
super(Net,self).__init__()
self.hidden = nn.Linear(n_feature,n_hidden)
self.predict = nn.Linear(n_hidden,n_output)
def forward(self, x):
x = F.relu(self.hidden(x))
x = self.predict(x)
return x
net1 = Net(1,10,1)
print(net1)
net2 = nn.Sequential(
nn.Linear(1,10),
nn.ReLU(), #实例化对象
nn.Linear(10,1)
)
print(net2)
输出:
Net(
(hidden): Linear(in_features=1, out_features=10, bias=True)
(predict): Linear(in_features=10, out_features=1, bias=True)
)
Sequential(
(0): Linear(in_features=1, out_features=10, bias=True)
(1): ReLU()
(2): Linear(in_features=10, out_features=1, bias=True)
)
实现mnist手写数字识别
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
import numpy as np
EPOCH = 1 # train the training data n times, to save time, we just train 1 epoch
BATCH_SIZE = 50
LR = 0.001 # learning rate
DOWNLOAD_MNIST = False
train_data = torchvision.datasets.MNIST(
root='./mnist/',
train=True, # this is training data
transform=torchvision.transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to
# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
download=DOWNLOAD_MNIST # download it if you don't have it
)
# print(train_data.train_data.size()) #[60000,28,28]
# print(train_data.train_labels.size()) #[60000]
train_loader = Data.DataLoader(
dataset=train_data,
batch_size=BATCH_SIZE,
shuffle=True,
num_workers=0
)
# convert test data into Variable, pick 2000 samples to speed up testing
test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
#使用unsqueeze对数据进行维度增加
test_x = Variable(torch.unsqueeze(test_data.test_data, dim=1)).type(torch.FloatTensor)[:2000]/255. # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
test_y = test_data.test_labels[:2000]
class CNN(nn.Module):
def __init__(self):
super(CNN,self).__init__()
self.conv1 = nn.Sequential( #[bacth_size,1,28,28]
nn.Conv2d(1,16,5,1,2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2) #[batch_size,16,14,14]
)
self.conv2 = nn.Sequential(
nn.Conv2d(16,32,5,1,2),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2) #[batch_size,32,7,7]
)
self.out = nn.Linear(32*7*7,10)
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x)
#x = x.view(-1,32*7*7)
x = x.view(x.size(0), -1)
output = self.out(x)
return output,x
cnn = CNN()
print(cnn)
optimizer = optim.Adam(cnn.parameters(),lr=LR)
loss_func = nn.CrossEntropyLoss()
for epoch in range(EPOCH):
for step, (x,y) in enumerate(train_loader):
b_x = Variable(x)
b_y = Variable(y)
output = cnn(b_x)[0]
loss = loss_func(output,b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step % 100 == 0:
test_output, last_layer = cnn(test_x)
pred_y = torch.max(test_output, 1)[1].data.squeeze()
accuracy = (pred_y == test_y).sum().item() / float(test_y.size(0))
print('Epoch: ', epoch, '| train loss: %.4f' % loss.item(), '| test accuracy: %.2f' % accuracy)
test_output, _ = cnn(test_x[:10])
pred_y = torch.max(test_output, 1)[1].data.numpy().squeeze()
print(pred_y, 'prediction number')
print(test_y[:10].numpy(), 'real number')
out:
CNN(
(conv1): Sequential(
(0): Conv2d(1, 16, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): ReLU()
(2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(conv2): Sequential(
(0): Conv2d(16, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): ReLU()
(2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(out): Linear(in_features=1568, out_features=10, bias=True)
)
Epoch: 0 | train loss: 2.3072 | test accuracy: 0.10
Epoch: 0 | train loss: 0.1293 | test accuracy: 0.89
Epoch: 0 | train loss: 0.1760 | test accuracy: 0.94
Epoch: 0 | train loss: 0.1097 | test accuracy: 0.96
Epoch: 0 | train loss: 0.0499 | test accuracy: 0.96
Epoch: 0 | train loss: 0.0317 | test accuracy: 0.97
Epoch: 0 | train loss: 0.1660 | test accuracy: 0.97
Epoch: 0 | train loss: 0.0486 | test accuracy: 0.97
Epoch: 0 | train loss: 0.0068 | test accuracy: 0.98
Epoch: 0 | train loss: 0.0150 | test accuracy: 0.98
Epoch: 0 | train loss: 0.1158 | test accuracy: 0.98
Epoch: 0 | train loss: 0.1373 | test accuracy: 0.98
[7 2 1 0 4 1 4 9 5 9] prediction number
[7 2 1 0 4 1 4 9 5 9] real number