Tensorflow:global_step不递增;因此exponentialDecay无效

时间:2016-07-07 19:22:22

标签: python tensorflow

我正在尝试学习Tensorflow,我想使用Tensorflow的cifar10教程框架并在mnist(结合两个教程)之上进行训练。

在cifar10.py的火车方法中:

cifar10.train(total_loss, global_step):
  lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE,                        
                                  global_step,                                  
                                  100,                                          
                                  0.1,                   
                                  staircase=True)                               
  tf.scalar_summary('learning_rate', lr)                                       
  tf.scalar_summary('global_step', global_step)

global_step传递初始化并传入,global_step一步增加1,学习速率正确衰减,源代码可以在tensorflow的cifar10教程中找到。

然而,当我尝试为修改后的mnist.py的火车方法代码做同样的事情时:

mnist.training(loss, batch_size, global_step):
  # Decay the learning rate exponentially based on the number of steps.         
  lr = tf.train.exponential_decay(0.1,                                          
                                  global_step,                                  
                                  100,                                             
                                  0.1,                                             
                                  staircase=True)                                  
  tf.scalar_summary('learning_rate1', lr)                                          
  tf.scalar_summary('global_step1', global_step)                                   

  # Create the gradient descent optimizer with the given learning rate.            
  optimizer = tf.train.GradientDescentOptimizer(lr)                                
  # Create a variable to track the global step.                                    
  global_step = tf.Variable(0, name='global_step', trainable=False)                
  # Use the optimizer to apply the gradients that minimize the loss                
  # (and also increment the global step counter) as a single training step.     
  train_op = optimizer.minimize(loss, global_step=global_step)                  
  tf.scalar_summary('global_step2', global_step)                                
  tf.scalar_summary('learning_rate2', lr)      
  return train_op       

全局步骤(在cifar10和我的mnist文件中)初始化为:

with tf.Graph().as_default(): 
  global_step = tf.Variable(0, trainable=False)
  ...
  # Build a Graph that trains the model with one batch of examples and           
  # updates the model parameters.                                                
  train_op = mnist10.training(loss, batch_size=100,                 
                              global_step=global_step)

在这里,我记录两次全局步长和学习率的标量: learning_rate1和learning_rate2都是相同的并且恒定为0.1(初始学习率)。 global_step1在2000步中也保持为0。 global_step2每步线性增加1。

可以在以下位置找到更详细的代码结构: https://bitbucket.org/jackywang529/tesorflow-sandbox/src

对我来说这可能是一个令人困惑的原因(在我的global_step的情况下,因为我认为所有内容都是符号设置的,所以一旦程序开始运行,无论我在哪里编写摘要,都应该增加全局步骤)我认为这就是我学习率不变的原因。当然,我可能会犯一些简单的错误,很乐意得到帮助/解释。

global_steps written before and after the minimize function is called

1 个答案:

答案 0 :(得分:5)

您正在将名为global_step的参数传递给mnist.training,并在global_step中创建名为mnist.training的变量。用于跟踪exponential_decay的那个是传入的变量,但实际递增的变量(通过传递给optimizer.minimize)是新创建的变量。只需从mnist.training中删除以下语句即可:

global_step = tf.Variable(0, name='global_step', trainable=False)