看到一个图片,就是那个表情包,大家都知道:

Adadelta  》  NAG 》 Momentum》 Remsprop 》Adagrad 》SGD

但是我觉得看情况而定,比如有http://blog.51cto.com/12568470/1898367常见优化算法 (tensorflow对应参数)就认为实际工作上实践中觉得是ADAM ,但是谁说的准呢是吧,每个工程师的场景不一样,得到的实践的经验也不一样,也说不准呢。

所以有的人建议:调试时用快的优化器去训练,等发论文时,所有的优化器都尝试一次,取最好的效果就好。

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data# 载入数据集
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)# 每个批次的大小
batch_size = 100
# 计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size# 定义两个placeholder
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])# 创建一个简单的神经网络
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x, W) + b)# 二次代价函数
# loss = tf.reduce_mean(tf.square(y-prediction))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
# 使用梯度下降法
# train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
# 初始化变量
init = tf.global_variables_initializer()# 结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1))  # argmax返回一维张量中最大的值所在的位置
# 求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))with tf.Session() as sess:sess.run(init)for epoch in range(21):for batch in range(n_batch):batch_xs, batch_ys = mnist.train.next_batch(batch_size)sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
  • SGD

optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate)

Iter 0,Testing Accuracy 0.825
Iter 1,Testing Accuracy 0.8798
Iter 2,Testing Accuracy 0.8994
Iter 3,Testing Accuracy 0.9047
Iter 4,Testing Accuracy 0.9076
Iter 5,Testing Accuracy 0.9104
Iter 6,Testing Accuracy 0.9121
Iter 7,Testing Accuracy 0.9127
Iter 8,Testing Accuracy 0.9147
Iter 9,Testing Accuracy 0.9166
Iter 10,Testing Accuracy 0.9174
Iter 11,Testing Accuracy 0.9167
Iter 12,Testing Accuracy 0.9183
Iter 13,Testing Accuracy 0.9183
Iter 14,Testing Accuracy 0.9202
Iter 15,Testing Accuracy 0.9197
Iter 16,Testing Accuracy 0.9204
Iter 17,Testing Accuracy 0.9213
Iter 18,Testing Accuracy 0.921
Iter 19,Testing Accuracy 0.9213
Iter 20,Testing Accuracy 0.9217
  • ADAM

optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, epsilon=1e-08)

train_step_AdamOptimizer = tf.train.AdamOptimizer(1e-3).minimize(loss)

Iter 0,Testing Accuracy 0.8991
Iter 1,Testing Accuracy 0.9109
Iter 2,Testing Accuracy 0.916
Iter 3,Testing Accuracy 0.9199
Iter 4,Testing Accuracy 0.9229
Iter 5,Testing Accuracy 0.9247
Iter 6,Testing Accuracy 0.926
Iter 7,Testing Accuracy 0.9276
Iter 8,Testing Accuracy 0.928
Iter 9,Testing Accuracy 0.928
Iter 10,Testing Accuracy 0.9289
Iter 11,Testing Accuracy 0.9297
Iter 12,Testing Accuracy 0.9308
Iter 13,Testing Accuracy 0.93
Iter 14,Testing Accuracy 0.9297
Iter 15,Testing Accuracy 0.9304
Iter 16,Testing Accuracy 0.9301
Iter 17,Testing Accuracy 0.9318
Iter 18,Testing Accuracy 0.9306
Iter 19,Testing Accuracy 0.9315
Iter 20,Testing Accuracy 0.9317

train_step_AdadeltaOptimizer = tf.train.AdadeltaOptimizer(1e-3).minimize(loss)

Iter 0,Testing Accuracy 0.6779
Iter 1,Testing Accuracy 0.6761
Iter 2,Testing Accuracy 0.6757
Iter 3,Testing Accuracy 0.6762
Iter 4,Testing Accuracy 0.6775
Iter 5,Testing Accuracy 0.6785
Iter 6,Testing Accuracy 0.6791
Iter 7,Testing Accuracy 0.682
Iter 8,Testing Accuracy 0.6854
Iter 9,Testing Accuracy 0.6878
Iter 10,Testing Accuracy 0.6896
Iter 11,Testing Accuracy 0.6919
Iter 12,Testing Accuracy 0.6941
Iter 13,Testing Accuracy 0.6961
Iter 14,Testing Accuracy 0.6973
Iter 15,Testing Accuracy 0.6983
Iter 16,Testing Accuracy 0.6996
Iter 17,Testing Accuracy 0.7001
Iter 18,Testing Accuracy 0.7013
Iter 19,Testing Accuracy 0.7017
Iter 20,Testing Accuracy 0.7023

train_step_AdadeltaOptimizer = tf.train.AdadeltaOptimizer(1).minimize(loss)

