卷积层、池化层、激活层
二维卷积层Conv2d
torch.nn.Conv2d
conv1 = torch.nn.Conv2d(in_chns, out_chns, kernel_size=3, stride=2, padding=0)
output = conv1(input)
最大池化层MaxPool2d()
torch.nn.Conv2d
# ceil_mode影响返回的值
# 若为True,滑动到不满足kernel_size * kernel_size的区域时,返回值。
# 若为False, 滑动到不满足kernel_size * kernel_size的区域时,不返回值。
max_pool = torch.nn.MaxPool2d(kernel_size=3, stride=2, padding=0, ceil_mode=False)
output = max_pool(input)
非线性激活层ReLU()
torch.nn.ReLU
# 参数inplace影响结果
# 若为True,则覆盖输入
# 若为False,则不覆盖输入
relu = nn.ReLU(inplace = False)
output = relu(input)
进阶
class MyClass(nn.Module):
def __init__(self):
super(MyClass, self).__init__()
self.model1 = nn.Sequential(
nn.Conv2d(),
nn.MaxPool2d(),
...
)
def forward(self, x):
x = self.model1(x)
return x