tf.train.batch的偶尔乱序问题
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的偶尔乱序问题的更多相关文章
- tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数(转)
tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...
- tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数
tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...
- 【转载】 tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数
原文地址: https://blog.csdn.net/dcrmg/article/details/79776876 ----------------------------------------- ...
- 深度学习原理与框架-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_ ...
- 【转载】 tf.train.slice_input_producer()和tf.train.batch()
原文地址: https://www.jianshu.com/p/8ba9cfc738c2 ------------------------------------------------------- ...
- tensorflow数据读取机制tf.train.slice_input_producer 和 tf.train.batch 函数
tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数据读入到一个内存队列中,另一个线程 ...
- tensorflow|tf.train.slice_input_producer|tf.train.Coordinator|tf.train.start_queue_runners
#### ''' tf.train.slice_input_producer :定义样本放入文件名队列的方式[迭代次数,是否乱序],但此时文件名队列还没有真正写入数据 slice_input_prod ...
- tfsenflow队列|tf.train.slice_input_producer|tf.train.Coordinator|tf.train.start_queue_runners
#### ''' tf.train.slice_input_producer :定义样本放入文件名队列的方式[迭代次数,是否乱序],但此时文件名队列还没有真正写入数据 slice_input_pr ...
- tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners
TensorFlow的Session对象是支持多线程的,可以在同一个会话(Session)中创建多个线程,并行执行.在Session中的所有线程都必须能被同步终止,异常必须能被正确捕获并报告,会话终止 ...
随机推荐
- CSS中水平居中设置的几种方式
1.行内元素: 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-align:center 来实现的. <body> <div class="t ...
- 互评Alpha版本——杨老师粉丝群——Pinball
一.基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论作品的选题 (1)N(Need,需求) 成语学习对除汉语言专业外的大学生的需求并不是很高,初中生和高中生因为在升学时需要参加语文 ...
- 按照Right-BICEP要求设计的测试用例
测试用例: 测试方法:Right-BICEP 测试要求: Right-结果是否正确? B-是否所有的边界条件都是正确的? P-是否满足性能要求? 题目是否有重复? 数量是否可定制? 数值范围是否可定制 ...
- C语言的知识与能力予以自评
看到一个问卷不错,拟作为第三次作业的部分内容. 你对自己的未来有什么规划?做了哪些准备?答:多学习几门生存技巧,首先先学会碰壁. 你认为什么是学习?学习有什么用?现在学习动力如何?为什么?答:学习是人 ...
- 注解实现IOC和DI
1.组件扫描 Spring3.0后为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component.@Service.@Controller.@Repository注解的类,并把这些类纳 ...
- hadoop对于压缩文件的支持
转载:https://www.cnblogs.com/ggjucheng/archive/2012/04/22/2465580.html hadoop对于压缩格式的是透明识别,我们的MapReduce ...
- crontab & php实现多进程思路
<?php $startTime = time(); while(1) { if (time() - $startTime > 600) { exit; } // ... Do SomeT ...
- webgl helloworld
之前的webgl 初识1, 初识2 已经介绍了webgl的基本概念,工作原理. 没有理解的自己yy. 现呈上例子一枚 <!DOCTYPE html> <html lang=" ...
- 彻底解决Webpack打包慢的问题
转载 这几天写腾讯实习生 Mini 项目的时候用上了 React 全家桶,当然同时引入了 Webpack 作为打包工具.但是开发过程中遇到一个很棘手的问题就是,React 加上 React-Route ...
- React安装React Devtools调试工具
在运行一个React项目的时候浏览器控制台会提醒你去安装react devtools调试工具. Download the React DevTools for a better development ...