tf.train.batch的偶尔乱序问题

tf.train.batch的偶尔乱序问题

  • 我们在通过tf.Reader读取文件后,都需要用batch函数将读取的数据根据预先设定的batch_size打包为一个个独立的batch方便我们进行学习。
  • 常用的batch函数有tf.train.batch和tf.train.shuffle_batch函数。前者是将数据从前往后读取并顺序打包,后者则要进行乱序处理————即将读取的数据进行乱序后在组成批次。
  • 训练时我往往都是使用shuffle_batch函数,但是这次我在验证集上预调好模型并freeze模型后我需要在测试集上进行测试。此时我需要将数据的标签和inference后的结果进行一一对应。 此时数据出现的顺序是十分重要的,这保证我们的产品在上线前的测试集中能准确get到每个数据和inference后结果的差距 而在验证集中我们不太关心数据原有的标签和inference后的真实值,我们往往只是需要让这两个数据一一对应,关于数据出现的顺序我们并不关心。
  • 此时我们一般使用tf.train.batch函数将tf.Reader读取的值进行顺序打包即可。

然而tf.train.batch函数往往会有偶尔乱序的情况

  • 我们将csv文件中每个数据样本从上往下依次进行标号,我们在使用tf.trian.batch函数依次进行读取,如果我们读取的数据编号乱序了,则表明tf.train.batch函数有偶尔乱序的状况。

源程序文件下载

test_tf_train_batch.csv

import tensorflow as tf

BATCH_SIZE = 400
NUM_THREADS = 2
MAX_NUM = 500 def read_data(file_queue):
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(file_queue)
defaults = [[0], [0.], [0.]]
NUM, C, Tensile = tf.decode_csv(value, defaults)
vertor_example = tf.stack([C])
vertor_label = tf.stack([Tensile])
vertor_num = tf.stack([NUM]) return vertor_example, vertor_label, vertor_num def create_pipeline(filename, batch_size, num_threads):
file_queue = tf.train.string_input_producer([filename]) # 设置文件名队列
example, label, no = read_data(file_queue) # 读取数据和标签 example_batch, label_batch, no_batch = tf.train.batch(
[example, label, no], batch_size=batch_size, num_threads=num_threads, capacity=MAX_NUM) return example_batch, label_batch, no_batch x_train_batch, y_train_batch, no_train_batch = create_pipeline('test_tf_train_batch.csv', batch_size=BATCH_SIZE,
num_threads=NUM_THREADS) init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer()
with tf.Session() as sess:
sess.run(local_init_op)
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
example, label, num = sess.run([x_train_batch, y_train_batch, no_train_batch])
print(example)
print(label)
print(num)
coord.request_stop()
coord.join(threads)

实验结果

我们将csv文件中的真实Tensile值放在第一列,将使用tf.train.batch函数得到的Tensile和no分别放在第二列和第三列

TureTensile FalseTensile NO
0.830357143 [ 0.52678573] [ 66]
0.526785714 [ 0.83035713] [ 65]
0.553571429 [ 0.4375 ] [ 68]
0.4375 [ 0.5535714 ] [ 67]
0.517857143 [ 0.33035713] [ 70]
0.330357143 [ 0.51785713] [ 69]
0.482142857 [ 0.6785714 ] [ 72]
0.678571429 [ 0.48214287] [ 71]
0.419642857 [ 0.02678571] [ 74]
0.026785714 [ 0.41964287] [ 73]
0.401785714 [ 0.4017857 ] [ 75]

解决方案

  • 将测试集中所有样本数据加NO顺序标签列