Iter 0,Testing Accuracy 0.874
Iter 1,Testing Accuracy 0.8949
Iter 2,Testing Accuracy 0.905
Iter 3,Testing Accuracy 0.9075
Iter 4,Testing Accuracy 0.9102
Iter 5,Testing Accuracy 0.9125
Iter 6,Testing Accuracy 0.9141
Iter 7,Testing Accuracy 0.9154
Iter 8,Testing Accuracy 0.9173
Iter 9,Testing Accuracy 0.9182
Iter 10,Testing Accuracy 0.919
Iter 11,Testing Accuracy 0.9204
Iter 12,Testing Accuracy 0.9213
Iter 13,Testing Accuracy 0.9213
Iter 14,Testing Accuracy 0.9222
Iter 15,Testing Accuracy 0.9228
Iter 16,Testing Accuracy 0.9226
Iter 17,Testing Accuracy 0.9226
Iter 18,Testing Accuracy 0.9232
Iter 19,Testing Accuracy 0.9239
Iter 20,Testing Accuracy 0.9237

上面的 AdadeltaOptimizer 学习率来看就知道啦,还是要找到最合适的学习率才行,要不然还是没啥效果,理论归整是理论。

AdadeltaOptimizertf.train包中的一个优化器,它可以自动调整学习率。最开始的时候我看到这个优化器觉得很厉害的一个,结果使用后发现loss根本不下降,本来还以为是用法用错了呢,几经周折,最后才发现是学习率设置的问题。

 # set optimizer
optimizer = tf.train.AdadeltaOptimizer()
# set train_op
train_op = slim.learning.create_train_op(loss, optimizer)

AdadeltaOptimizer优化器默认的learning_rate=0.001,非常的小,导致梯度下降速度非常慢,最后的解决方案是:提高学习率

 # set optimizer
optimizer = tf.train.AdadeltaOptimizer(learning_rate=1)
# set train_op
train_op = slim.learning.create_train_op(loss, optimizer)

http://data-science.vip/2017/12/18/TensorFlow%E7%BC%96%E7%A8%8B%E4%B8%AD%E8%B8%A9%E8%BF%87%E7%9A%84%E5%9D%91(1).html  TensorFlow编程中踩过的坑(1)

  • Momentum
    train_step_MomentumOptimizer = tf.train.MomentumOptimizer(1e-3, 0.9).minimize(loss)

Iter 0,Testing Accuracy 0.548
Iter 1,Testing Accuracy 0.6094
Iter 2,Testing Accuracy 0.7094
Iter 3,Testing Accuracy 0.7726
Iter 4,Testing Accuracy 0.791
Iter 5,Testing Accuracy 0.7964
Iter 6,Testing Accuracy 0.8015
Iter 7,Testing Accuracy 0.8056
Iter 8,Testing Accuracy 0.8081
Iter 9,Testing Accuracy 0.811
Iter 10,Testing Accuracy 0.8137
Iter 11,Testing Accuracy 0.8162
Iter 12,Testing Accuracy 0.8184
Iter 13,Testing Accuracy 0.8193
Iter 14,Testing Accuracy 0.8198
Iter 15,Testing Accuracy 0.821
Iter 16,Testing Accuracy 0.8226
Iter 17,Testing Accuracy 0.8236
Iter 18,Testing Accuracy 0.8243
Iter 19,Testing Accuracy 0.825
Iter 20,Testing Accuracy 0.8259

  • RMSProp

    train_step_RMSPropOptimizer = tf.train.RMSPropOptimizer(0.003, 0.9).minimize(loss)

Iter 0,Testing Accuracy 0.9158
Iter 1,Testing Accuracy 0.9216
Iter 2,Testing Accuracy 0.9263
Iter 3,Testing Accuracy 0.9274
Iter 4,Testing Accuracy 0.9275
Iter 5,Testing Accuracy 0.9319
Iter 6,Testing Accuracy 0.9309
Iter 7,Testing Accuracy 0.9286
Iter 8,Testing Accuracy 0.9303
Iter 9,Testing Accuracy 0.9305
Iter 10,Testing Accuracy 0.9316
Iter 11,Testing Accuracy 0.9318
Iter 12,Testing Accuracy 0.933
Iter 13,Testing Accuracy 0.9316
Iter 14,Testing Accuracy 0.9327
Iter 15,Testing Accuracy 0.9315
Iter 16,Testing Accuracy 0.9307
Iter 17,Testing Accuracy 0.9327
Iter 18,Testing Accuracy 0.9332
Iter 19,Testing Accuracy 0.9324
Iter 20,Testing Accuracy 0.9316

  • 实际经验

ADAM通常会取得比较好的结果,同时收敛非常快相比SGD L-BFGS适用于全batch做优化的情况, 有时候可以多种优化方法同时使用,比如使用SGD进行warm up,然后ADAM 对于比较奇怪的需求,deepbit两个loss的收敛需要进行控制的情况,比较慢的SGD比较适用。

tensorflow 不同优化算法对应的参数

SGD

optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate)

Momentum

optimizer = tf.train.MomentumOptimizer(lr, 0.9)

AdaGrad

optimizer = tf.train.AdagradientOptimizer(learning_rate=self.learning_rate)

RMSProp

optimizer = tf.train.RMSPropOptimizer(0.001, 0.9)

ADAM

optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, epsilon=1e-08)

