一般神经网络都是基于梯度下降算法来优化的,梯度下降是基于导数的,有一定的数学道理,但是常常会遇到局部最优问题,导致不能达到最优解
1. 定义神经网络模型
class Module(nn.Module):
def __init__(self, pso_data):
super(Module, self).__init__()
# 接收 pso_data 优化参数
self.pso_data = pso_data
# 图片尺寸:1*28*28
self.conv1 = nn.Conv2d(in_channels=1, out_channels=8, kernel_size=3, padding=1, bias=False)
self.conv2 = nn.Conv2d(in_channels=8, out_channels=4, kernel_size=3, padding=1, bias=False)
self.conv3 = nn.Conv2d(in_channels=4, out_channels=2, kernel_size=3, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(2)
self.bn2 = nn.BatchNorm2d(8)
self.maxPooling = nn.MaxPool2d(2)
self.fc = nn.Linear(98, 10)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
# 调用初始化 PSO 参数
self._model_param_init()
x = self.conv1(x)
x = self.bn2(x)
x = self.relu(self.maxPooling(x))
x = self.conv2(x)
x = self.relu(self.maxPooling(x))
x = self.conv3(x)
x = self.bn1(x)
x = self.relu(x)
# print("x.shape = ", x.shape)
# Flatten data from (64, 2, 7, 7) to (64,98)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
2.指定初始化参数
# 初始化函数
def _model_param_init(self):
self.conv1.weight.data = torch.FloatTensor(self.pso_data[0:72]).reshape(8, 1, 3, 3)
self.conv2.weight.data = torch.FloatTensor(self.pso_data[72:360]).reshape(4, 8, 3, 3)
self.conv3.weight.data = torch.FloatTensor(self.pso_data[360:432]).res