tf.train.batch的偶尔乱序问题的更多相关文章

  1. tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数(转)

    tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...

  2. tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数

    tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...

  3. 【转载】 tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数

    原文地址: https://blog.csdn.net/dcrmg/article/details/79776876 ----------------------------------------- ...

  4. 深度学习原理与框架-Tfrecord数据集的读取与训练(代码) 1.tf.train.batch(获取batch图片) 2.tf.image.resize_image_with_crop_or_pad(图片压缩) 3.tf.train.per_image_stand..(图片标准化) 4.tf.train.string_input_producer(字符串入队列) 5.tf.TFRecord(读

    1.tf.train.batch(image, batch_size=batch_size, num_threads=1) # 获取一个batch的数据 参数说明:image表示输入图片,batch_ ...

  5. 【转载】 tf.train.slice_input_producer()和tf.train.batch()

    原文地址: https://www.jianshu.com/p/8ba9cfc738c2 ------------------------------------------------------- ...

  6. tensorflow数据读取机制tf.train.slice_input_producer 和 tf.train.batch 函数

    tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数据读入到一个内存队列中,另一个线程 ...

  7. tensorflow|tf.train.slice_input_producer|tf.train.Coordinator|tf.train.start_queue_runners

    #### ''' tf.train.slice_input_producer :定义样本放入文件名队列的方式[迭代次数,是否乱序],但此时文件名队列还没有真正写入数据 slice_input_prod ...

  8. tfsenflow队列|tf.train.slice_input_producer|tf.train.Coordinator|tf.train.start_queue_runners

      #### ''' tf.train.slice_input_producer :定义样本放入文件名队列的方式[迭代次数,是否乱序],但此时文件名队列还没有真正写入数据 slice_input_pr ...

  9. tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners

    TensorFlow的Session对象是支持多线程的,可以在同一个会话(Session)中创建多个线程,并行执行.在Session中的所有线程都必须能被同步终止,异常必须能被正确捕获并报告,会话终止 ...

随机推荐

  1. IT工具使用

    linux 其他知识目录 常用快捷键总结 博客view  code 删除,先删除,再清除格式

  2. C#的lock语句

    文章:lock 语句(C# 参考) 代码: using System; using System.Threading.Tasks; public class Account { private rea ...

  3. asp.net登录状态验证

    文章:ASP.NET 登录验证 文章:ASP.NET MVC下判断用户登录和授权状态方法 文章:.net学习笔记---HttpHandle与HttpModule 第一篇文章,介绍了 1)早期的Base ...

  4. C++ Primer Plus学习:第六章

    C++入门第六章:分支语句和逻辑运算符 if语句 语法: if (test-condition) statement if else语句 if (test-condition) statement1 ...

  5. equals()和hashcode()详解

    转载自http://www.cnblogs.com/Qian123/p/5703507.html java.lang.Object类中有两个非常重要的方法:   public boolean equa ...

  6. playbook详解—YAML格式的文本

    在playbook中有一些核心的指令 hosts:指明命令运行在哪个node之上 remote_user:在远端的node之上以什么用户的身份运行命令 var:给模板传递变量值 tasks:指明需要执 ...

  7. JS高级 2

    递归:函数自己调用自己 在JavaScript中唯一能产生作用域的东西是 函数!js中只有函数可以创建作用域 词法作用域,也叫做静态作用域 //就是在代码写好的那一刻,变量和函数的作用域就已经确定了, ...

  8. 【week10】规格说明书练习-吉林市1日游

    假设我们全班同学及教师去吉林省吉林市1日游,请为这次活动给出规格说明书. 版本:1.0 编订:于淼 团队:2016级计算机技术全体同学 日期:2016/11/19 1.引言 1.1 编写目的 1.2 ...

  9. HDU 2164 Rock, Paper, or Scissors?

    http://acm.hdu.edu.cn/showproblem.php?pid=2164 Problem Description Rock, Paper, Scissors is a two pl ...

  10. FreeMarker(XML模板)导出word

    在项目中使用它完成的功能是按照固定的模板将数据导出到Word.比如台账.在完成后将处理过程按照台账的要求导出,有时程序中需要实现生成标准Word文档,要求能够打印,并且保持页面样式不变. 这个功能就是 ...