部分局部参数需要查找tensorflow官方文档

深度学习框架tensorflow学习与应用6(优化器SGD、ADAM、Adadelta、Momentum、RMSProp比较)相关推荐

  1. 深度学习框架tensorflow学习与应用——代码笔记11(未完成)

    11-1 第十周作业-验证码识别(未完成) #!/usr/bin/env python # coding: utf-8# In[1]:import os import tensorflow as tf ...

  2. 深度学习框架tensorflow学习与应用6(防止过拟合dropout,keep_prob =tf.placeholder(tf.float32))

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data# In[3]:# 载入数据集 mn ...

  3. 深度学习框架Tensorflow学习与应用(五卷机神经网络CNN的讲解及CNN解决MNIST分类问题)

    (一)作业 # coding: utf-8# In[2]:import tensorflow as tf from tensorflow.examples.tutorials.mnist import ...

  4. 4.1 深度学习框架-TensorFlow

    4.1 深度学习框架-TensorFlow 学习目标 目标 了解Tensorflow框架的组成.接口 了解TensorFlow框架的安装 知道tf.keras的特点和使用 应用 无 4.1.1 常见深 ...

  5. Pytorch学习 - Task6 PyTorch常见的损失函数和优化器使用

    Pytorch学习 - Task6 PyTorch常见的损失函数和优化器使用 官方参考链接 1. 损失函数 (1)BCELoss 二分类 计算公式 小例子: (2) BCEWithLogitsLoss ...

  6. 吴恩达深度学习代码_吴恩达深度学习笔记(58)-深度学习框架Tensorflow

    TensorFlow 有很多很棒的深度学习编程框架,其中一个是TensorFlow,很期待帮助你开始学习使用TensorFlow,我想在这个笔记中向你展示TensorFlow程序的基本结构,然后让你自 ...

  7. 2_初学者快速掌握主流深度学习框架Tensorflow、Keras、Pytorch学习代码(20181211)

    初学者快速掌握主流深度学习框架Tensorflow.Keras.Pytorch学习代码 一.TensorFlow 1.资源地址: 2.资源介绍: 3.配置环境: 4.资源目录: 二.Keras 1.资 ...

  8. DL框架之TensorFlow:深度学习框架TensorFlow Core(低级别TensorFlow API)的简介、安装、使用方法之详细攻略

    DL框架之TensorFlow:TensorFlow Core(低级别TensorFlow API)的简介.安装.使用方法之详细DL框架之TensorFlow:深度学习框架TensorFlow Cor ...

  9. DL框架:主流深度学习框架(TensorFlow/Pytorch/Caffe/Keras/CNTK/MXNet/Theano/PaddlePaddle)简介、多个方向比较、案例应用之详细攻略

    DL框架:主流深度学习框架(TensorFlow/Pytorch/Caffe/Keras/CNTK/MXNet/Theano/PaddlePaddle)简介.多个方向比较.案例应用之详细攻略 目录 深 ...

  10. TensorFlow:深度学习框架TensorFlow TensorFlow_GPU的简介、安装、测试之详细攻略

    TensorFlow:深度学习框架TensorFlow & TensorFlow_GPU的简介.安装.测试之详细攻略 目录 TensorFlow的简介 TensorFlow的安装 1.tens ...

最新文章

  1. 8086 汇编指令手册查询(转)
  2. android:inputtype有哪些类型,android:inputType参数类型说明
  3. javascript 实现快排 ,三向切分快排
  4. OS / 进程和线程的区别和联系
  5. max hit in personalization - CRM My Opportunity搜索的实现
  6. pipelineDB学习笔记-2. Stream (流)
  7. poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
  8. Ubuntu 10.04下安装jekyll
  9. cut point and bridge总结
  10. 解决php无法上传大文件问题
  11. 距离一个优秀程序员,你还差多少?
  12. ehd边缘直方图描述子 matlab,一种新的图像空间特征提取方法
  13. caffee学习中文指南(1)(1)
  14. IE Adobe Flash Player版本已是最新,界面仍然提示版本过旧原因
  15. Android 清理app缓存数据的方法
  16. 失控的热潮:为什么说特斯拉的“电池日”名副其实?
  17. 配置微信小程序开发分享朋友圈功能
  18. 微信小程序开发之文件上传下载应用场景(附Demo源码)
  19. JS实现TTS语音播报
  20. xp系统打印机服务器win7连接不了,xp不能访问win7共享打印机

热门文章

  1. 4836: [Lydsy1704月赛]二元运算(cdq分治 FFT)
  2. 微信使用技巧 - 收集整理
  3. TextView实现左边图片右边文字或 上面图片下面文字
  4. 给计算机专业的同学一些建议
  5. retrofit介绍
  6. Word插入Excel的时候报错:用于创建此对象的程序是Excel
  7. WinCC 高速数据采集的实现
  8. 【设计】电压电流偏置
  9. #windowsxpsp3系统MS10-046漏洞测试
  10. 论文阅读:SCAFFOLD: Stochastic Controlled Averaging for Federated Learning