How to choose optimizer ?训练时,如何选择优化器?

 站在巨人的肩膀上肩膀左肩膀右+1+2+3+4


当学习神经网络到一定程度时,不难发现优化器和损失函数是神经网络模型不可缺少的部分。

优化器Optimizer


目录

1 优化器Optimizer

2 优化器调用及相关参数

3 优化方法

3.1 SGD算法——随机梯度下降方法

3.2 ASGD算法——平均随机梯度下降算法

3.3 Adagrad算法

3.4 Adadelta算法——自适应学习率调整

3.5 RMSprop算法

3.6 Adam算法——自适应矩估计

3.7 Adamax算法——Adam的无穷范数变种

3.8 SparseAdam算法

3.9 L-BFGS算法

3.10 Rprop算法——弹性反向传播算法

4 重要优化函数的动态图 

4.1 损失平面等高线

4.2 在鞍点处的比较


1 优化器Optimizer

求解神经网络,也就是求解 y =f(wx + b) 中的w 和 b。

如:何找到正确的权重值 w 和 b 呢?

方法1:随机搜索

  • 需要很多权重值,随机采样,然后把它们输入损失函数,再看它们效果如何。

方法2:梯度下降算法

  • 首先,初始化 w 和 b, 然后,使用梯度下降算法,对 w 和 b 进行更新。
  • 估值和真值间存在误差通过loss函数可以得到结果,优化器则考虑将损失值传递到网络的前端,最终目的是达到一个训练学习的目的。
  • 所有的优化函数都位于torch.optim包下,常用的优化器SGD、Adam、Adadelta、Adagrad、Adamax等,后续详细介绍。

2 优化器调用及相关参数

2.1 以SGD、Adam为例

optimizer = optim.SGD(model.parameters(), lr = 0.01, momentum=0.9)
optimizer = optim.Adam([var1, var2], lr = 0.0001)
# lr:学习率,大于0的浮点数
# momentum:动量参数,大于0的浮点数
# parameters:Variable参数,要优化的对象

2.2  torch.optim包下调用,同时指定优化的参数

torch.optim.Optimizer(params, defaults)
# params (iterable) —— Variable 或者 dict的iterable。指定了什么参数应当被优化。
# defaults —— (dict):包含了优化选项默认值的字典(一个参数组没有指定的参数选项将会使用默认值)。

2.3  优化器调用过程

load_state_dict(state_dict):加载optimizer状态。
state_dict():以dict返回optimizer的状态。包含两项:state - 一个保存了当前优化状态的dict,param_groups - 一个包含了全部参数组的dict。
add_param_group(param_group):给 optimizer 管理的参数组中增加一组参数,可为该组参数定制 lr,momentum, weight_decay 等,在 finetune 中常用。
step(closure) :进行单次优化 (参数更新)。
zero_grad() :清空所有被优化过的Variable的梯度。

3 优化方法

3.1 SGD算法——随机梯度下降方法

g_{t} = \triangledown _{\theta _{t-1}}f(\theta _{t-1})

\triangle \theta _{t} = -\eta *g_{t}       
其中, \eta是学习率,g_{t}是梯度

torch.optim.SGD(params, lr=, momentum=0, dampening=0, weight_decay=0, nesterov=False)

params (iterable) – 待优化参数的iterable或者是定义了参数组的dict
lr (float) – 学习率
momentum (float, 可选) – 动量因子(默认:0)
weight_decay (float, 可选) – 权重衰减(L2惩罚)(默认:0)
dampening (float, 可选) – 动量的抑制因子(默认:0)
nesterov (bool, 可选) – 使用Nesterov动量(默认:False)
  • 实质:
    • SGD表示min-batch gradient descent。
    • SGD就是每一次迭代计算mini-batch的梯度,然后对参数进行更新,是常见的优化方法之一。SGD完全依赖于当前batch的梯度,所以η(学习率)可理解为允许当前batch的梯度多大程度影响参数更新。
  • 优点:
    • 训练速度快,避免了批量梯度更新过程中的计算冗余问题,对于很大的数据
Semantic segmentation is a technique used to partition an image into multiple regions or objects and label them with semantic meaning. In Python, you can use various deep learning frameworks like TensorFlow, Keras, and PyTorch to perform semantic segmentation. Here is an example of how to perform semantic segmentation using TensorFlow: 1. Import the necessary libraries: ``` import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator ``` 2. Load the dataset: ``` train_datagen = ImageDataGenerator(rescale=1./255) train_data = train_datagen.flow_from_directory('path/to/train/dataset', batch_size=32, class_mode='categorical', target_size=(224, 224)) ``` 3. Define the model: ``` model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding='same', input_shape=(224,224,3)), tf.keras.layers.Conv2D(64, (3,3), activation='relu', padding='same'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(128, (3,3), activation='relu', padding='same'), tf.keras.layers.Conv2D(128, (3,3), activation='relu', padding='same'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(256, (3,3), activation='relu', padding='same'), tf.keras.layers.Conv2D(256, (3,3), activation='relu', padding='same'), tf.keras.layers.Conv2D(256, (3,3), activation='relu', padding='same'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(512, (3,3), activation='relu', padding='same'), tf.keras.layers.Conv2D(512, (3,3), activation='relu', padding='same'), tf.keras.layers.Conv2D(512, (3,3), activation='relu', padding='same'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(512, (3,3), activation='relu', padding='same'), tf.keras.layers.Conv2D(512, (3,3), activation='relu', padding='same'), tf.keras.layers.Conv2D(512, (3,3), activation='relu', padding='same'), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(4096, activation='relu'), tf.keras.layers.Dense(4096, activation='relu'), tf.keras.layers.Dense(21, activation='softmax') ]) ``` 4. Compile the model: ``` model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` 5. Train the model: ``` model.fit(train_data, epochs=10, steps_per_epoch=len(train_data)) ``` 6. Predict on new data: ``` image = tf.keras.preprocessing.image.load_img('path/to/image', target_size=(224, 224)) input_arr = tf.keras.preprocessing.image.img_to_array(image) input_arr = tf.expand_dims(input_arr, axis=0) predictions = model.predict(input_arr) ``` This is just an example and there are many other ways to perform semantic segmentation using Python. It's important to choose the right framework and model architecture based on your specific use case.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MengYa_DreamZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值