您的位置:首页 > 科技 > IT业 > 河南网络优化服务_成都九度装饰设计有限公司_免费收录网站提交_新闻类软文

河南网络优化服务_成都九度装饰设计有限公司_免费收录网站提交_新闻类软文

2024/12/27 4:24:27 来源:https://blog.csdn.net/weixin_74113694/article/details/144634684  浏览:    关键词:河南网络优化服务_成都九度装饰设计有限公司_免费收录网站提交_新闻类软文
河南网络优化服务_成都九度装饰设计有限公司_免费收录网站提交_新闻类软文

目录

一、优化算法的实验设定

1.1 2D可视化实验(被优化函数为)

1.2 简单拟合实验

二、学习率调整

2.1 AdaGrad算法

2.2 RMSprop算法

三、梯度修正估计

3.1 动量法

3.2 Adam算法

四、被优化函数变为的2D可视化

五、不同优化器的3D可视化对比

1.被优化函数为

2.被优化函数为

3.被优化函数为

总结:

附完整可运行代码

被优化函数为的各类优化器2D可视化和拟合实验代码:

nndl.py:

 被优化函数为的2D可视化代码:

 被优化函数为的3D图代码:

 被优化函数为的3D可视化图:

被优化函数为的3D可视化图(期待改进):

实验大体步骤:

除了批大小对模型收敛速度的影响外,学习率和梯度估计也是影响神经网络优化的重要因素。神经网络优化中常用的优化方法也主要是如下两方面的改进,包括:

  • 学习率调整:主要通过自适应地调整学习率使得优化更稳定。这类算法主要有AdaGrad、RMSprop、AdaDelta算法等。
  • 梯度估计修正:主要通过修正每次迭代时估计的梯度方向来加快收敛速度。这类算法主要有动量法、Nesterov加速梯度方法等。

除上述方法外,本节还会介绍综合学习率调整和梯度估计修正的优化算法,如Adam算法。

一、优化算法的实验设定

为了更好地对比不同的优化算法,我们准备两个实验:第一个是2D可视化实验。第二个是简单拟合实验。

