目录
1 卷积运算方法
kernel:卷积核
从Input里面从左到右从上到下依次移动选取与kernel同样大小的块与其作数乘放入output中
input有几个通道,卷积核就得有几个通道
3*5*5的input通过3*3*3的卷积获得1*3*3的output
输入有n个通道,卷积核就有n个通道,输出有m个通道,就有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)#3*3的卷积,图像大小减2 print(conv_layer.weight.shape)
m*n*w*h
2.padding
如果本来输入的是5*5,kernel是3*3,输出是3*3,如果想要输出不变也是5*5,由于kernel是3*3,那么就需要将input填充一圈0
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)
3.stride
之前的例子都是stride等于1,改成等于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) 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)
4.Max Pooling Layer 最大池化层
2*2的maxpooling默认stride = 2,它将原input分成4个2*2的小块,从每个小块里面选取最大值放入output中,大小缩小一半
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= torch.nn.MaxPool2d(kernel_size=2) output = maxpooling(input) print(output)
5.一个简单的卷积神经网络
模型:
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): 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) x = self.fc(x) return x#不做激活,因为做交叉熵损失算softmax model = Net()
本文为B站刘二大人pytorch学习视频的笔记