Pytorch定义Conv Layer
-
卷积的讲解可以看我的这篇文章CS231n 笔记:通俗理解 CNN
下面主要讲解一下如何用pytorch定义卷积层
# 卷积神经网络的编写要用到nn.Conv2d # 该API意为进行2D的函数卷积层计算 import torch import torch.nn as nn layer = nn.Conv2d(1, 5, kernel_size=3, stride=1, padding=0) # 1代表每个kernel的channel是1,5代表kernel的数量,同时也是输出到下一层的channel数量 x = torch.rand(1, 1, 28, 28) # 1张图片,1channel,28*28 out = layer.forward(x) print(out.shape) print(layer.weight.size()) print(layer.bias.size())
输出
torch.Size([1, 5, 26, 26]) torch.Size([5, 1, 3, 3]) torch.Size([5])
这里可能需要解释一下weight的size,首先第一值5是kernel的数量,也是下一层的channel数量,第二个值1是input的channel数量,3和3表示kernel的长宽
需要注意,kernel的channel必须与input图片的channel相等,否则会报错,例如定义一个RGB三通道的输入图片,但是kernel的channel不是3就会报错,见下面的代码
layer = nn.Conv2d(2, 5, kernel_size=3, stride=1, padding=0) # 2代表每个kernel的channel是2 x = torch.rand(1, 3, 28, 28) # 1张图片,3channel,28*28 # 由于输入图片的channel是3,而kernel的channel是2,两者不等,所以会报错
除此之外,pytorch还有一种定义卷积神经网络的方法
import torch.nn.functional as F x = torch.rand(1, 1, 28, 28) weight = torch.rand(16, 3, 5, 5) b = torch.randn(16) out = F.conv2d(x, weight, b, stride=1, padding=1)
当然,上面的写法肯定会报错的,因为我们前面说了,weight的参数分别是kernel的数量、输入图片的channel,以及kernel的长宽。因为weight中制定输入图片的channel是3,因此x的channel必须是3,但是我们定义x的channel是1,它们发生了矛盾,所以报错了,把x重新定义为3通道
x = torch.rand(1, 3, 28, 28)
,或者把weight的3改成1即可weight = torch.rand(16, 1, 5, 5)