How to set adaptive learning rate for GradientDescentOptimizer?

前端 未结 5 1095
醉酒成梦
醉酒成梦 2020-11-28 01:08

I am using TensorFlow to train a neural network. This is how I am initializing the GradientDescentOptimizer:

init = tf.initialize_all_variables(         


        
5条回答
  •  迷失自我
    2020-11-28 01:51

    First of all, tf.train.GradientDescentOptimizer is designed to use a constant learning rate for all variables in all steps. TensorFlow also provides out-of-the-box adaptive optimizers including the tf.train.AdagradOptimizer and the tf.train.AdamOptimizer, and these can be used as drop-in replacements.

    However, if you want to control the learning rate with otherwise-vanilla gradient descent, you can take advantage of the fact that the learning_rate argument to the tf.train.GradientDescentOptimizer constructor can be a Tensor object. This allows you to compute a different value for the learning rate in each step, for example:

    learning_rate = tf.placeholder(tf.float32, shape=[])
    # ...
    train_step = tf.train.GradientDescentOptimizer(
        learning_rate=learning_rate).minimize(mse)
    
    sess = tf.Session()
    
    # Feed different values for learning rate to each training step.
    sess.run(train_step, feed_dict={learning_rate: 0.1})
    sess.run(train_step, feed_dict={learning_rate: 0.1})
    sess.run(train_step, feed_dict={learning_rate: 0.01})
    sess.run(train_step, feed_dict={learning_rate: 0.01})
    

    Alternatively, you could create a scalar tf.Variable that holds the learning rate, and assign it each time you want to change the learning rate.

提交回复
热议问题