1.1 2D可视化实验(被优化函数为x^2

将被优化函数实现为OptimizedFunction算子,其forward方法是Sphere函数的前向计算,backward方法则计算被优化函数对x的偏导。

# =====将被优化函数实现为OptimizedFunction算子====================================================
class OptimizedFunction(Op):def __init__(self, w):super(OptimizedFunction, self).__init__()self.w = torch.as_tensor(w, dtype=torch.float32)self.params = {'x': torch.as_tensor(0, dtype=torch.float32)}self.grads = {'x': torch.as_tensor(0, dtype=torch.float32)}def forward(self, x):self.params['x'] = xreturn torch.matmul(self.w.T, torch.square(self.params['x']))def backward(self):self.grads['x'] = 2 * torch.multiply(self.w.T, self.params['x'])

nndl.op.Op如下:

class Op(object):def __init__(self):passdef __call__(self, inputs):return self.forward(torch.as_tensor(inputs,dtype=torch.float32))def forward(self, inputs):raise NotImplementedErrordef backward(self, inputs):raise NotImplementedError

训练函数 定义一个简易的训练函数,记录梯度下降过程中每轮的参数x和损失。代码实现如下:

# =====训练函数 定义一个简易的训练函数,记录梯度下降过程中每轮的参数x和损失==============================================================
def train_f(model, optimizer, x_init, epoch):x = x_initall_x = []losses = []for i in range(epoch):all_x.append(copy.copy(x.numpy()))loss = model(x)losses.append(loss)model.backward()optimizer.step()x = model.params['x']return torch.as_tensor(all_x), losses

可视化函数 定义一个Visualization类,用于绘制x的更新轨迹。代码如下:

# =======可视化函数===============================================
import numpy as np
import matplotlib.pyplot as pltclass Visualization(object):def __init__(self):x1 = np.arange(-5, 5, 0.1)x2 = np.arange(-5, 5, 0.1)x1, x2 = np.meshgrid(x1, x2)self.init_x = torch.as_tensor([x1, x2])def plot_2d(self, model, x, fig_name):fig, ax = plt.subplots(figsize=(10, 6))cp = ax.contourf(self.init_x[0], self.init_x[1], model(self.init_x.transpose(1, 0)),colors=['#e4007f', '#f19ec2', '#e86096', '#eb7aaa', '#f6c8dc', '#f5f5f5', '#000000'])c = ax.contour(self.init_x[0], self.init_x[1], model(self.init_x.transpose(1, 0)), colors='black')cbar = fig.colorbar(cp)ax.plot(x[:, 0], x[:, 1], '-o', color='#000000')ax.plot(0, 'r*', markersize=18, color='#fefefe')ax.set_xlabel('$x1$')ax.set_ylabel('$x2$')ax.set_xlim((-2, 5))ax.set_ylim((-2, 5))plt.savefig(fig_name)

定义train_and_plot_f函数,调用train_f和Visualization,训练模型并可视化参数更新轨迹。

# =====调用train_f和Visualization,训练模型并可视化参数更新轨迹======================================
def train_and_plot_f(model, optimizer, epoch, fig_name):x_init = torch.as_tensor([3, 4], dtype=torch.float32)print('x1 initiate: {}, x2 initiate: {}'.format(x_init[0].numpy(), x_init[1].numpy()))x, losses = train_f(model, optimizer, x_init, epoch)losses = np.array(losses)# 展示x1、x2的更新轨迹vis = Visualization()vis.plot_2d(model, x, fig_name)

模型训练与可视化:

from nndl.op import SimpleBatchGD
# 固定随机种子
torch.seed()
w = torch.as_tensor([0.2, 2])
model = OptimizedFunction(w)
opt = SimpleBatchGD(init_lr=0.2, model=model)
# train_and_plot_f(model, opt, epoch=20, fig_name='opti-vis-para.pdf')

nndl.op.SimpleBatchGD和Optimizer如下:

class SimpleBatchGD(Optimizer):def __init__(self, init_lr, model):super(SimpleBatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):#参数更新if isinstance(self.model.params, dict):for key in self.model.params.keys():self.model.params[key] = self.model.params[key] - self.init_lr * self.model.grads[key]# 优化器基类
class Optimizer(object):def __init__(self, init_lr, model):self.init_lr = init_lr#指定优化器需要优化的模型self.model = model@abstractmethoddef step(self):pass

运行结果:(SGD优化器)

1.2 简单拟合实验

数据集构建

# 固定随机种子
torch.seed()
# 随机生成shape为(1000,2)的训练数据
X = torch.randn([1000, 2])
w = torch.as_tensor([0.5, 0.8])
w = torch.unsqueeze(w, dim=1)
noise = 0.01 * torch.rand([1000])
noise = torch.unsqueeze(noise, dim=1)
# 计算y
y = torch.matmul(X, w) + noise
# 打印X, y样本
print('X: ', X[0].numpy())
print('y: ', y[0].numpy())# X,y组成训练样本数据
data = torch.concat((X, y), dim=1)
print('input data shape: ', data.shape)
print('data: ', data[0].numpy())

定义Linear算子,实现一个线性层的前向和反向计算。代码实现如下:

class Linear(Op):def __init__(self, input_size,  weight_init=np.random.standard_normal, bias_init=torch.zeros):self.params = {}self.params['W'] = weight_init([input_size, 1])self.params['W'] = torch.as_tensor(self.params['W'],dtype=torch.float32)self.params['b'] = bias_init([1])self.inputs = Noneself.grads = {}def forward(self, inputs):self.inputs = inputsself.outputs = torch.matmul(self.inputs, self.params['W']) + self.params['b']return self.outputsdef backward(self, labels):K = self.inputs.shape[0]self.grads['W'] = 1./ K*torch.matmul(self.inputs.T, (self.outputs - labels))self.grads['b'] = 1./K* torch.sum(self.outputs-labels, dim=0)

模型训练train函数的代码实现如下:

def train(data, num_epochs, batch_size, model, calculate_loss, optimizer, verbose=False):# 记录每个回合损失的变化epoch_loss = []# 记录每次迭代损失的变化iter_loss = []N = len(data)for epoch_id in range(num_epochs):# np.random.shuffle(data) #不再随机打乱数据# 将训练数据进行拆分,每个mini_batch包含batch_size条的数据mini_batches = [data[i:i+batch_size] for i in range(0, N, batch_size)]for iter_id, mini_batch in enumerate(mini_batches):# data中前两个分量为Xinputs = mini_batch[:, :-1]# data中最后一个分量为ylabels = mini_batch[:, -1:]# 前向计算outputs = model(inputs)# 计算损失loss = calculate_loss(outputs, labels).numpy()# 计算梯度model.backward(labels)# 梯度更新optimizer.step()iter_loss.append(loss)# verbose = True 则打印当前回合的损失if verbose:print('Epoch {:3d}, loss = {:.4f}'.format(epoch_id, np.mean(iter_loss)))epoch_loss.append(np.mean(iter_loss))return iter_loss, epoch_loss

优化过程可视化 定义plot_loss函数,用于绘制损失函数变化趋势。代码实现如下:

def plot_loss(iter_loss, epoch_loss, fig_name):"""可视化损失函数的变化趋势"""plt.figure(figsize=(10, 4))ax1 = plt.subplot(121)ax1.plot(iter_loss, color='#e4007f')plt.title('iteration loss')ax2 = plt.subplot(122)ax2.plot(epoch_loss, color='#f19ec2')plt.title('epoch loss')plt.savefig(fig_name)plt.show()

对于使用不同优化器的模型训练,保存每一个回合损失的更新情况,并绘制出损失函数的变化趋势,以此验证模型是否收敛。定义train_and_plot函数,调用train和plot_loss函数,训练并展示每个回合和每次迭代(Iteration)的损失变化情况。在模型训练时,使用torch.nn.MSELoss()计算均方误差。代码实现如下:

import torch.nn as nn
def train_and_plot(optimizer, fig_name):"""训练网络并画出损失函数的变化趋势输入:- optimizer:优化器"""# 定义均方差损失mse = nn.MSELoss()iter_loss, epoch_loss = train(data, num_epochs=30, batch_size=64, model=model, calculate_loss=mse, optimizer=optimizer)plot_loss(iter_loss, epoch_loss, fig_name)

训练网络并可视化损失函数的变化趋势。代码实现如下:

# 固定随机种子
torch.seed()
# 定义网络结构
model = Linear(2)
# 定义优化器
opt = SimpleBatchGD(init_lr=0.01, model=model)
train_and_plot(opt, 'opti-loss.pdf')

运行结果:

二、学习率调整

2.1 AdaGrad算法

构建优化器 定义Adagrad类,继承Optimizer类。定义step函数调用adagrad进行参数更新。代码实现如下:


class Adagrad(Optimizer):def __init__(self, init_lr, model, epsilon):super(Adagrad, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.epsilon = epsilondef adagrad(self, x, gradient_x, G, init_lr):G += gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):for key in self.model.params.keys():self.model.params[key], self.G[key] = self.adagrad(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)

2D可视化实验 使用被优化函数展示Adagrad算法的参数更新轨迹。代码实现如下:

# 固定随机种子
torch.seed()
w = torch.as_tensor([0.2, 2])
model2 = OptimizedFunction(w)
opt2 = Adagrad(init_lr=0.5, model=model2, epsilon=1e-7)
train_and_plot_f(model2, opt2, epoch=50, fig_name='opti-vis-para2.pdf')

 简单拟合实验 训练单层线性网络,验证损失是否收敛。代码实现如下:

# 固定随机种子
torch.seed()
# 定义网络结构
model = Linear(2)
# 定义优化器
opt = Adagrad(init_lr=0.1, model=model, epsilon=1e-7)
train_and_plot(opt, 'opti-loss2.pdf')

运行结果:

 

2.2 RMSprop算法

构建优化器 定义RMSprop类,继承Optimizer类。定义step函数调用rmsprop更新参数。代码实现如下:

class RMSprop(Optimizer):def __init__(self, init_lr, model, beta, epsilon):super(RMSprop, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.beta = betaself.epsilon = epsilondef rmsprop(self, x, gradient_x, G, init_lr):"""rmsprop算法更新参数,G为迭代梯度平方的加权移动平均"""G = self.beta * G + (1 - self.beta) * gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.rmsprop(self.model.params[key], self.model.grads[key],self.G[key], self.init_lr)

 2D可视化实验 使用被优化函数展示RMSprop算法的参数更新轨迹。代码实现如下:

torch.seed()
w = torch.as_tensor([0.2, 2])
model3 = OptimizedFunction(w)
opt3 = RMSprop(init_lr=0.1, model=model3, beta=0.9, epsilon=1e-7)
train_and_plot_f(model3, opt3, epoch=50, fig_name='opti-vis-para3.pdf')

简单拟合实验 训练单层线性网络,进行简单的拟合实验。代码实现如下:

# 固定随机种子
torch.seed()
# 定义网络结构
model = Linear(2)
# 定义优化器
opt3 = RMSprop(init_lr=0.1, model=model, beta=0.9, epsilon=1e-7)
train_and_plot(opt3, 'opti-loss3.pdf')

 运行结果:

三、梯度修正估计

除了调整学习率之外,还可以进行梯度估计修正。在小批量梯度下降法中,由于每次迭代的样本具有一定的随机性,因此每次迭代的梯度估计和整个训练集上的最优梯度并不一致。如果每次选取样本数量比较小,损失会呈振荡的方式下降。
一种有效地缓解梯度估计随机性的方式是通过使用最近一段时间内的平均梯度来代替当前时刻的随机梯度来作为参数更新的方向,从而提高优化速度。

3.1 动量法

构建优化器 定义Momentum类,继承Optimizer类。定义step函数调用momentum进行参数更新。代码实现如下:


class Momentum(Optimizer):def __init__(self, init_lr, model, rho):super(Momentum, self).__init__(init_lr=init_lr, model=model)self.delta_x = {}for key in self.model.params.keys():self.delta_x[key] = 0self.rho = rhodef momentum(self, x, gradient_x, delta_x, init_lr):"""momentum算法更新参数,delta_x为梯度的加权移动平均"""delta_x = self.rho * delta_x - init_lr * gradient_xx += delta_xreturn x, delta_xdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.delta_x[key] = self.momentum(self.model.params[key],self.model.grads[key],self.delta_x[key],self.init_lr)

2D可视化实验 使用被优化函数展示Momentum算法的参数更新轨迹。

# 固定随机种子
torch.seed()
w = torch.as_tensor([0.2, 2])
model4 = OptimizedFunction(w)
opt4 = Momentum(init_lr=0.01, model=model4, rho=0.9)
train_and_plot_f(model4, opt4, epoch=50, fig_name='opti-vis-para4.pdf')

 简单拟合实验 训练单层线性网络,进行简单的拟合实验。代码实现如下:

# 固定随机种子
torch.seed()# 定义网络结构
model = Linear(2)
# 定义优化器
opt = Momentum(init_lr=0.01, model=model, rho=0.9)
train_and_plot(opt, 'opti-loss4.pdf')

运行结果:

 

3.2 Adam算法

Adam算法(自适应矩估计算法)可以看作动量法和RMSprop算法的结合,不但使用动量作为参数更新方向,而且可以自适应调整学习率。代码如下:


class Adam(Optimizer):def __init__(self, init_lr, model, beta1, beta2, epsilon):super(Adam, self).__init__(init_lr=init_lr, model=model)self.beta1 = beta1self.beta2 = beta2self.epsilon = epsilonself.M, self.G = {}, {}for key in self.model.params.keys():self.M[key] = 0self.G[key] = 0self.t = 1def adam(self, x, gradient_x, G, M, t, init_lr):M = self.beta1 * M + (1 - self.beta1) * gradient_xG = self.beta2 * G + (1 - self.beta2) * gradient_x ** 2M_hat = M / (1 - self.beta1 ** t)G_hat = G / (1 - self.beta2 ** t)t += 1x -= init_lr / torch.sqrt(G_hat + self.epsilon) * M_hatreturn x, G, M, tdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key], self.M[key], self.t = self.adam(self.model.params[key],self.model.grads[key],self.G[key],self.M[key],self.t,self.init_lr)

2D可视化实验 使用被优化函数展示Adam算法的参数更新轨迹。代码实现如下:

# 固定随机种子
torch.seed()
w = torch.as_tensor([0.2, 2])
model5 = OptimizedFunction(w)
opt5 = Adam(init_lr=0.2, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)
train_and_plot_f(model5, opt5, epoch=20, fig_name='opti-vis-para5.pdf')

 简单拟合实验 训练单层线性网络,进行简单的拟合实验。代码实现如下:

# 固定随机种子
torch.seed()
# 定义网络结构
model = Linear(2)
# 定义优化器
opt5 = Adam(init_lr=0.1, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)
train_and_plot(opt5, 'opti-loss5.pdf')

运行结果:

 

我将书上的代码中epoch调到了70,发现此算法到达星点并不是简单的一个直线,有点像螺旋线一样。所以但从此实验的路径上看,Adam算法并没有AdaGrad算法好。

四、被优化函数变为\frac{1}{20}x^2+y^2的2D可视化

代码如下:

# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
from collections import OrderedDictclass SGD:"""随机梯度下降法(Stochastic Gradient Descent)"""def __init__(self, lr=0.01):self.lr = lrdef update(self, params, grads):for key in params.keys():params[key] -= self.lr * grads[key]class Momentum:"""Momentum SGD"""def __init__(self, lr=0.01, momentum=0.9):self.lr = lrself.momentum = momentumself.v = Nonedef update(self, params, grads):if self.v is None:self.v = {}for key, val in params.items():self.v[key] = np.zeros_like(val)for key in params.keys():self.v[key] = self.momentum * self.v[key] - self.lr * grads[key]params[key] += self.v[key]class Nesterov:"""Nesterov's Accelerated Gradient (http://arxiv.org/abs/1212.0901)"""def __init__(self, lr=0.01, momentum=0.9):self.lr = lrself.momentum = momentumself.v = Nonedef update(self, params, grads):if self.v is None:self.v = {}for key, val in params.items():self.v[key] = np.zeros_like(val)for key in params.keys():self.v[key] *= self.momentumself.v[key] -= self.lr * grads[key]params[key] += self.momentum * self.momentum * self.v[key]params[key] -= (1 + self.momentum) * self.lr * grads[key]class AdaGrad:"""AdaGrad"""def __init__(self, lr=0.01):self.lr = lrself.h = Nonedef update(self, params, grads):if self.h is None:self.h = {}for key, val in params.items():self.h[key] = np.zeros_like(val)for key in params.keys():self.h[key] += grads[key] * grads[key]params[key] -= self.lr * grads[key] / (np.sqrt(self.h[key]) + 1e-7)class RMSprop:"""RMSprop"""def __init__(self, lr=0.01, decay_rate=0.99):self.lr = lrself.decay_rate = decay_rateself.h = Nonedef update(self, params, grads):if self.h is None:self.h = {}for key, val in params.items():self.h[key] = np.zeros_like(val)for key in params.keys():self.h[key] *= self.decay_rateself.h[key] += (1 - self.decay_rate) * grads[key] * grads[key]params[key] -= self.lr * grads[key] / (np.sqrt(self.h[key]) + 1e-7)class Adam:"""Adam (http://arxiv.org/abs/1412.6980v8)"""def __init__(self, lr=0.001, beta1=0.9, beta2=0.999):self.lr = lrself.beta1 = beta1self.beta2 = beta2self.iter = 0self.m = Noneself.v = Nonedef update(self, params, grads):if self.m is None:self.m, self.v = {}, {}for key, val in params.items():self.m[key] = np.zeros_like(val)self.v[key] = np.zeros_like(val)self.iter += 1lr_t = self.lr * np.sqrt(1.0 - self.beta2 ** self.iter) / (1.0 - self.beta1 ** self.iter)for key in params.keys():self.m[key] += (1 - self.beta1) * (grads[key] - self.m[key])self.v[key] += (1 - self.beta2) * (grads[key] ** 2 - self.v[key])params[key] -= lr_t * self.m[key] / (np.sqrt(self.v[key]) + 1e-7)def f(x, y):return x ** 2 / 20.0 + y ** 2def df(x, y):return x / 10.0, 2.0 * yinit_pos = (-7.0, 2.0)
params = {}
params['x'], params['y'] = init_pos[0], init_pos[1]
grads = {}
grads['x'], grads['y'] = 0, 0optimizers = OrderedDict()
optimizers["SGD"] = SGD(lr=0.95)
optimizers["Momentum"] = Momentum(lr=0.1)
optimizers["AdaGrad"] = AdaGrad(lr=1.5)
optimizers["Adam"] = Adam(lr=0.3)idx = 1for key in optimizers:optimizer = optimizers[key]x_history = []y_history = []params['x'], params['y'] = init_pos[0], init_pos[1]for i in range(30):x_history.append(params['x'])y_history.append(params['y'])grads['x'], grads['y'] = df(params['x'], params['y'])optimizer.update(params, grads)x = np.arange(-10, 10, 0.01)y = np.arange(-5, 5, 0.01)X, Y = np.meshgrid(x, y)Z = f(X, Y)# for simple contour linemask = Z > 7Z[mask] = 0# plotplt.subplot(2, 2, idx)idx += 1plt.plot(x_history, y_history, 'o-', color="red")plt.contour(X, Y, Z)  # 绘制等高线plt.ylim(-10, 10)plt.xlim(-10, 10)plt.plot(0, 0, '+')plt.title(key)plt.xlabel("x")plt.ylabel("y")plt.subplots_adjust(wspace=0, hspace=0)  # 调整子图间距
plt.show()

运行结果:

五、不同优化器的3D可视化对比

1.被优化函数为f(x)={x[0]}^2+{x[1]}^2+{x[1]}^3+x[0]*x[1]

代码如下:

import torch
import numpy as np
import copy
from matplotlib import pyplot as plt
from matplotlib import animation
from itertools import zip_longestclass Op(object):def __init__(self):passdef __call__(self, inputs):return self.forward(inputs)# 输入:张量inputs# 输出:张量outputsdef forward(self, inputs):# return outputsraise NotImplementedError# 输入:最终输出对outputs的梯度outputs_grads# 输出:最终输出对inputs的梯度inputs_gradsdef backward(self, outputs_grads):# return inputs_gradsraise NotImplementedErrorclass Optimizer(object):  # 优化器基类def __init__(self, init_lr, model):"""优化器类初始化"""# 初始化学习率,用于参数更新的计算self.init_lr = init_lr# 指定优化器需要优化的模型self.model = modeldef step(self):"""定义每次迭代如何更新参数"""passclass SimpleBatchGD(Optimizer):def __init__(self, init_lr, model):super(SimpleBatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):# 参数更新if isinstance(self.model.params, dict):for key in self.model.params.keys():self.model.params[key] = self.model.params[key] - self.init_lr * self.model.grads[key]class Adagrad(Optimizer):def __init__(self, init_lr, model, epsilon):"""Adagrad 优化器初始化输入:- init_lr: 初始学习率 - model:模型,model.params存储模型参数值  - epsilon:保持数值稳定性而设置的非常小的常数"""super(Adagrad, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.epsilon = epsilondef adagrad(self, x, gradient_x, G, init_lr):"""adagrad算法更新参数,G为参数梯度平方的累计值。"""G += gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.adagrad(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class RMSprop(Optimizer):def __init__(self, init_lr, model, beta, epsilon):"""RMSprop优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta:衰减率- epsilon:保持数值稳定性而设置的常数"""super(RMSprop, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.beta = betaself.epsilon = epsilondef rmsprop(self, x, gradient_x, G, init_lr):"""rmsprop算法更新参数,G为迭代梯度平方的加权移动平均"""G = self.beta * G + (1 - self.beta) * gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.rmsprop(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class Momentum(Optimizer):def __init__(self, init_lr, model, rho):"""Momentum优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- rho:动量因子"""super(Momentum, self).__init__(init_lr=init_lr, model=model)self.delta_x = {}for key in self.model.params.keys():self.delta_x[key] = 0self.rho = rhodef momentum(self, x, gradient_x, delta_x, init_lr):"""momentum算法更新参数,delta_x为梯度的加权移动平均"""delta_x = self.rho * delta_x - init_lr * gradient_xx += delta_xreturn x, delta_xdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.delta_x[key] = self.momentum(self.model.params[key],self.model.grads[key],self.delta_x[key],self.init_lr)class Adam(Optimizer):def __init__(self, init_lr, model, beta1, beta2, epsilon):"""Adam优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta1, beta2:移动平均的衰减率- epsilon:保持数值稳定性而设置的常数"""super(Adam, self).__init__(init_lr=init_lr, model=model)self.beta1 = beta1self.beta2 = beta2self.epsilon = epsilonself.M, self.G = {}, {}for key in self.model.params.keys():self.M[key] = 0self.G[key] = 0self.t = 1def adam(self, x, gradient_x, G, M, t, init_lr):"""adam算法更新参数输入:- x:参数- G:梯度平方的加权移动平均- M:梯度的加权移动平均- t:迭代次数- init_lr:初始学习率"""M = self.beta1 * M + (1 - self.beta1) * gradient_xG = self.beta2 * G + (1 - self.beta2) * gradient_x ** 2M_hat = M / (1 - self.beta1 ** t)G_hat = G / (1 - self.beta2 ** t)t += 1x -= init_lr / torch.sqrt(G_hat + self.epsilon) * M_hatreturn x, G, M, tdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key], self.M[key], self.t = self.adam(self.model.params[key],self.model.grads[key],self.G[key],self.M[key],self.t,self.init_lr)class OptimizedFunction3D(Op):def __init__(self):super(OptimizedFunction3D, self).__init__()self.params = {'x': 0}self.grads = {'x': 0}def forward(self, x):self.params['x'] = xreturn x[0] ** 2 + x[1] ** 2 + x[1] ** 3 + x[0] * x[1]def backward(self):x = self.params['x']gradient1 = 2 * x[0] + x[1]gradient2 = 2 * x[1] + 3 * x[1] ** 2 + x[0]grad1 = torch.Tensor([gradient1])grad2 = torch.Tensor([gradient2])self.grads['x'] = torch.cat([grad1, grad2])class Visualization3D(animation.FuncAnimation):"""    绘制动态图像,可视化参数更新轨迹    """def __init__(self, *xy_values, z_values, labels=[], colors=[], fig, ax, interval=600, blit=True, **kwargs):"""初始化3d可视化类输入:xy_values:三维中x,y维度的值z_values:三维中z维度的值labels:每个参数更新轨迹的标签colors:每个轨迹的颜色interval:帧之间的延迟(以毫秒为单位)blit:是否优化绘图"""self.fig = figself.ax = axself.xy_values = xy_valuesself.z_values = z_valuesframes = max(xy_value.shape[0] for xy_value in xy_values)self.lines = [ax.plot([], [], [], label=label, color=color, lw=2)[0]for _, label, color in zip_longest(xy_values, labels, colors)]super(Visualization3D, self).__init__(fig, self.animate, init_func=self.init_animation, frames=frames,interval=interval, blit=blit, **kwargs)def init_animation(self):# 数值初始化for line in self.lines:line.set_data([], [])# line.set_3d_properties(np.asarray([]))  # 源程序中有这一行,加上会报错。 Edit by David 2022.12.4return self.linesdef animate(self, i):# 将x,y,z三个数据传入,绘制三维图像for line, xy_value, z_value in zip(self.lines, self.xy_values, self.z_values):line.set_data(xy_value[:i, 0], xy_value[:i, 1])line.set_3d_properties(z_value[:i])return self.linesdef train_f(model, optimizer, x_init, epoch):x = x_initall_x = []losses = []for i in range(epoch):all_x.append(copy.copy(x.numpy()))  # 浅拷贝 改为 深拷贝, 否则List的原值会被改变。 Edit by David 2022.12.4.loss = model(x)losses.append(loss)model.backward()optimizer.step()x = model.params['x']return torch.Tensor(np.array(all_x)), losses# 构建5个模型,分别配备不同的优化器
model1 = OptimizedFunction3D()
opt_gd = SimpleBatchGD(init_lr=0.01, model=model1)model2 = OptimizedFunction3D()
opt_adagrad = Adagrad(init_lr=0.5, model=model2, epsilon=1e-7)model3 = OptimizedFunction3D()
opt_rmsprop = RMSprop(init_lr=0.1, model=model3, beta=0.9, epsilon=1e-7)model4 = OptimizedFunction3D()
opt_momentum = Momentum(init_lr=0.01, model=model4, rho=0.9)model5 = OptimizedFunction3D()
opt_adam = Adam(init_lr=0.1, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)models = [model1, model2, model3, model4, model5]
opts = [opt_gd, opt_adagrad, opt_rmsprop, opt_momentum, opt_adam]x_all_opts = []
z_all_opts = []# 使用不同优化器训练for model, opt in zip(models, opts):x_init = torch.FloatTensor([2, 3])x_one_opt, z_one_opt = train_f(model, opt, x_init, 150)  # epoch# 保存参数值x_all_opts.append(x_one_opt.numpy())z_all_opts.append(np.squeeze(z_one_opt))# 使用numpy.meshgrid生成x1,x2矩阵,矩阵的每一行为[-3, 3],以0.1为间隔的数值
x1 = np.arange(-3, 3, 0.1)
x2 = np.arange(-3, 3, 0.1)
x1, x2 = np.meshgrid(x1, x2)
init_x = torch.Tensor(np.array([x1, x2]))model = OptimizedFunction3D()# 绘制 f_3d函数 的 三维图像
fig = plt.figure()
ax = plt.axes(projection='3d')
X = init_x[0].numpy()
Y = init_x[1].numpy()
Z = model(init_x).numpy()  # 改为 model(init_x).numpy() David 2022.12.4
ax.plot_surface(X, Y, Z, cmap='rainbow')ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('f(x1,x2)')labels = ['SGD', 'AdaGrad', 'RMSprop', 'Momentum', 'Adam']
colors = ['#f6373c', '#f6f237', '#45f637', '#37f0f6', '#000000']animator = Visualization3D(*x_all_opts, z_values=z_all_opts, labels=labels, colors=colors, fig=fig, ax=ax)
ax.legend(loc='upper left')plt.show()

 运行结果:

从图可以看到被优化函数在(0,0)处存在一个鞍点,即一个既不是极大值点也不是极小值点的临界点。从右图中可以看到,对于我们构建的函数,只有优化器Momentum在参数更新时成功逃离鞍点,其他优化器在实验中收敛到鞍点处没有逃离成功。但这并不证明Momentum优化器是最好的优化器,在模型训练时使用哪种优化器,还要结合具体的场景和数据具体分析。

2.被优化函数为f(x)=\frac{1}{2}{x[0]}^2+{x[1]}^2

代码如下:

import torch
import numpy as np
import copy
from matplotlib import pyplot as plt
from matplotlib import animation
from itertools import zip_longest
from matplotlib import cmclass Op(object):def __init__(self):passdef __call__(self, inputs):return self.forward(inputs)# 输入:张量inputs# 输出:张量outputsdef forward(self, inputs):# return outputsraise NotImplementedError# 输入:最终输出对outputs的梯度outputs_grads# 输出:最终输出对inputs的梯度inputs_gradsdef backward(self, outputs_grads):# return inputs_gradsraise NotImplementedErrorclass Optimizer(object):  # 优化器基类def __init__(self, init_lr, model):"""优化器类初始化"""# 初始化学习率,用于参数更新的计算self.init_lr = init_lr# 指定优化器需要优化的模型self.model = modeldef step(self):"""定义每次迭代如何更新参数"""passclass SimpleBatchGD(Optimizer):def __init__(self, init_lr, model):super(SimpleBatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):# 参数更新if isinstance(self.model.params, dict):for key in self.model.params.keys():self.model.params[key] = self.model.params[key] - self.init_lr * self.model.grads[key]class Adagrad(Optimizer):def __init__(self, init_lr, model, epsilon):"""Adagrad 优化器初始化输入:- init_lr: 初始学习率 - model:模型,model.params存储模型参数值  - epsilon:保持数值稳定性而设置的非常小的常数"""super(Adagrad, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.epsilon = epsilondef adagrad(self, x, gradient_x, G, init_lr):"""adagrad算法更新参数,G为参数梯度平方的累计值。"""G += gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.adagrad(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class RMSprop(Optimizer):def __init__(self, init_lr, model, beta, epsilon):"""RMSprop优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta:衰减率- epsilon:保持数值稳定性而设置的常数"""super(RMSprop, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.beta = betaself.epsilon = epsilondef rmsprop(self, x, gradient_x, G, init_lr):"""rmsprop算法更新参数,G为迭代梯度平方的加权移动平均"""G = self.beta * G + (1 - self.beta) * gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.rmsprop(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class Momentum(Optimizer):def __init__(self, init_lr, model, rho):"""Momentum优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- rho:动量因子"""super(Momentum, self).__init__(init_lr=init_lr, model=model)self.delta_x = {}for key in self.model.params.keys():self.delta_x[key] = 0self.rho = rhodef momentum(self, x, gradient_x, delta_x, init_lr):"""momentum算法更新参数,delta_x为梯度的加权移动平均"""delta_x = self.rho * delta_x - init_lr * gradient_xx += delta_xreturn x, delta_xdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.delta_x[key] = self.momentum(self.model.params[key],self.model.grads[key],self.delta_x[key],self.init_lr)class Adam(Optimizer):def __init__(self, init_lr, model, beta1, beta2, epsilon):"""Adam优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta1, beta2:移动平均的衰减率- epsilon:保持数值稳定性而设置的常数"""super(Adam, self).__init__(init_lr=init_lr, model=model)self.beta1 = beta1self.beta2 = beta2self.epsilon = epsilonself.M, self.G = {}, {}for key in self.model.params.keys():self.M[key] = 0self.G[key] = 0self.t = 1def adam(self, x, gradient_x, G, M, t, init_lr):"""adam算法更新参数输入:- x:参数- G:梯度平方的加权移动平均- M:梯度的加权移动平均- t:迭代次数- init_lr:初始学习率"""M = self.beta1 * M + (1 - self.beta1) * gradient_xG = self.beta2 * G + (1 - self.beta2) * gradient_x ** 2M_hat = M / (1 - self.beta1 ** t)G_hat = G / (1 - self.beta2 ** t)t += 1x -= init_lr / torch.sqrt(G_hat + self.epsilon) * M_hatreturn x, G, M, tdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key], self.M[key], self.t = self.adam(self.model.params[key],self.model.grads[key],self.G[key],self.M[key],self.t,self.init_lr)class OptimizedFunction3D(Op):def __init__(self):super(OptimizedFunction3D, self).__init__()self.params = {'x': 0}self.grads = {'x': 0}def forward(self, x):self.params['x'] = xreturn - x[0] * x[0] / 2 + x[1] * x[1] / 1  # x[0] ** 2 + x[1] ** 2 + x[1] ** 3 + x[0] * x[1]def backward(self):x = self.params['x']gradient1 = - 2 * x[0] / 2gradient2 = 2 * x[1] / 1grad1 = torch.Tensor([gradient1])grad2 = torch.Tensor([gradient2])self.grads['x'] = torch.cat([grad1, grad2])class Visualization3D(animation.FuncAnimation):"""    绘制动态图像,可视化参数更新轨迹    """def __init__(self, *xy_values, z_values, labels=[], colors=[], fig, ax, interval=100, blit=True, **kwargs):"""初始化3d可视化类输入:xy_values:三维中x,y维度的值z_values:三维中z维度的值labels:每个参数更新轨迹的标签colors:每个轨迹的颜色interval:帧之间的延迟(以毫秒为单位)blit:是否优化绘图"""self.fig = figself.ax = axself.xy_values = xy_valuesself.z_values = z_valuesframes = max(xy_value.shape[0] for xy_value in xy_values)# , marker = 'o'self.lines = [ax.plot([], [], [], label=label, color=color, lw=2)[0]for _, label, color in zip_longest(xy_values, labels, colors)]print(self.lines)super(Visualization3D, self).__init__(fig, self.animate, init_func=self.init_animation, frames=frames,interval=interval, blit=blit, **kwargs)def init_animation(self):# 数值初始化for line in self.lines:line.set_data([], [])# line.set_3d_properties(np.asarray([]))  # 源程序中有这一行,加上会报错。 Edit by David 2022.12.4return self.linesdef animate(self, i):# 将x,y,z三个数据传入,绘制三维图像for line, xy_value, z_value in zip(self.lines, self.xy_values, self.z_values):line.set_data(xy_value[:i, 0], xy_value[:i, 1])line.set_3d_properties(z_value[:i])return self.linesdef train_f(model, optimizer, x_init, epoch):x = x_initall_x = []losses = []for i in range(epoch):all_x.append(copy.deepcopy(x.numpy()))  # 浅拷贝 改为 深拷贝, 否则List的原值会被改变。 Edit by David 2022.12.4.loss = model(x)losses.append(loss)model.backward()optimizer.step()x = model.params['x']return torch.Tensor(np.array(all_x)), losses# 构建5个模型,分别配备不同的优化器
model1 = OptimizedFunction3D()
opt_gd = SimpleBatchGD(init_lr=0.05, model=model1)model2 = OptimizedFunction3D()
opt_adagrad = Adagrad(init_lr=0.05, model=model2, epsilon=1e-7)model3 = OptimizedFunction3D()
opt_rmsprop = RMSprop(init_lr=0.05, model=model3, beta=0.9, epsilon=1e-7)model4 = OptimizedFunction3D()
opt_momentum = Momentum(init_lr=0.05, model=model4, rho=0.9)model5 = OptimizedFunction3D()
opt_adam = Adam(init_lr=0.05, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)models = [model5, model2, model3, model4, model1]
opts = [opt_adam, opt_adagrad, opt_rmsprop, opt_momentum, opt_gd]x_all_opts = []
z_all_opts = []# 使用不同优化器训练for model, opt in zip(models, opts):x_init = torch.FloatTensor([0.00001, 0.5])x_one_opt, z_one_opt = train_f(model, opt, x_init, 100)  # epoch# 保存参数值x_all_opts.append(x_one_opt.numpy())z_all_opts.append(np.squeeze(z_one_opt))# 使用numpy.meshgrid生成x1,x2矩阵,矩阵的每一行为[-3, 3],以0.1为间隔的数值
x1 = np.arange(-1, 2, 0.01)
x2 = np.arange(-1, 1, 0.05)
x1, x2 = np.meshgrid(x1, x2)
init_x = torch.Tensor(np.array([x1, x2]))model = OptimizedFunction3D()# 绘制 f_3d函数 的 三维图像
fig = plt.figure()
ax = plt.axes(projection='3d')
X = init_x[0].numpy()
Y = init_x[1].numpy()
Z = model(init_x).numpy()  # 改为 model(init_x).numpy() David 2022.12.4
surf = ax.plot_surface(X, Y, Z, edgecolor='grey', cmap=cm.coolwarm)
# fig.colorbar(surf, shrink=0.5, aspect=1)
ax.set_zlim(-3, 2)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('f(x1,x2)')labels = ['Adam', 'AdaGrad', 'RMSprop', 'Momentum', 'SGD']
colors = ['#8B0000', '#0000FF', '#000000', '#008B00', '#FF0000']animator = Visualization3D(*x_all_opts, z_values=z_all_opts, labels=labels, colors=colors, fig=fig, ax=ax)
ax.legend(loc='upper right')plt.show()

运行结果:

从图中可以看出,只有SGD没有逃离鞍点,其他优化器均逃离。同时,观看博客上的动图可知,Momentum优化器最开始也是陷入了鞍点之中的,当找到逃离鞍点的方向时,就会下降的特别快,甚至超过了没有陷入鞍点的AdaGrad算法。

得出了一个和上文相反的运行结果,对于上文实验的平面是只有Momentum逃离鞍点,对于本节实验则开始Momentum溜的最慢,说明算法的优劣和所优化的平面有非常大的关系,对应到数据上,就是不同的数据分布所需要的优化算法是不同的,具体什么优化算法就得实验结果来说明了,实践是检验真理的唯一标准。

Momentum算法当某个参数在最近一段时间内的梯度方向不一致时,其真实的参数更新幅度变小;相反,当在最近一段时间内的梯度方向都一致时,其真实的参数更新幅度变大,起到加速作用.从此图中可以看到,Momentum最初陷入了鞍点,但是当找到逃离鞍点的梯度方向后,下降的就很快了。

3.被优化函数为\frac{1}{20}x^2+y^2

代码如下:

import animator
import torch
import numpy as np
import copy
from matplotlib import pyplot as plt
from matplotlib import animation
from itertools import zip_longest
from matplotlib import cm
from matplotlib.animation import FuncAnimationclass Op(object):def __init__(self):passdef __call__(self, inputs):return self.forward(inputs)# 输入:张量inputs# 输出:张量outputsdef forward(self, inputs):# return outputsraise NotImplementedError# 输入:最终输出对outputs的梯度outputs_grads# 输出:最终输出对inputs的梯度inputs_gradsdef backward(self, outputs_grads):# return inputs_gradsraise NotImplementedErrorclass Optimizer(object):  # 优化器基类def __init__(self, init_lr, model):"""优化器类初始化"""# 初始化学习率,用于参数更新的计算self.init_lr = init_lr# 指定优化器需要优化的模型self.model = modeldef step(self):"""定义每次迭代如何更新参数"""passclass SimpleBatchGD(Optimizer):def __init__(self, init_lr, model):super(SimpleBatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):# 参数更新if isinstance(self.model.params, dict):for key in self.model.params.keys():self.model.params[key] = self.model.params[key] - self.init_lr * self.model.grads[key]class Adagrad(Optimizer):def __init__(self, init_lr, model, epsilon):"""Adagrad 优化器初始化输入:- init_lr: 初始学习率 - model:模型,model.params存储模型参数值  - epsilon:保持数值稳定性而设置的非常小的常数"""super(Adagrad, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.epsilon = epsilondef adagrad(self, x, gradient_x, G, init_lr):"""adagrad算法更新参数,G为参数梯度平方的累计值。"""G += gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.adagrad(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class RMSprop(Optimizer):def __init__(self, init_lr, model, beta, epsilon):"""RMSprop优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta:衰减率- epsilon:保持数值稳定性而设置的常数"""super(RMSprop, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.beta = betaself.epsilon = epsilondef rmsprop(self, x, gradient_x, G, init_lr):"""rmsprop算法更新参数,G为迭代梯度平方的加权移动平均"""G = self.beta * G + (1 - self.beta) * gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.rmsprop(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class Momentum(Optimizer):def __init__(self, init_lr, model, rho):"""Momentum优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- rho:动量因子"""super(Momentum, self).__init__(init_lr=init_lr, model=model)self.delta_x = {}for key in self.model.params.keys():self.delta_x[key] = 0self.rho = rhodef momentum(self, x, gradient_x, delta_x, init_lr):"""momentum算法更新参数,delta_x为梯度的加权移动平均"""delta_x = self.rho * delta_x - init_lr * gradient_xx += delta_xreturn x, delta_xdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.delta_x[key] = self.momentum(self.model.params[key],self.model.grads[key],self.delta_x[key],self.init_lr)class Adam(Optimizer):def __init__(self, init_lr, model, beta1, beta2, epsilon):"""Adam优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta1, beta2:移动平均的衰减率- epsilon:保持数值稳定性而设置的常数"""super(Adam, self).__init__(init_lr=init_lr, model=model)self.beta1 = beta1self.beta2 = beta2self.epsilon = epsilonself.M, self.G = {}, {}for key in self.model.params.keys():self.M[key] = 0self.G[key] = 0self.t = 1def adam(self, x, gradient_x, G, M, t, init_lr):"""adam算法更新参数输入:- x:参数- G:梯度平方的加权移动平均- M:梯度的加权移动平均- t:迭代次数- init_lr:初始学习率"""M = self.beta1 * M + (1 - self.beta1) * gradient_xG = self.beta2 * G + (1 - self.beta2) * gradient_x ** 2M_hat = M / (1 - self.beta1 ** t)G_hat = G / (1 - self.beta2 ** t)t += 1x -= init_lr / torch.sqrt(G_hat + self.epsilon) * M_hatreturn x, G, M, tdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key], self.M[key], self.t = self.adam(self.model.params[key],self.model.grads[key],self.G[key],self.M[key],self.t,self.init_lr)class OptimizedFunction3D(Op):def __init__(self):super(OptimizedFunction3D, self).__init__()self.params = {'x': 0}self.grads = {'x': 0}def forward(self, x):self.params['x'] = xreturn x[0] * x[0] / 20 + x[1] * x[1] / 1  # x[0] ** 2 + x[1] ** 2 + x[1] ** 3 + x[0] * x[1]def backward(self):x = self.params['x']gradient1 = 2 * x[0] / 20gradient2 = 2 * x[1] / 1grad1 = torch.Tensor([gradient1])grad2 = torch.Tensor([gradient2])self.grads['x'] = torch.cat([grad1, grad2])class Visualization3D(animation.FuncAnimation):"""绘制动态图像,可视化参数更新轨迹"""def __init__(self, *xy_values, z_values, labels=[], colors=[], fig, ax, interval=100, blit=True, **kwargs):"""初始化3D可视化类输入:xy_values:三维中x,y维度的值z_values:三维中z维度的值labels:每个参数更新轨迹的标签colors:每个轨迹的颜色interval:帧之间的延迟(以毫秒为单位)blit:是否优化绘图"""self.fig = figself.ax = axself.xy_values = xy_valuesself.z_values = z_valuesself.surf = None  # 曲面图初始化为空frames = max(xy_value.shape[0] for xy_value in xy_values)self.lines = [ax.plot([], [], [], label=label, color=color, lw=2)[0]for _, label, color in zip_longest(xy_values, labels, colors)]self.points = [ax.plot([], [], [], color=color, markeredgewidth=1, markeredgecolor='black', marker='o')[0]for _, color in zip_longest(xy_values, colors)]super(Visualization3D, self).__init__(fig, self.animate, init_func=self.init_animation, frames=frames,interval=interval, blit=blit, **kwargs)def init_animation(self):# 数值初始化for line in self.lines:line.set_data([], [])for point in self.points:point.set_data([], [])if self.surf is not None:self.surf.remove()self.surf = None  # 确保清除之前的曲面return self.points + self.linesdef animate(self, i):try:print(f"Frame {i}")  # 打印当前帧号for line, xy_value, z_value in zip(self.lines, self.xy_values, self.z_values):line.set_data(xy_value[:i, 0], xy_value[:i, 1])line.set_3d_properties(z_value[:i])for point, xy_value, z_value in zip(self.points, self.xy_values, self.z_values):point.set_data(xy_value[i, 0], xy_value[i, 1])point.set_3d_properties(z_value[i])if self.surf is not None:self.surf.remove()  # 移除前一帧的曲面x = self.xy_values[0][:i, 0]y = self.xy_values[0][:i, 1]z = self.z_values[0][:i]X, Y = np.meshgrid(x, y)Z = np.reshape(z, (len(x), len(y)))self.surf = self.ax.plot_surface(X, Y, Z, color='gray', alpha=0.5, rstride=1, cstride=1)return self.points + self.lines + [self.surf]except Exception as e:print(f"Error in frame {i}: {e}")raisedef train_f(model, optimizer, x_init, epoch):x = x_initall_x = []losses = []for i in range(epoch):all_x.append(copy.deepcopy(x.numpy()))  # 浅拷贝 改为 深拷贝, 否则List的原值会被改变。 Edit by David 2022.12.4.loss = model(x)losses.append(loss)model.backward()optimizer.step()x = model.params['x']return torch.Tensor(np.array(all_x)), losses# 构建5个模型,分别配备不同的优化器
model1 = OptimizedFunction3D()
opt_gd = SimpleBatchGD(init_lr=0.95, model=model1)model2 = OptimizedFunction3D()
opt_adagrad = Adagrad(init_lr=1.5, model=model2, epsilon=1e-7)model3 = OptimizedFunction3D()
opt_rmsprop = RMSprop(init_lr=0.05, model=model3, beta=0.9, epsilon=1e-7)model4 = OptimizedFunction3D()
opt_momentum = Momentum(init_lr=0.1, model=model4, rho=0.9)model5 = OptimizedFunction3D()
opt_adam = Adam(init_lr=0.3, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)models = [model1, model2, model3, model4, model5]
opts = [opt_gd, opt_adagrad, opt_rmsprop, opt_momentum, opt_adam]x_all_opts = []  # 存储每种优化器的参数轨迹
z_all_opts = []  # 存储每种优化器的函数值轨迹# 使用不同优化器训练
for model, opt in zip(models, opts):x_init = torch.FloatTensor([-7, 2])  # 初始值x_one_opt, z_one_opt = train_f(model, opt, x_init, 100)  # 训练 100 个 epoch# 保存参数值x_all_opts.append(x_one_opt.numpy())  # 转为 NumPy 数组,形状为 (T, 2)z_all_opts.append(np.squeeze(z_one_opt))  # 转为 NumPy 数组,形状为 (T,)# print("x_all_opts:", x_all_opts)
# print("z_all_opts:", z_all_opts)# 使用numpy.meshgrid生成x1,x2矩阵
x1 = np.arange(-10, 10, 0.01)
x2 = np.arange(-5, 5, 0.01)
x1, x2 = np.meshgrid(x1, x2)# 将x1和x2作为输入,堆叠成形状为 (2, M, N)
init_x = torch.Tensor(np.stack([x1, x2], axis=0))  # 形状为 (2, M, N)# 假设模型已经定义
model = OptimizedFunction3D()# 确保模型能处理形状为 (2, M, N) 的输入
Z = model(init_x)
if isinstance(Z, torch.Tensor):  # 如果是PyTorch张量,转换为NumPy数组Z = Z.detach().numpy()# 确保 Z 的形状和 x1, x2 一致
Z = Z.reshape(x1.shape)# 绘制三维表面
fig = plt.figure()
ax = plt.axes(projection='3d')
surf = ax.plot_surface(x1, x2, Z, cmap='viridis', edgecolor='none', alpha=0.6)# 设置标题和坐标轴标签
ax.set_title('Optimized Function 3D')
ax.set_xlabel('X1')
ax.set_ylabel('X2')
ax.set_zlabel('Z')# 添加颜色条
fig.colorbar(surf)# 每种优化器的训练轨迹
labels = ['SGD', 'Adagrad', 'RMSprop', 'Momentum', 'Adam']
colors = ['r', 'g', 'b', 'c', 'm']# 假设 x_all_opts 和 z_all_opts 是训练过程中收集的轨迹点
# x_all_opts[i] 是形状 (T, 2) 的数组,表示第 i 种优化器 T 步的 (x1, x2) 参数
# z_all_opts[i] 是形状 (T,) 的数组,表示每步对应的函数值 z
# x_all_opts = [...]  # 填入实际数据
# z_all_opts = [...]  # 填入实际数据# 初始化小球的位置
balls = []
for color in colors:ball, = ax.plot([], [], [], 'o', color=color, markersize=6)balls.append(ball)# 初始化小球和轨迹
scatters = []  # 保存小球的列表
lines = []  # 保存轨迹线的列表for i in range(len(x_all_opts)):scatter = ax.scatter(x_all_opts[i][0, 0],x_all_opts[i][0, 1],z_all_opts[i][0],color=colors[i],s=50,  # 小球大小label=labels[i])scatters.append(scatter)line, = ax.plot([], [], [],color=colors[i],linewidth=1.5,alpha=0.8)lines.append(line)# 添加图例到右上角
ax.legend(loc="upper right")# 更新函数
def update(frame):updated_artists = []  # 存储更新的对象for i in range(len(scatters)):# 更新小球位置scatters[i]._offsets3d = ([x_all_opts[i][frame, 0]],[x_all_opts[i][frame, 1]],[z_all_opts[i][frame]],)# 更新轨迹线lines[i].set_data(x_all_opts[i][:frame + 1, 0],  # X 轨迹x_all_opts[i][:frame + 1, 1],  # Y 轨迹)lines[i].set_3d_properties(z_all_opts[i][:frame + 1])  # Z 轨迹# 添加到更新列表updated_artists.append(scatters[i])updated_artists.append(lines[i])return updated_artists  # 返回更新的对象# 创建动画
num_frames = max(len(x) for x in x_all_opts)  # 获取最长的轨迹步数
ani = FuncAnimation(fig, update, frames=num_frames, interval=100, blit=True)# 显示图像
plt.show()
# animator.save('teaser' + '.gif', writer='imagemagick', fps=10)  # 效果不好,估计被挡住了…… 有待进一步提高 Edit by David 2022.12.4
# save不好用,不费劲了,安装个软件做gif https://pc.qq.com/detail/13/detail_23913.html

运行结果:

从图中可以看到,此函数的极小值点位于(0,0)。在迭代的过程中,RMSprop算法最慢,在其他优化器均到达的情况下,它还没有到。虽然SGD到达的路径崎岖了点,但终究是很快就到了。观察Momentum的路径,相当于是SGD路径的一个优化,更加平滑了些。
老师博客上的这个代码我运行有点问题,总是弹出一个2D图就闪退了,修改了一下是我这个样子了,代码放在了我的博客上。不过也有一点缺点,就是小球动的不是很灵敏,要拖动一下才会动一会儿。

在迭代的过程中,RMSprop算法最慢,在其他优化器均到达的情况下,它还没有到,这进一步说明了在模型训练时使用哪种优化器,需要结合具体的场景和数据具体分析。

从图的路径可以看出,虽然SGD到达的路径崎岖了点,但终究是很快就到了。观察Momentum的路径,相当于是SGD路径的一个优化,更加平滑了些。通过观察他们的公式也可以知道,Momentum是对SGD公式的一个优化。SGD倾向于在狭窄的沟谷上摆动,因为负梯度将沿着陡峭的一侧下降,而非沿着沟谷向最优点前进。动量则有助于加速梯度向正确的方向前进。

总结:

1.各算法的比较

2.为什么SGD会走“之字形”?其它算法为什么会比较平滑?

因为在此函数中梯度的方向并没有指向最小值的方向,SGD只是单纯的朝着梯度方向,放弃了对梯度准确性的追求,会使得其在 函数的形状非均向(比如y方向变化很大时,x方向变化很小 ),能迂回往复地寻找,效率很低。
其他算法因为虽然x轴方向上受到的力非常小,但是一直在同一方向上受力,所以朝同一个方向会有一定的加速。反过来,虽然y轴方向上受到的力很大,但是因为交互地受到正方向和反方向的力,它们会互相抵消,所以y轴方向上的速度不稳定。因此,和SGD时的情形相比,可以更快地朝x轴方向靠近,减弱“之”字形的变动程度。

3.Adam这么好,SGD是不是就用不到了?

虽然 Adam 优化器具有许多优点,例如快速收敛、自适应学习率和对稀疏数据的鲁棒性,但 SGD 并未被完全取代,仍然在许多场景下广泛使用。
(1)Adam 在某些任务中容易导致模型过拟合,因为其优化路径更快趋于局部最优。SGD 的噪声特性(梯度更新基于小批量)有助于逃离局部最优,从而提升泛化能力,尤其是在图像分类任务(如 ImageNet)中表现突出。

(2)SGD 实现简单,对计算资源要求低。在参数和学习率的调节下,SGD 能表现出良好的鲁棒性。

(3)对于超大规模数据集和分布式训练,SGD 是首选,因为其计算和内存开销更低。
(4)在学长的博客中提到一个很重要的原因:算法固然美好,数据才是根本。理解数据对于设计算法的必要性。优化算法的演变历史,都是基于对数据的某种假设而进行的优化,那么某种算法是否有效,就要看你的数据是否符合该算法的胃口了。

并且其总结为:Adam之流虽然说已经简化了调参,但是并没有一劳永逸地解决问题,默认参数虽然好,但也不是放之四海而皆准。因此,在充分理解数据的基础上,依然需要根据数据特性、算法特性进行充分的调参实验,找到自己炼丹的最优解。而这个时候,不论是Adam,还是SGD,都已经不重要了。 

4. 5.

6.

附完整可运行代码

被优化函数为x^2的各类优化器2D可视化和拟合实验代码:

import copyfrom nndl import Op, Linear
import torch# =====将被优化函数实现为OptimizedFunction算子====================================================
class OptimizedFunction(Op):def __init__(self, w):super(OptimizedFunction, self).__init__()self.w = torch.as_tensor(w, dtype=torch.float32)self.params = {'x': torch.as_tensor(0, dtype=torch.float32)}self.grads = {'x': torch.as_tensor(0, dtype=torch.float32)}def forward(self, x):self.params['x'] = xreturn torch.matmul(self.w.T, torch.square(self.params['x']))def backward(self):self.grads['x'] = 2 * torch.multiply(self.w.T, self.params['x'])# =====训练函数 定义一个简易的训练函数,记录梯度下降过程中每轮的参数x和损失==============================================================
def train_f(model, optimizer, x_init, epoch):x = x_initall_x = []losses = []for i in range(epoch):all_x.append(copy.copy(x.numpy()))loss = model(x)losses.append(loss)model.backward()optimizer.step()x = model.params['x']return torch.as_tensor(all_x), losses# =======可视化函数===============================================
import numpy as np
import matplotlib.pyplot as pltclass Visualization(object):def __init__(self):x1 = np.arange(-5, 5, 0.1)x2 = np.arange(-5, 5, 0.1)x1, x2 = np.meshgrid(x1, x2)self.init_x = torch.as_tensor([x1, x2])def plot_2d(self, model, x, fig_name):fig, ax = plt.subplots(figsize=(10, 6))cp = ax.contourf(self.init_x[0], self.init_x[1], model(self.init_x.transpose(1, 0)),colors=['#e4007f', '#f19ec2', '#e86096', '#eb7aaa', '#f6c8dc', '#f5f5f5', '#000000'])c = ax.contour(self.init_x[0], self.init_x[1], model(self.init_x.transpose(1, 0)), colors='black')cbar = fig.colorbar(cp)ax.plot(x[:, 0], x[:, 1], '-o', color='#000000')ax.plot(0, 'r*', markersize=18, color='#fefefe')ax.set_xlabel('$x1$')ax.set_ylabel('$x2$')ax.set_xlim((-2, 5))ax.set_ylim((-2, 5))plt.savefig(fig_name)# =====调用train_f和Visualization,训练模型并可视化参数更新轨迹======================================
def train_and_plot_f(model, optimizer, epoch, fig_name):x_init = torch.as_tensor([3, 4], dtype=torch.float32)print('x1 initiate: {}, x2 initiate: {}'.format(x_init[0].numpy(), x_init[1].numpy()))x, losses = train_f(model, optimizer, x_init, epoch)losses = np.array(losses)# 展示x1、x2的更新轨迹vis = Visualization()vis.plot_2d(model, x, fig_name)# =========模型训练train函数================================================================
def train(data, num_epochs, batch_size, model, calculate_loss, optimizer, verbose=False):# 记录每个回合损失的变化epoch_loss = []# 记录每次迭代损失的变化iter_loss = []N = len(data)for epoch_id in range(num_epochs):# np.random.shuffle(data) #不再随机打乱数据# 将训练数据进行拆分,每个mini_batch包含batch_size条的数据mini_batches = [data[i:i+batch_size] for i in range(0, N, batch_size)]for iter_id, mini_batch in enumerate(mini_batches):# data中前两个分量为Xinputs = mini_batch[:, :-1]# data中最后一个分量为ylabels = mini_batch[:, -1:]# 前向计算outputs = model(inputs)# 计算损失loss = calculate_loss(outputs, labels).numpy()# 计算梯度model.backward(labels)# 梯度更新optimizer.step()iter_loss.append(loss)# verbose = True 则打印当前回合的损失if verbose:print('Epoch {:3d}, loss = {:.4f}'.format(epoch_id, np.mean(iter_loss)))epoch_loss.append(np.mean(iter_loss))return iter_loss, epoch_loss# =====优化过程可视化 定义plot_loss函数=================================================
def plot_loss(iter_loss, epoch_loss, fig_name):"""可视化损失函数的变化趋势"""plt.figure(figsize=(10, 4))ax1 = plt.subplot(121)ax1.plot(iter_loss, color='#e4007f')plt.title('iteration loss')ax2 = plt.subplot(122)ax2.plot(epoch_loss, color='#f19ec2')plt.title('epoch loss')plt.savefig(fig_name)plt.show()# ======训练并展示每个回合和每次迭代的损失变化情况=================================================
import torch.nn as nn
def train_and_plot(optimizer, fig_name):"""训练网络并画出损失函数的变化趋势输入:- optimizer:优化器"""# 定义均方差损失mse = nn.MSELoss()iter_loss, epoch_loss = train(data, num_epochs=30, batch_size=64, model=model, calculate_loss=mse, optimizer=optimizer)plot_loss(iter_loss, epoch_loss, fig_name)# =====模型训练与可视化:================================
# ###SGD=============================================
from nndl import SimpleBatchGD# 固定随机种子
torch.seed()
w = torch.as_tensor([0.2, 2])
model = OptimizedFunction(w)
opt = SimpleBatchGD(init_lr=0.2, model=model)
train_and_plot_f(model, opt, epoch=40, fig_name='opti-vis-para.pdf')# ##拟合=======
# 固定随机种子
torch.seed()
# 随机生成shape为(1000,2)的训练数据
X = torch.randn([1000, 2])
w = torch.as_tensor([0.5, 0.8])
w = torch.unsqueeze(w, dim=1)
noise = 0.01 * torch.rand([1000])
noise = torch.unsqueeze(noise, dim=1)
# 计算y
y = torch.matmul(X, w) + noise
# 打印X, y样本
print('X: ', X[0].numpy())
print('y: ', y[0].numpy())# X,y组成训练样本数据
data = torch.concat((X, y), dim=1)
print('input data shape: ', data.shape)
print('data: ', data[0].numpy())# 固定随机种子
torch.seed()
# 定义网络结构
model = Linear(2)
# 定义优化器
opt = SimpleBatchGD(init_lr=0.01, model=model)
train_and_plot(opt, 'opti-loss.pdf')# ###=====AdaGrad================================
from nndl import Optimizer
class Adagrad(Optimizer):def __init__(self, init_lr, model, epsilon):super(Adagrad, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.epsilon = epsilondef adagrad(self, x, gradient_x, G, init_lr):G += gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):for key in self.model.params.keys():self.model.params[key], self.G[key] = self.adagrad(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)# 固定随机种子
torch.seed()
w = torch.as_tensor([0.2, 2])
model2 = OptimizedFunction(w)
opt2 = Adagrad(init_lr=0.5, model=model2, epsilon=1e-7)
train_and_plot_f(model2, opt2, epoch=60, fig_name='opti-vis-para2.pdf')# ##拟合=======
# 固定随机种子
torch.seed()
# 定义网络结构
model = Linear(2)
# 定义优化器
opt = Adagrad(init_lr=0.1, model=model, epsilon=1e-7)
train_and_plot(opt, 'opti-loss2.pdf')# ###PMSprop======================================
class RMSprop(Optimizer):def __init__(self, init_lr, model, beta, epsilon):super(RMSprop, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.beta = betaself.epsilon = epsilondef rmsprop(self, x, gradient_x, G, init_lr):"""rmsprop算法更新参数,G为迭代梯度平方的加权移动平均"""G = self.beta * G + (1 - self.beta) * gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.rmsprop(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)torch.seed()
w = torch.as_tensor([0.2, 2])
model3 = OptimizedFunction(w)
opt3 = RMSprop(init_lr=0.1, model=model3, beta=0.9, epsilon=1e-7)
train_and_plot_f(model3, opt3, epoch=50, fig_name='opti-vis-para3.pdf')# ##拟合===========
# 固定随机种子
torch.seed()
# 定义网络结构
model = Linear(2)
# 定义优化器
opt3 = RMSprop(init_lr=0.1, model=model, beta=0.9, epsilon=1e-7)
train_and_plot(opt3, 'opti-loss3.pdf')# ###动量法=====================================================
class Momentum(Optimizer):def __init__(self, init_lr, model, rho):super(Momentum, self).__init__(init_lr=init_lr, model=model)self.delta_x = {}for key in self.model.params.keys():self.delta_x[key] = 0self.rho = rhodef momentum(self, x, gradient_x, delta_x, init_lr):"""momentum算法更新参数,delta_x为梯度的加权移动平均"""delta_x = self.rho * delta_x - init_lr * gradient_xx += delta_xreturn x, delta_xdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.delta_x[key] = self.momentum(self.model.params[key],self.model.grads[key],self.delta_x[key],self.init_lr)# 固定随机种子
torch.seed()
w = torch.as_tensor([0.2, 2])
model4 = OptimizedFunction(w)
opt4 = Momentum(init_lr=0.01, model=model4, rho=0.9)
train_and_plot_f(model4, opt4, epoch=70, fig_name='opti-vis-para4.pdf')# ##拟合==========
# 固定随机种子
torch.seed()# 定义网络结构
model = Linear(2)
# 定义优化器
opt = Momentum(init_lr=0.01, model=model, rho=0.9)
train_and_plot(opt, 'opti-loss4.pdf')# ###Adam=========================================class Adam(Optimizer):def __init__(self, init_lr, model, beta1, beta2, epsilon):super(Adam, self).__init__(init_lr=init_lr, model=model)self.beta1 = beta1self.beta2 = beta2self.epsilon = epsilonself.M, self.G = {}, {}for key, param in self.model.params.items():self.M[key] = torch.zeros_like(param)  # 确保与参数形状一致self.G[key] = torch.zeros_like(param)  # 确保与参数形状一致self.t = 1def adam(self, x, gradient_x, G, M, t, init_lr):M = self.beta1 * M + (1 - self.beta1) * gradient_xG = self.beta2 * G + (1 - self.beta2) * gradient_x ** 2M_hat = M / (1 - self.beta1 ** t)G_hat = G / (1 - self.beta2 ** t)t += 1x -= init_lr / (torch.sqrt(G_hat) + self.epsilon) * M_hatreturn x, G, M, tdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key], self.M[key], self.t = self.adam(self.model.params[key],self.model.grads[key],self.G[key],self.M[key],self.t,self.init_lr)# 修复后的代码使用方式
torch.manual_seed(42)
w = torch.as_tensor([0.2, 2], dtype=torch.float32)
model5 = OptimizedFunction(w)
opt5 = Adam(init_lr=0.2, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)
train_and_plot_f(model5, opt5, epoch=40, fig_name='opti-vis-para5.pdf')torch.manual_seed(42)
model = Linear(2)
opt5 = Adam(init_lr=0.1, model=model, beta1=0.9, beta2=0.99, epsilon=1e-7)
train_and_plot(opt5, 'opti-loss5.pdf')

nndl.py:

import math
from abc import abstractmethodimport numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F# 定义基础操作类
class Op(object):def __init__(self):passdef __call__(self, inputs):return self.forward(torch.as_tensor(inputs, dtype=torch.float32))def forward(self, inputs):raise NotImplementedErrordef backward(self, inputs):raise NotImplementedError# 优化器基类
class Optimizer(object):def __init__(self, init_lr, model):self.init_lr = init_lr# 指定优化器需要优化的模型self.model = model@abstractmethoddef step(self):passclass SimpleBatchGD(Optimizer):def __init__(self, init_lr, model):super(SimpleBatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):# 参数更新if isinstance(self.model.params, dict):for key in self.model.params.keys():self.model.params[key] = self.model.params[key] - self.init_lr * self.model.grads[key]# 实现线性层算子
class Linear(Op):def __init__(self, input_size,  weight_init=np.random.standard_normal, bias_init=torch.zeros):self.params = {}self.params['W'] = weight_init([input_size, 1])self.params['W'] = torch.as_tensor(self.params['W'],dtype=torch.float32)self.params['b'] = bias_init([1])self.inputs = Noneself.grads = {}def forward(self, inputs):self.inputs = inputsself.outputs = torch.matmul(self.inputs, self.params['W']) + self.params['b']return self.outputsdef backward(self, labels):K = self.inputs.shape[0]self.grads['W'] = 1./ K*torch.matmul(self.inputs.T, (self.outputs - labels))self.grads['b'] = 1./K* torch.sum(self.outputs-labels, dim=0)# 实现 Logistic 激活函数
class Logistic(Op):def __init__(self):super().__init__()self.inputs = Noneself.outputs = Noneself.params = Nonedef forward(self, inputs):outputs = 1.0 / (1.0 + torch.exp(-inputs))self.outputs = outputsreturn outputsdef backward(self, grads):# 计算Logistic激活函数对输入的导数outputs_grad_inputs = torch.multiply(self.outputs, (1.0 - self.outputs))return torch.multiply(grads, outputs_grad_inputs)# 定义多层感知机模型(MLP)带L2正则化
# 实现一个两层前馈神经网络
class Model_MLP_L2(Op):def __init__(self, input_size, hidden_size, output_size):# 线性层super().__init__()self.fc1 = Linear(input_size, hidden_size, name="fc1")# Logistic激活函数层self.act_fn1 = Logistic()self.fc2 = Linear(hidden_size, output_size, name="fc2")self.act_fn2 = Logistic()self.layers = [self.fc1, self.act_fn1, self.fc2, self.act_fn2]def __call__(self, X):return self.forward(X)# 前向计算def forward(self, X):z1 = self.fc1(X)a1 = self.act_fn1(z1)z2 = self.fc2(a1)a2 = self.act_fn2(z2)return a2# 反向计算def backward(self, loss_grad_a2):loss_grad_z2 = self.act_fn2.backward(loss_grad_a2)loss_grad_a1 = self.fc2.backward(loss_grad_z2)loss_grad_z1 = self.act_fn1.backward(loss_grad_a1)loss_grad_inputs = self.fc1.backward(loss_grad_z1)# 实现交叉熵损失函数
class BinaryCrossEntropyLoss(Op):def __init__(self, model):super().__init__()self.predicts = Noneself.labels = Noneself.num = Noneself.model = modeldef __call__(self, predicts, labels):return self.forward(predicts, labels)def forward(self, predicts, labels):self.predicts = predictsself.labels = labelsself.num = self.predicts.shape[0]loss = -1. / self.num * (torch.matmul(self.labels.t(), torch.log(self.predicts))+ torch.matmul((1 - self.labels.t()), torch.log(1 - self.predicts)))loss = torch.squeeze(loss, axis=1)return lossdef backward(self):# 计算损失函数对模型预测的导数loss_grad_predicts = -1.0 * (self.labels / self.predicts -(1 - self.labels) / (1 - self.predicts)) / self.num# 梯度反向传播self.model.backward(loss_grad_predicts)def make_moons(n_samples=1000, shuffle=True, noise=None):n_samples_out = n_samples // 2n_samples_in = n_samples - n_samples_outouter_circ_x = torch.cos(torch.linspace(0, math.pi, n_samples_out))outer_circ_y = torch.sin(torch.linspace(0, math.pi, n_samples_out))inner_circ_x = 1 - torch.cos(torch.linspace(0, math.pi, n_samples_in))inner_circ_y = 0.5 - torch.sin(torch.linspace(0, math.pi, n_samples_in))print('outer_circ_x.shape:', outer_circ_x.shape, 'outer_circ_y.shape:', outer_circ_y.shape)print('inner_circ_x.shape:', inner_circ_x.shape, 'inner_circ_y.shape:', inner_circ_y.shape)X = torch.stack([torch.cat([outer_circ_x, inner_circ_x]),torch.cat([outer_circ_y, inner_circ_y])],axis=1)print('after concat shape:', torch.cat([outer_circ_x, inner_circ_x]).shape)print('X shape:', X.shape)# 使用'torch. zeros'将第一类数据的标签全部设置为0# 使用'torch. ones'将第一类数据的标签全部设置为1y = torch.cat([torch.zeros([n_samples_out]), torch.ones([n_samples_in])])print('y shape:', y.shape)# 如果shuffle为True,将所有数据打乱if shuffle:# 使用'torch.randperm'生成一个数值在0到X.shape[0],随机排列的一维Tensor做索引值,用于打乱数据idx = torch.randperm(X.shape[0])X = X[idx]y = y[idx]# 如果noise不为None,则给特征值加入噪声if noise is not None:X += np.random.normal(0.0, noise, X.shape)return X, y# 新增优化器基类
class Optimizer(object):def __init__(self, init_lr, model):# 初始化学习率,用于参数更新的计算self.init_lr = init_lr# 指定优化器需要优化的模型self.model = model@abstractmethoddef step(self):passclass BatchGD(Optimizer):def __init__(self, init_lr, model):super(BatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):# 参数更新for layer in self.model.layers:  # 遍历所有层if isinstance(layer.params, dict):for key in layer.params.keys():layer.params[key] = layer.params[key] - self.init_lr * layer.grads[key]

 被优化函数为\frac{1}{20}x^2+y^2的2D可视化代码:

# coding: utf-8
import numpy as np
import matplotlib.pyplot as plt
from collections import OrderedDictclass SGD:"""随机梯度下降法(Stochastic Gradient Descent)"""def __init__(self, lr=0.01):self.lr = lrdef update(self, params, grads):for key in params.keys():params[key] -= self.lr * grads[key]class Momentum:"""Momentum SGD"""def __init__(self, lr=0.01, momentum=0.9):self.lr = lrself.momentum = momentumself.v = Nonedef update(self, params, grads):if self.v is None:self.v = {}for key, val in params.items():self.v[key] = np.zeros_like(val)for key in params.keys():self.v[key] = self.momentum * self.v[key] - self.lr * grads[key]params[key] += self.v[key]class Nesterov:"""Nesterov's Accelerated Gradient (http://arxiv.org/abs/1212.0901)"""def __init__(self, lr=0.01, momentum=0.9):self.lr = lrself.momentum = momentumself.v = Nonedef update(self, params, grads):if self.v is None:self.v = {}for key, val in params.items():self.v[key] = np.zeros_like(val)for key in params.keys():self.v[key] *= self.momentumself.v[key] -= self.lr * grads[key]params[key] += self.momentum * self.momentum * self.v[key]params[key] -= (1 + self.momentum) * self.lr * grads[key]class AdaGrad:"""AdaGrad"""def __init__(self, lr=0.01):self.lr = lrself.h = Nonedef update(self, params, grads):if self.h is None:self.h = {}for key, val in params.items():self.h[key] = np.zeros_like(val)for key in params.keys():self.h[key] += grads[key] * grads[key]params[key] -= self.lr * grads[key] / (np.sqrt(self.h[key]) + 1e-7)class RMSprop:"""RMSprop"""def __init__(self, lr=0.01, decay_rate=0.99):self.lr = lrself.decay_rate = decay_rateself.h = Nonedef update(self, params, grads):if self.h is None:self.h = {}for key, val in params.items():self.h[key] = np.zeros_like(val)for key in params.keys():self.h[key] *= self.decay_rateself.h[key] += (1 - self.decay_rate) * grads[key] * grads[key]params[key] -= self.lr * grads[key] / (np.sqrt(self.h[key]) + 1e-7)class Adam:"""Adam (http://arxiv.org/abs/1412.6980v8)"""def __init__(self, lr=0.001, beta1=0.9, beta2=0.999):self.lr = lrself.beta1 = beta1self.beta2 = beta2self.iter = 0self.m = Noneself.v = Nonedef update(self, params, grads):if self.m is None:self.m, self.v = {}, {}for key, val in params.items():self.m[key] = np.zeros_like(val)self.v[key] = np.zeros_like(val)self.iter += 1lr_t = self.lr * np.sqrt(1.0 - self.beta2 ** self.iter) / (1.0 - self.beta1 ** self.iter)for key in params.keys():self.m[key] += (1 - self.beta1) * (grads[key] - self.m[key])self.v[key] += (1 - self.beta2) * (grads[key] ** 2 - self.v[key])params[key] -= lr_t * self.m[key] / (np.sqrt(self.v[key]) + 1e-7)def f(x, y):return x ** 2 / 20.0 + y ** 2def df(x, y):return x / 10.0, 2.0 * yinit_pos = (-7.0, 2.0)
params = {}
params['x'], params['y'] = init_pos[0], init_pos[1]
grads = {}
grads['x'], grads['y'] = 0, 0optimizers = OrderedDict()
optimizers["SGD"] = SGD(lr=0.95)
optimizers["Momentum"] = Momentum(lr=0.1)
optimizers["AdaGrad"] = AdaGrad(lr=1.5)
optimizers["Adam"] = Adam(lr=0.3)idx = 1for key in optimizers:optimizer = optimizers[key]x_history = []y_history = []params['x'], params['y'] = init_pos[0], init_pos[1]for i in range(30):x_history.append(params['x'])y_history.append(params['y'])grads['x'], grads['y'] = df(params['x'], params['y'])optimizer.update(params, grads)x = np.arange(-10, 10, 0.01)y = np.arange(-5, 5, 0.01)X, Y = np.meshgrid(x, y)Z = f(X, Y)# for simple contour linemask = Z > 7Z[mask] = 0# plotplt.subplot(2, 2, idx)idx += 1plt.plot(x_history, y_history, 'o-', color="red")plt.contour(X, Y, Z)  # 绘制等高线plt.ylim(-10, 10)plt.xlim(-10, 10)plt.plot(0, 0, '+')plt.title(key)plt.xlabel("x")plt.ylabel("y")plt.subplots_adjust(wspace=0, hspace=0)  # 调整子图间距
plt.show()

 被优化函数为f(x)={x[0]}^2+{x[1]}^2+{x[1]}^3+x[0]*x[1]的3D图代码:

import torch
import numpy as np
import copy
from matplotlib import pyplot as plt
from matplotlib import animation
from itertools import zip_longestclass Op(object):def __init__(self):passdef __call__(self, inputs):return self.forward(inputs)# 输入:张量inputs# 输出:张量outputsdef forward(self, inputs):# return outputsraise NotImplementedError# 输入:最终输出对outputs的梯度outputs_grads# 输出:最终输出对inputs的梯度inputs_gradsdef backward(self, outputs_grads):# return inputs_gradsraise NotImplementedErrorclass Optimizer(object):  # 优化器基类def __init__(self, init_lr, model):"""优化器类初始化"""# 初始化学习率,用于参数更新的计算self.init_lr = init_lr# 指定优化器需要优化的模型self.model = modeldef step(self):"""定义每次迭代如何更新参数"""passclass SimpleBatchGD(Optimizer):def __init__(self, init_lr, model):super(SimpleBatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):# 参数更新if isinstance(self.model.params, dict):for key in self.model.params.keys():self.model.params[key] = self.model.params[key] - self.init_lr * self.model.grads[key]class Adagrad(Optimizer):def __init__(self, init_lr, model, epsilon):"""Adagrad 优化器初始化输入:- init_lr: 初始学习率 - model:模型,model.params存储模型参数值  - epsilon:保持数值稳定性而设置的非常小的常数"""super(Adagrad, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.epsilon = epsilondef adagrad(self, x, gradient_x, G, init_lr):"""adagrad算法更新参数,G为参数梯度平方的累计值。"""G += gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.adagrad(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class RMSprop(Optimizer):def __init__(self, init_lr, model, beta, epsilon):"""RMSprop优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta:衰减率- epsilon:保持数值稳定性而设置的常数"""super(RMSprop, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.beta = betaself.epsilon = epsilondef rmsprop(self, x, gradient_x, G, init_lr):"""rmsprop算法更新参数,G为迭代梯度平方的加权移动平均"""G = self.beta * G + (1 - self.beta) * gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.rmsprop(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class Momentum(Optimizer):def __init__(self, init_lr, model, rho):"""Momentum优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- rho:动量因子"""super(Momentum, self).__init__(init_lr=init_lr, model=model)self.delta_x = {}for key in self.model.params.keys():self.delta_x[key] = 0self.rho = rhodef momentum(self, x, gradient_x, delta_x, init_lr):"""momentum算法更新参数,delta_x为梯度的加权移动平均"""delta_x = self.rho * delta_x - init_lr * gradient_xx += delta_xreturn x, delta_xdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.delta_x[key] = self.momentum(self.model.params[key],self.model.grads[key],self.delta_x[key],self.init_lr)class Adam(Optimizer):def __init__(self, init_lr, model, beta1, beta2, epsilon):"""Adam优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta1, beta2:移动平均的衰减率- epsilon:保持数值稳定性而设置的常数"""super(Adam, self).__init__(init_lr=init_lr, model=model)self.beta1 = beta1self.beta2 = beta2self.epsilon = epsilonself.M, self.G = {}, {}for key in self.model.params.keys():self.M[key] = 0self.G[key] = 0self.t = 1def adam(self, x, gradient_x, G, M, t, init_lr):"""adam算法更新参数输入:- x:参数- G:梯度平方的加权移动平均- M:梯度的加权移动平均- t:迭代次数- init_lr:初始学习率"""M = self.beta1 * M + (1 - self.beta1) * gradient_xG = self.beta2 * G + (1 - self.beta2) * gradient_x ** 2M_hat = M / (1 - self.beta1 ** t)G_hat = G / (1 - self.beta2 ** t)t += 1x -= init_lr / torch.sqrt(G_hat + self.epsilon) * M_hatreturn x, G, M, tdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key], self.M[key], self.t = self.adam(self.model.params[key],self.model.grads[key],self.G[key],self.M[key],self.t,self.init_lr)class OptimizedFunction3D(Op):def __init__(self):super(OptimizedFunction3D, self).__init__()self.params = {'x': 0}self.grads = {'x': 0}def forward(self, x):self.params['x'] = xreturn x[0] ** 2 + x[1] ** 2 + x[1] ** 3 + x[0] * x[1]def backward(self):x = self.params['x']gradient1 = 2 * x[0] + x[1]gradient2 = 2 * x[1] + 3 * x[1] ** 2 + x[0]grad1 = torch.Tensor([gradient1])grad2 = torch.Tensor([gradient2])self.grads['x'] = torch.cat([grad1, grad2])class Visualization3D(animation.FuncAnimation):"""    绘制动态图像,可视化参数更新轨迹    """def __init__(self, *xy_values, z_values, labels=[], colors=[], fig, ax, interval=600, blit=True, **kwargs):"""初始化3d可视化类输入:xy_values:三维中x,y维度的值z_values:三维中z维度的值labels:每个参数更新轨迹的标签colors:每个轨迹的颜色interval:帧之间的延迟(以毫秒为单位)blit:是否优化绘图"""self.fig = figself.ax = axself.xy_values = xy_valuesself.z_values = z_valuesframes = max(xy_value.shape[0] for xy_value in xy_values)self.lines = [ax.plot([], [], [], label=label, color=color, lw=2)[0]for _, label, color in zip_longest(xy_values, labels, colors)]super(Visualization3D, self).__init__(fig, self.animate, init_func=self.init_animation, frames=frames,interval=interval, blit=blit, **kwargs)def init_animation(self):# 数值初始化for line in self.lines:line.set_data([], [])# line.set_3d_properties(np.asarray([]))  # 源程序中有这一行,加上会报错。 Edit by David 2022.12.4return self.linesdef animate(self, i):# 将x,y,z三个数据传入,绘制三维图像for line, xy_value, z_value in zip(self.lines, self.xy_values, self.z_values):line.set_data(xy_value[:i, 0], xy_value[:i, 1])line.set_3d_properties(z_value[:i])return self.linesdef train_f(model, optimizer, x_init, epoch):x = x_initall_x = []losses = []for i in range(epoch):all_x.append(copy.copy(x.numpy()))  # 浅拷贝 改为 深拷贝, 否则List的原值会被改变。 Edit by David 2022.12.4.loss = model(x)losses.append(loss)model.backward()optimizer.step()x = model.params['x']return torch.Tensor(np.array(all_x)), losses# 构建5个模型,分别配备不同的优化器
model1 = OptimizedFunction3D()
opt_gd = SimpleBatchGD(init_lr=0.01, model=model1)model2 = OptimizedFunction3D()
opt_adagrad = Adagrad(init_lr=0.5, model=model2, epsilon=1e-7)model3 = OptimizedFunction3D()
opt_rmsprop = RMSprop(init_lr=0.1, model=model3, beta=0.9, epsilon=1e-7)model4 = OptimizedFunction3D()
opt_momentum = Momentum(init_lr=0.01, model=model4, rho=0.9)model5 = OptimizedFunction3D()
opt_adam = Adam(init_lr=0.1, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)models = [model1, model2, model3, model4, model5]
opts = [opt_gd, opt_adagrad, opt_rmsprop, opt_momentum, opt_adam]x_all_opts = []
z_all_opts = []# 使用不同优化器训练for model, opt in zip(models, opts):x_init = torch.FloatTensor([2, 3])x_one_opt, z_one_opt = train_f(model, opt, x_init, 150)  # epoch# 保存参数值x_all_opts.append(x_one_opt.numpy())z_all_opts.append(np.squeeze(z_one_opt))# 使用numpy.meshgrid生成x1,x2矩阵,矩阵的每一行为[-3, 3],以0.1为间隔的数值
x1 = np.arange(-3, 3, 0.1)
x2 = np.arange(-3, 3, 0.1)
x1, x2 = np.meshgrid(x1, x2)
init_x = torch.Tensor(np.array([x1, x2]))model = OptimizedFunction3D()# 绘制 f_3d函数 的 三维图像
fig = plt.figure()
ax = plt.axes(projection='3d')
X = init_x[0].numpy()
Y = init_x[1].numpy()
Z = model(init_x).numpy()  # 改为 model(init_x).numpy() David 2022.12.4
ax.plot_surface(X, Y, Z, cmap='rainbow')ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('f(x1,x2)')labels = ['SGD', 'AdaGrad', 'RMSprop', 'Momentum', 'Adam']
colors = ['#f6373c', '#f6f237', '#45f637', '#37f0f6', '#000000']animator = Visualization3D(*x_all_opts, z_values=z_all_opts, labels=labels, colors=colors, fig=fig, ax=ax)
ax.legend(loc='upper left')plt.show()

 被优化函数为f(x)=\frac{1}{2}{x[0]}^2+{x[1]}^2的3D可视化图:

import torch
import numpy as np
import copy
from matplotlib import pyplot as plt
from matplotlib import animation
from itertools import zip_longest
from matplotlib import cmclass Op(object):def __init__(self):passdef __call__(self, inputs):return self.forward(inputs)# 输入:张量inputs# 输出:张量outputsdef forward(self, inputs):# return outputsraise NotImplementedError# 输入:最终输出对outputs的梯度outputs_grads# 输出:最终输出对inputs的梯度inputs_gradsdef backward(self, outputs_grads):# return inputs_gradsraise NotImplementedErrorclass Optimizer(object):  # 优化器基类def __init__(self, init_lr, model):"""优化器类初始化"""# 初始化学习率,用于参数更新的计算self.init_lr = init_lr# 指定优化器需要优化的模型self.model = modeldef step(self):"""定义每次迭代如何更新参数"""passclass SimpleBatchGD(Optimizer):def __init__(self, init_lr, model):super(SimpleBatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):# 参数更新if isinstance(self.model.params, dict):for key in self.model.params.keys():self.model.params[key] = self.model.params[key] - self.init_lr * self.model.grads[key]class Adagrad(Optimizer):def __init__(self, init_lr, model, epsilon):"""Adagrad 优化器初始化输入:- init_lr: 初始学习率 - model:模型,model.params存储模型参数值  - epsilon:保持数值稳定性而设置的非常小的常数"""super(Adagrad, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.epsilon = epsilondef adagrad(self, x, gradient_x, G, init_lr):"""adagrad算法更新参数,G为参数梯度平方的累计值。"""G += gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.adagrad(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class RMSprop(Optimizer):def __init__(self, init_lr, model, beta, epsilon):"""RMSprop优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta:衰减率- epsilon:保持数值稳定性而设置的常数"""super(RMSprop, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.beta = betaself.epsilon = epsilondef rmsprop(self, x, gradient_x, G, init_lr):"""rmsprop算法更新参数,G为迭代梯度平方的加权移动平均"""G = self.beta * G + (1 - self.beta) * gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.rmsprop(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class Momentum(Optimizer):def __init__(self, init_lr, model, rho):"""Momentum优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- rho:动量因子"""super(Momentum, self).__init__(init_lr=init_lr, model=model)self.delta_x = {}for key in self.model.params.keys():self.delta_x[key] = 0self.rho = rhodef momentum(self, x, gradient_x, delta_x, init_lr):"""momentum算法更新参数,delta_x为梯度的加权移动平均"""delta_x = self.rho * delta_x - init_lr * gradient_xx += delta_xreturn x, delta_xdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.delta_x[key] = self.momentum(self.model.params[key],self.model.grads[key],self.delta_x[key],self.init_lr)class Adam(Optimizer):def __init__(self, init_lr, model, beta1, beta2, epsilon):"""Adam优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta1, beta2:移动平均的衰减率- epsilon:保持数值稳定性而设置的常数"""super(Adam, self).__init__(init_lr=init_lr, model=model)self.beta1 = beta1self.beta2 = beta2self.epsilon = epsilonself.M, self.G = {}, {}for key in self.model.params.keys():self.M[key] = 0self.G[key] = 0self.t = 1def adam(self, x, gradient_x, G, M, t, init_lr):"""adam算法更新参数输入:- x:参数- G:梯度平方的加权移动平均- M:梯度的加权移动平均- t:迭代次数- init_lr:初始学习率"""M = self.beta1 * M + (1 - self.beta1) * gradient_xG = self.beta2 * G + (1 - self.beta2) * gradient_x ** 2M_hat = M / (1 - self.beta1 ** t)G_hat = G / (1 - self.beta2 ** t)t += 1x -= init_lr / torch.sqrt(G_hat + self.epsilon) * M_hatreturn x, G, M, tdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key], self.M[key], self.t = self.adam(self.model.params[key],self.model.grads[key],self.G[key],self.M[key],self.t,self.init_lr)class OptimizedFunction3D(Op):def __init__(self):super(OptimizedFunction3D, self).__init__()self.params = {'x': 0}self.grads = {'x': 0}def forward(self, x):self.params['x'] = xreturn - x[0] * x[0] / 2 + x[1] * x[1] / 1  # x[0] ** 2 + x[1] ** 2 + x[1] ** 3 + x[0] * x[1]def backward(self):x = self.params['x']gradient1 = - 2 * x[0] / 2gradient2 = 2 * x[1] / 1grad1 = torch.Tensor([gradient1])grad2 = torch.Tensor([gradient2])self.grads['x'] = torch.cat([grad1, grad2])class Visualization3D(animation.FuncAnimation):"""    绘制动态图像,可视化参数更新轨迹    """def __init__(self, *xy_values, z_values, labels=[], colors=[], fig, ax, interval=100, blit=True, **kwargs):"""初始化3d可视化类输入:xy_values:三维中x,y维度的值z_values:三维中z维度的值labels:每个参数更新轨迹的标签colors:每个轨迹的颜色interval:帧之间的延迟(以毫秒为单位)blit:是否优化绘图"""self.fig = figself.ax = axself.xy_values = xy_valuesself.z_values = z_valuesframes = max(xy_value.shape[0] for xy_value in xy_values)# , marker = 'o'self.lines = [ax.plot([], [], [], label=label, color=color, lw=2)[0]for _, label, color in zip_longest(xy_values, labels, colors)]print(self.lines)super(Visualization3D, self).__init__(fig, self.animate, init_func=self.init_animation, frames=frames,interval=interval, blit=blit, **kwargs)def init_animation(self):# 数值初始化for line in self.lines:line.set_data([], [])# line.set_3d_properties(np.asarray([]))  # 源程序中有这一行,加上会报错。 Edit by David 2022.12.4return self.linesdef animate(self, i):# 将x,y,z三个数据传入,绘制三维图像for line, xy_value, z_value in zip(self.lines, self.xy_values, self.z_values):line.set_data(xy_value[:i, 0], xy_value[:i, 1])line.set_3d_properties(z_value[:i])return self.linesdef train_f(model, optimizer, x_init, epoch):x = x_initall_x = []losses = []for i in range(epoch):all_x.append(copy.deepcopy(x.numpy()))  # 浅拷贝 改为 深拷贝, 否则List的原值会被改变。 Edit by David 2022.12.4.loss = model(x)losses.append(loss)model.backward()optimizer.step()x = model.params['x']return torch.Tensor(np.array(all_x)), losses# 构建5个模型,分别配备不同的优化器
model1 = OptimizedFunction3D()
opt_gd = SimpleBatchGD(init_lr=0.05, model=model1)model2 = OptimizedFunction3D()
opt_adagrad = Adagrad(init_lr=0.05, model=model2, epsilon=1e-7)model3 = OptimizedFunction3D()
opt_rmsprop = RMSprop(init_lr=0.05, model=model3, beta=0.9, epsilon=1e-7)model4 = OptimizedFunction3D()
opt_momentum = Momentum(init_lr=0.05, model=model4, rho=0.9)model5 = OptimizedFunction3D()
opt_adam = Adam(init_lr=0.05, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)models = [model5, model2, model3, model4, model1]
opts = [opt_adam, opt_adagrad, opt_rmsprop, opt_momentum, opt_gd]x_all_opts = []
z_all_opts = []# 使用不同优化器训练for model, opt in zip(models, opts):x_init = torch.FloatTensor([0.00001, 0.5])x_one_opt, z_one_opt = train_f(model, opt, x_init, 100)  # epoch# 保存参数值x_all_opts.append(x_one_opt.numpy())z_all_opts.append(np.squeeze(z_one_opt))# 使用numpy.meshgrid生成x1,x2矩阵,矩阵的每一行为[-3, 3],以0.1为间隔的数值
x1 = np.arange(-1, 2, 0.01)
x2 = np.arange(-1, 1, 0.05)
x1, x2 = np.meshgrid(x1, x2)
init_x = torch.Tensor(np.array([x1, x2]))model = OptimizedFunction3D()# 绘制 f_3d函数 的 三维图像
fig = plt.figure()
ax = plt.axes(projection='3d')
X = init_x[0].numpy()
Y = init_x[1].numpy()
Z = model(init_x).numpy()  # 改为 model(init_x).numpy() David 2022.12.4
surf = ax.plot_surface(X, Y, Z, edgecolor='grey', cmap=cm.coolwarm)
# fig.colorbar(surf, shrink=0.5, aspect=1)
ax.set_zlim(-3, 2)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('f(x1,x2)')labels = ['Adam', 'AdaGrad', 'RMSprop', 'Momentum', 'SGD']
colors = ['#8B0000', '#0000FF', '#000000', '#008B00', '#FF0000']animator = Visualization3D(*x_all_opts, z_values=z_all_opts, labels=labels, colors=colors, fig=fig, ax=ax)
ax.legend(loc='upper right')plt.show()

被优化函数为\frac{1}{20}x^2+y^2的3D可视化图(期待改进):

import animator
import torch
import numpy as np
import copy
from matplotlib import pyplot as plt
from matplotlib import animation
from itertools import zip_longest
from matplotlib import cm
from matplotlib.animation import FuncAnimationclass Op(object):def __init__(self):passdef __call__(self, inputs):return self.forward(inputs)# 输入:张量inputs# 输出:张量outputsdef forward(self, inputs):# return outputsraise NotImplementedError# 输入:最终输出对outputs的梯度outputs_grads# 输出:最终输出对inputs的梯度inputs_gradsdef backward(self, outputs_grads):# return inputs_gradsraise NotImplementedErrorclass Optimizer(object):  # 优化器基类def __init__(self, init_lr, model):"""优化器类初始化"""# 初始化学习率,用于参数更新的计算self.init_lr = init_lr# 指定优化器需要优化的模型self.model = modeldef step(self):"""定义每次迭代如何更新参数"""passclass SimpleBatchGD(Optimizer):def __init__(self, init_lr, model):super(SimpleBatchGD, self).__init__(init_lr=init_lr, model=model)def step(self):# 参数更新if isinstance(self.model.params, dict):for key in self.model.params.keys():self.model.params[key] = self.model.params[key] - self.init_lr * self.model.grads[key]class Adagrad(Optimizer):def __init__(self, init_lr, model, epsilon):"""Adagrad 优化器初始化输入:- init_lr: 初始学习率 - model:模型,model.params存储模型参数值  - epsilon:保持数值稳定性而设置的非常小的常数"""super(Adagrad, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.epsilon = epsilondef adagrad(self, x, gradient_x, G, init_lr):"""adagrad算法更新参数,G为参数梯度平方的累计值。"""G += gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.adagrad(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class RMSprop(Optimizer):def __init__(self, init_lr, model, beta, epsilon):"""RMSprop优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta:衰减率- epsilon:保持数值稳定性而设置的常数"""super(RMSprop, self).__init__(init_lr=init_lr, model=model)self.G = {}for key in self.model.params.keys():self.G[key] = 0self.beta = betaself.epsilon = epsilondef rmsprop(self, x, gradient_x, G, init_lr):"""rmsprop算法更新参数,G为迭代梯度平方的加权移动平均"""G = self.beta * G + (1 - self.beta) * gradient_x ** 2x -= init_lr / torch.sqrt(G + self.epsilon) * gradient_xreturn x, Gdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key] = self.rmsprop(self.model.params[key],self.model.grads[key],self.G[key],self.init_lr)class Momentum(Optimizer):def __init__(self, init_lr, model, rho):"""Momentum优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- rho:动量因子"""super(Momentum, self).__init__(init_lr=init_lr, model=model)self.delta_x = {}for key in self.model.params.keys():self.delta_x[key] = 0self.rho = rhodef momentum(self, x, gradient_x, delta_x, init_lr):"""momentum算法更新参数,delta_x为梯度的加权移动平均"""delta_x = self.rho * delta_x - init_lr * gradient_xx += delta_xreturn x, delta_xdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.delta_x[key] = self.momentum(self.model.params[key],self.model.grads[key],self.delta_x[key],self.init_lr)class Adam(Optimizer):def __init__(self, init_lr, model, beta1, beta2, epsilon):"""Adam优化器初始化输入:- init_lr:初始学习率- model:模型,model.params存储模型参数值- beta1, beta2:移动平均的衰减率- epsilon:保持数值稳定性而设置的常数"""super(Adam, self).__init__(init_lr=init_lr, model=model)self.beta1 = beta1self.beta2 = beta2self.epsilon = epsilonself.M, self.G = {}, {}for key in self.model.params.keys():self.M[key] = 0self.G[key] = 0self.t = 1def adam(self, x, gradient_x, G, M, t, init_lr):"""adam算法更新参数输入:- x:参数- G:梯度平方的加权移动平均- M:梯度的加权移动平均- t:迭代次数- init_lr:初始学习率"""M = self.beta1 * M + (1 - self.beta1) * gradient_xG = self.beta2 * G + (1 - self.beta2) * gradient_x ** 2M_hat = M / (1 - self.beta1 ** t)G_hat = G / (1 - self.beta2 ** t)t += 1x -= init_lr / torch.sqrt(G_hat + self.epsilon) * M_hatreturn x, G, M, tdef step(self):"""参数更新"""for key in self.model.params.keys():self.model.params[key], self.G[key], self.M[key], self.t = self.adam(self.model.params[key],self.model.grads[key],self.G[key],self.M[key],self.t,self.init_lr)class OptimizedFunction3D(Op):def __init__(self):super(OptimizedFunction3D, self).__init__()self.params = {'x': 0}self.grads = {'x': 0}def forward(self, x):self.params['x'] = xreturn x[0] * x[0] / 20 + x[1] * x[1] / 1  # x[0] ** 2 + x[1] ** 2 + x[1] ** 3 + x[0] * x[1]def backward(self):x = self.params['x']gradient1 = 2 * x[0] / 20gradient2 = 2 * x[1] / 1grad1 = torch.Tensor([gradient1])grad2 = torch.Tensor([gradient2])self.grads['x'] = torch.cat([grad1, grad2])class Visualization3D(animation.FuncAnimation):"""绘制动态图像,可视化参数更新轨迹"""def __init__(self, *xy_values, z_values, labels=[], colors=[], fig, ax, interval=100, blit=True, **kwargs):"""初始化3D可视化类输入:xy_values:三维中x,y维度的值z_values:三维中z维度的值labels:每个参数更新轨迹的标签colors:每个轨迹的颜色interval:帧之间的延迟(以毫秒为单位)blit:是否优化绘图"""self.fig = figself.ax = axself.xy_values = xy_valuesself.z_values = z_valuesself.surf = None  # 曲面图初始化为空frames = max(xy_value.shape[0] for xy_value in xy_values)self.lines = [ax.plot([], [], [], label=label, color=color, lw=2)[0]for _, label, color in zip_longest(xy_values, labels, colors)]self.points = [ax.plot([], [], [], color=color, markeredgewidth=1, markeredgecolor='black', marker='o')[0]for _, color in zip_longest(xy_values, colors)]super(Visualization3D, self).__init__(fig, self.animate, init_func=self.init_animation, frames=frames,interval=interval, blit=blit, **kwargs)def init_animation(self):# 数值初始化for line in self.lines:line.set_data([], [])for point in self.points:point.set_data([], [])if self.surf is not None:self.surf.remove()self.surf = None  # 确保清除之前的曲面return self.points + self.linesdef animate(self, i):try:print(f"Frame {i}")  # 打印当前帧号for line, xy_value, z_value in zip(self.lines, self.xy_values, self.z_values):line.set_data(xy_value[:i, 0], xy_value[:i, 1])line.set_3d_properties(z_value[:i])for point, xy_value, z_value in zip(self.points, self.xy_values, self.z_values):point.set_data(xy_value[i, 0], xy_value[i, 1])point.set_3d_properties(z_value[i])if self.surf is not None:self.surf.remove()  # 移除前一帧的曲面x = self.xy_values[0][:i, 0]y = self.xy_values[0][:i, 1]z = self.z_values[0][:i]X, Y = np.meshgrid(x, y)Z = np.reshape(z, (len(x), len(y)))self.surf = self.ax.plot_surface(X, Y, Z, color='gray', alpha=0.5, rstride=1, cstride=1)return self.points + self.lines + [self.surf]except Exception as e:print(f"Error in frame {i}: {e}")raisedef train_f(model, optimizer, x_init, epoch):x = x_initall_x = []losses = []for i in range(epoch):all_x.append(copy.deepcopy(x.numpy()))  # 浅拷贝 改为 深拷贝, 否则List的原值会被改变。 Edit by David 2022.12.4.loss = model(x)losses.append(loss)model.backward()optimizer.step()x = model.params['x']return torch.Tensor(np.array(all_x)), losses# 构建5个模型,分别配备不同的优化器
model1 = OptimizedFunction3D()
opt_gd = SimpleBatchGD(init_lr=0.95, model=model1)model2 = OptimizedFunction3D()
opt_adagrad = Adagrad(init_lr=1.5, model=model2, epsilon=1e-7)model3 = OptimizedFunction3D()
opt_rmsprop = RMSprop(init_lr=0.05, model=model3, beta=0.9, epsilon=1e-7)model4 = OptimizedFunction3D()
opt_momentum = Momentum(init_lr=0.1, model=model4, rho=0.9)model5 = OptimizedFunction3D()
opt_adam = Adam(init_lr=0.3, model=model5, beta1=0.9, beta2=0.99, epsilon=1e-7)models = [model1, model2, model3, model4, model5]
opts = [opt_gd, opt_adagrad, opt_rmsprop, opt_momentum, opt_adam]x_all_opts = []  # 存储每种优化器的参数轨迹
z_all_opts = []  # 存储每种优化器的函数值轨迹# 使用不同优化器训练
for model, opt in zip(models, opts):x_init = torch.FloatTensor([-7, 2])  # 初始值x_one_opt, z_one_opt = train_f(model, opt, x_init, 100)  # 训练 100 个 epoch# 保存参数值x_all_opts.append(x_one_opt.numpy())  # 转为 NumPy 数组,形状为 (T, 2)z_all_opts.append(np.squeeze(z_one_opt))  # 转为 NumPy 数组,形状为 (T,)# print("x_all_opts:", x_all_opts)
# print("z_all_opts:", z_all_opts)# 使用numpy.meshgrid生成x1,x2矩阵
x1 = np.arange(-10, 10, 0.01)
x2 = np.arange(-5, 5, 0.01)
x1, x2 = np.meshgrid(x1, x2)# 将x1和x2作为输入,堆叠成形状为 (2, M, N)
init_x = torch.Tensor(np.stack([x1, x2], axis=0))  # 形状为 (2, M, N)# 假设模型已经定义
model = OptimizedFunction3D()# 确保模型能处理形状为 (2, M, N) 的输入
Z = model(init_x)
if isinstance(Z, torch.Tensor):  # 如果是PyTorch张量,转换为NumPy数组Z = Z.detach().numpy()# 确保 Z 的形状和 x1, x2 一致
Z = Z.reshape(x1.shape)# 绘制三维表面
fig = plt.figure()
ax = plt.axes(projection='3d')
surf = ax.plot_surface(x1, x2, Z, cmap='viridis', edgecolor='none', alpha=0.6)# 设置标题和坐标轴标签
ax.set_title('Optimized Function 3D')
ax.set_xlabel('X1')
ax.set_ylabel('X2')
ax.set_zlabel('Z')# 添加颜色条
fig.colorbar(surf)# 每种优化器的训练轨迹
labels = ['SGD', 'Adagrad', 'RMSprop', 'Momentum', 'Adam']
colors = ['r', 'g', 'b', 'c', 'm']# 假设 x_all_opts 和 z_all_opts 是训练过程中收集的轨迹点
# x_all_opts[i] 是形状 (T, 2) 的数组,表示第 i 种优化器 T 步的 (x1, x2) 参数
# z_all_opts[i] 是形状 (T,) 的数组,表示每步对应的函数值 z
# x_all_opts = [...]  # 填入实际数据
# z_all_opts = [...]  # 填入实际数据# 初始化小球的位置
balls = []
for color in colors:ball, = ax.plot([], [], [], 'o', color=color, markersize=6)balls.append(ball)# 初始化小球和轨迹
scatters = []  # 保存小球的列表
lines = []  # 保存轨迹线的列表for i in range(len(x_all_opts)):scatter = ax.scatter(x_all_opts[i][0, 0],x_all_opts[i][0, 1],z_all_opts[i][0],color=colors[i],s=50,  # 小球大小label=labels[i])scatters.append(scatter)line, = ax.plot([], [], [],color=colors[i],linewidth=1.5,alpha=0.8)lines.append(line)# 添加图例到右上角
ax.legend(loc="upper right")# 更新函数
def update(frame):updated_artists = []  # 存储更新的对象for i in range(len(scatters)):# 更新小球位置scatters[i]._offsets3d = ([x_all_opts[i][frame, 0]],[x_all_opts[i][frame, 1]],[z_all_opts[i][frame]],)# 更新轨迹线lines[i].set_data(x_all_opts[i][:frame + 1, 0],  # X 轨迹x_all_opts[i][:frame + 1, 1],  # Y 轨迹)lines[i].set_3d_properties(z_all_opts[i][:frame + 1])  # Z 轨迹# 添加到更新列表updated_artists.append(scatters[i])updated_artists.append(lines[i])return updated_artists  # 返回更新的对象# 创建动画
num_frames = max(len(x) for x in x_all_opts)  # 获取最长的轨迹步数
ani = FuncAnimation(fig, update, frames=num_frames, interval=100, blit=True)# 显示图像
plt.show()
# animator.save('teaser' + '.gif', writer='imagemagick', fps=10)  # 效果不好,估计被挡住了…… 有待进一步提高 Edit by David 2022.12.4
# save不好用,不费劲了,安装个软件做gif https://pc.qq.com/detail/13/detail_23913.html

参考:

[1]NNDL 实验八 网络优化与正则化(3)不同优化算法比较_2d可视化实验与简单拟合实验-CSDN博客
[2]NNDL实验 优化算法3D轨迹 鱼书例题3D版_优化算法3d展示-CSDN博客

[3]NNDL实验 优化算法3D轨迹 复现cs231经典动画_优化算法寻优过程3d-CSDN博客

[4]NNDL实验 优化算法3D轨迹 pytorch版_nndl 实验三 将数据转换为 pytorch 张量-CSDN博客

[5]飞桨AI Studio星河社区-人工智能学习与实训社区

[6]神经网络与深度学习

[7]NNDL 作业11:优化算法比较_cifar10 sgd和adam-CSDN博客

这次是深度学习的最后一次实验啦(*^▽^*)完结撒花❀❀❀

在完成这门深度学习与神经网络课程的最后一次实验后,我感到收获颇丰。通过亲手实践和比较不同的神经网络优化算法,我对这些算法的内部机制和实际应用有了更深刻的理解。实验过程中,我不仅学会了如何实现和调整这些算法,还学会了如何根据实验结果分析它们的优缺点和适用场景。这些经验对于我未来在机器学习和人工智能领域的研究和工作无疑是宝贵的财富。
此外,在这学期的课程中我也体会到了实验过程中的挑战和乐趣。每次实验都像是一次探险,充满了未知和可能性。当看到算法按照预期工作,或者找到了更好的参数组合时,那种成就感和兴奋感是难以言表的。同时,遇到问题和困难时,通过查阅资料、讨论和思考找到解决方案的过程,也锻炼了我的问题解决能力和批判性思维。虽然作业多了那么一丢丢,但是收获真的大大滴!

我也遇到了一个讲课非常好的老师,非常的认真敬业,教会了我很多东西。学完这门课后,发现自己这学期读了很多篇论文。之前看论文图感觉蒙蒙的,现在再也不怕啦。也学会了很多的应用,学到了各种知识。遇到一个好的老师是多么重要呀!(#^.^#)

这门课程不仅让我掌握了深度学习和神经网络的核心技术,还培养了我独立思考和解决问题的能力。我将这些经验视为我职业生涯中的宝贵财富,并期待在未来的学习和工作中继续探索和应用这些知识!随着人工智能技术的不断发展,我相信这些技能将帮助我在这一领域取得更大的成就!

撒有哪啦~

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com