示例
输入通道:
N输入通道和M输出通道:
卷积层:
import torch
in_channels,out_channels = 5,10
width,height = 100,100
kernel_size = 3
batch_size = 1
input = torch.randn(batch_size,
in_channels,
width,
height)
conv_layer = torch.nn.Conv2d(in_channels,
out_channels,
kernel_size=kernel_size)
output = conv_layer(input)
print(input.shape)
print(output.shape)
print(conv_layer.weight.shape)
输出结果:
torch.Size([1, 5, 100, 100])
torch.Size([1, 10, 98, 98])
torch.Size([10, 5, 3, 3])
padding的含义
padding=1:
import torch
input = [3,4,6,5,7,
2,4,6,8,2,
1,6,7,8,4,
9,7,4,6,2,
3,7,5,4,1]
input = torch.Tensor(input).view(1,1,5,5)
conv_layer = torch.nn.Conv2d(1,1,kernel_size=3,padding=1,bias=False)
kernel = torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1,1,3,3)#输出、维度、宽度、高度
conv_layer.weight.data = kernel.data
output = conv_layer(input)
print(output)
输出:
tensor([[[[ 91., 168., 224., 215., 127.],
[114., 211., 295., 262., 149.],
[192., 259., 282., 214., 122.],
[194., 251., 253., 169., 86.],
[ 96., 112., 110., 68., 31.]]]], grad_fn=)
stride的含义
stride=2
import torch
input = [3,4,6,5,7,
2,4,6,8,2,
1,6,7,8,4,
9,7,4,6,2,
3,7,5,4,1]
input = torch.Tensor(input).view(1,1,5,5)
conv_layer = torch.nn.Conv2d(1,1,kernel_size=3,stride=2,bias=False)#索引跳2个
kernel = torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1,1,3,3)#输出、维度、宽度、高度
conv_layer.weight.data = kernel.data
output = conv_layer(input)
print(output)
输出:
tensor([[[[211., 262.],
[251., 169.]]]], grad_fn=)
最大池化层(Max Pooling Layer)
下采样:
import torch
input = [3,4,6,5,
2,4,6,8,
1,6,7,8,
9,7,4,6,]
input = torch.Tensor(input).view(1,1,4,4)
maxpooling_layer = torch.nn.MaxPool2d(kernel_size = 2)
output = maxpooling_layer(input)
print(output)
输出:
tensor([[[[4., 8.],
[9., 8.]]]])
CNN实例
使用GPU训练模型:
import torch,torchvision
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
batch_size = 64
transform = transforms.Compose([
transforms.ToTensor(),#将PIL图像转换为PyTorch张量
transforms.Normalize((0.1307, ),(0.3081, ))#这两个参数是平均值和标准差
])
train_dataset = datasets.MNIST(root='../dataset/mnist/',
train = True,
download = True,
transform =transform)
train_loader = DataLoader(train_dataset,
shuffle = True,
batch_size = batch_size)
test_dataset = datasets.MNIST(root='../dataset/mnist/',
train=False,
download = True,
transform = transform)
test_loader = DataLoader(test_dataset,
shuffle = False,
batch_size = batch_size)
class Net(torch.nn.Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = torch.nn.Conv2d(1,10,kernel_size = 5)
self.conv2 = torch.nn.Conv2d(10,20,kernel_size = 5)
self.pooling = torch.nn.MaxPool2d(2)
self.fc = torch.nn.Linear(320,10)
def forward(self,x):
#将数据从(n,1,28,28)转换为 (n,784)
batch_size = x.size(0)
x = F.relu(self.pooling(self.conv1(x)))
x = F.relu(self.pooling(self.conv2(x)))
x = x.view(batch_size,-1)#flatten
x = self.fc(x)
return x
model = Net()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(),lr=0.01,momentum=0.5)
def train(epoch):
running_loss = 0.0
for batch_idx,data in enumerate(train_loader,0):
inputs, target = data
inputs, target = inputs.to(device),target.to(device)
optimizer.zero_grad()
#前向传播+反向传播+更新
outputs = model(inputs)
loss = criterion(outputs,target)
loss.backward()
optimizer.step()
running_loss += loss.item()
if batch_idx % 300 == 299:
print('[%d,%5d] loss: %.3f' % (epoch+1,batch_idx+1,running_loss/2000))
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
inputs, target = data
inputs, target = inputs.to(device),target.to(device)
outputs = model(inputs)
_,predicted = torch.max(outputs.data,dim=1)
total += target.size(0)
correct += (predicted == target).sum().item()
print('Accuracy on test set: %d %% [%d/%d]'% (100*correct/total,correct,total))
epoch_list = []
accuracy_list = []
for epoch in range(10):
train(epoch)
accuracy = test()
epoch_list.append(epoch)
accuracy_list.append(accuracy)
plt.plot(epoch_list,accuracy_list)
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.show()
结果: