一、最大池化的使用

池化——压缩特征

最大池化——取当前池化核中的最大的数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import torch
from torch import nn
from torch.nn import MaxPool2d

input = torch.tensor([[1, 2, 0, 3, 1],
[0, 1, 2, 3, 1],
[1, 2, 1, 0, 0],
[5, 2, 3, 1, 1],
[2, 1, 0, 1, 1]])
input = torch.reshape(input, (-1, 1, 5, 5))


class Test(nn.Module):
def __init__(self):
super(Test,self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)

def forward(self,input):
output = self.maxpool1(input)
return output


test = Test()
output = test(input)
print(output)

二、非线性激活层

Relu

sigmoid

三、线性层和其他层

dropout用于防止过拟合(过于关注噪点之类的,捡了芝麻丢了西瓜)

四、小实战和sequential的使用

sequential作用:类似compose,可以序列化执行操作,使代码更简洁

五、loss

1、计算实际输出和目标之间的差距

2、为更新输出提供一定的依据(反向传播,从loss反向修正参数)

这里backward()函数可以反向传播计算梯度(grad),将这个梯度给合适的优化器,可以对神经网络的参数进行更新。

六、优化器

TIPS

1、如果某一步的参数不会算,可以先让程序运行到上一步,然后print(output.size)查看对应属性的值

2、要将数据变为浮点数,后面写dtype=torch,float32