Skip to main content
 首页 » 编程设计

python之Tensorflow 调用之间的数据变化

2024年08月21日32cmt

我对 Tensorflow MNIST 教程做了一点改动。 原始代码(fully_connected_feed.py,第 194-202 行):

checkpoint_file = os.path.join(FLAGS.log_dir, 'model.ckpt') 
saver.save(sess, checkpoint_file, global_step=global_step) 
#Evaluate against the training set. 
print('Training Data Eval:') 
do_eval(sess,  
        eval_correct,  
        images_placeholder, 
        labels_placeholder, 
        data_sets.train) 

我简单的加了一个评价:

checkpoint_file = os.path.join(FLAGS.log_dir, 'model.ckpt') 
saver.save(sess, checkpoint_file, global_step=global_step) 
print('Something strange:') 
do_eval(sess, eval_correct, images_placeholder,labels_placeholder, 
        data_sets.train) 
#Evaluate against the training set. 
print('Training Data Eval:') 
do_eval(sess,  
        eval_correct,  
        images_placeholder, 
        labels_placeholder, 
        data_sets.train) 

此评估的结果很接近,但不相同(数字因发布而异):

Something strange: 
  Num examples: 55000  Num correct: 49218  Precision @ 1: 0.8949 
Training Data Eval: 
  Num examples: 55000  Num correct: 49324  Precision @ 1: 0.8968 

这怎么可能? UPD:添加了指向 tensorflow github 的链接: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/mnist

请您参考如下方法:

do_eval() 函数实际上确实有副作用,因为 data_sets.train 是 stateful DataSet object包含当前的 _index_in_epoch 成员,每次调用 DataSet.next_batch() 时都会提前(即在 fill_feed_dict() 中)。

就其本身而言,这一事实不足以给出不确定的结果,但是有关 DataSet.next_batch() 的另外两个细节导致了不确定性:

  1. 每开始一个新的epoch,例子都是randomly shuffled .

  2. 当数据集reaches the end of an epoch ,数据集重置为开始,最后的 num_examples % batch_size 示例被丢弃。由于随机改组,每次都会丢弃一个随机子批处理的示例,从而导致不确定的结果。

鉴于代码的结构方式(在训练和测试之间共享 DataSet),要使代码具有确定性是很棘手的。 DataSet 类的文档很少,但这种行为令人惊讶,所以我会考虑 filing a GitHub issue关于这个问题。