一、几个函数

  • RandomShuffleQueue类

__init__(self, capacity, min_after_dequeue,dtypes, shapes=None,names=None, seed=None, shared_name=None, name="random_shuffle_queue")

queue = tf.RandomShuffleQueue(...):创建一个queue,按随机顺序进行dequeue

RandomShuffleQueue有一定的容量限制capacity,支持多个生产者和消费者
RandomShuffleQueue中的每个元素是固定长度的tensor 元组,数据类型由dtypes定义,形状为shapes。如果shapes没有定义,那么不同的queue元素可能有不同的形状,此时就不能使用dqueue_many。如果shapes定义了,则所有的元素必须有相同的形状
min_after_dequeue决定queue在dequeue以后要保持的元素个数,如果没有足够的元素,就会block住dequeue的相关操作,直到有足够元素进来。当queue关闭,则这个参数被忽略

  • enqueue(self, vals, name=None)

enqueue_op = queue.enqueue(...) 创建enqueue元素到queue中的操作

如果操作执行时queue是满的,则会block住
vals是一个tensor或一个tensor的list/tuple,或者是一个字典,它相当于enqueue操作时的数据池
enqueue操作是要手动触发的,也就是不是说像一般的那种计算,会把enqueue作为依赖操作被执行

  • queue.dequeue(self, name=None)

从queue中取出一个元素

  • Coordinator类

__init__(self, clean_stop_exception_types=None)

coord = tf.Coordinator() 协调线程的执行

  • QueueRunner类

__init__(self, queue=None, enqueue_ops=None, close_op=None, cancel_op=None, queue_closed_exception_types=None,queue_runner_def=None, import_scope=None)

说明
qr = tf.train.QueueRunner(...) 为一个queue保持一系列enqueue操作,每个操作以一个线程执行
queue: a Queue
enqueue_ops: 一个enqueue ops列表
close_op: 指定关闭queue的操作
cancel_op:指定关闭以及取消挂起的enqueue ops的操作

  • qr.create_threads(self, sess, coord=None, daemon=False, start=False)

为给定的sess创建多个线程以执行enqueue ops
start:如果为False,则需要手动调用 start()来启动

  • start_queue_runners

start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection=ops.GraphKeys.QUEUE_RUNNERS)

tf.train.start_queue_runners(...) 启动图中所有的queue runners,与add_queue_runner()配合使用
start: `False`只是创建线程,但是没有启动

二、实例 

 def example1():

 """
最简单的例子,只使用enqueue和dequeue
:return:
"""
example = tf.constant(2, "float32", [2, 2])
# 创建一个queue
# tf.RandomShuffleQueue(capacity,: queue的容量
# min_after_dequeue, : 保证queue中最少的个数
# dtypes,
# shapes=None,...)
queue = tf.RandomShuffleQueue(10, 0, "float32", shapes=[2, 2])
# 为queue添加enqueue操作
enqueue_op = queue.enqueue(example)
# 为queue添加dequeue操作
inputs = queue.dequeue()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(enqueue_op)
print(sess.run(inputs))
 def example2():
"""
使用queue runner来管理多个enqueue线程,用coord来关闭线程
:return:
"""
data = tf.constant(2, "float32", [2, 2])
example = [data, data, data, data, data, data, data, data]
queue = tf.RandomShuffleQueue(10, 0, "float32", shapes=[2, 2])
enqueue_op = queue.enqueue(example) qr = tf.train.QueueRunner(queue, [enqueue_op] * 4)
coord = tf.train.Coordinator() inputs = queue.dequeue()
with tf.Session() as sess:
threads = qr.create_threads(sess, coord, start=True)
sess.run(tf.global_variables_initializer())
print(sess.run(inputs))
# 用coord来停止所有的enqueu线程
coord.request_stop()
coord.join(threads)

RandomShuffleQueue的更多相关文章

  1. 【err】tensorflow.python.framework.errors_impl.OutOfRangeError: RandomShuffleQueue

    problem Traceback (most recent call last): File , in _do_call return fn(*args) File , in _run_fn opt ...

  2. 【error】OutOfRangeError (see above for traceback): RandomShuffleQueue

    前言 在使用tensorflow TFRecord的过程中,读取*.tfrecord文件时出现错误,本文解决这个错误. 错误描述: OutOfRangeError (see above , curre ...

  3. TFRecordReader "OutOfRangeError (see above for traceback): RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 1, current size 0)" 问题原因总结;

    1. tf.decode_raw(features['image_raw'],tf.uint8) 解码时,数据类型有没有错?tf.float32 和tf.uint8有没有弄混??? 2. tf.tra ...

  4. 学习笔记TF049:TensorFlow 模型存储加载、队列线程、加载数据、自定义操作

    生成检查点文件(chekpoint file),扩展名.ckpt,tf.train.Saver对象调用Saver.save()生成.包含权重和其他程序定义变量,不包含图结构.另一程序使用,需要重新创建 ...

  5. TensorFlow实践笔记(一):数据读取

    本文整理了TensorFlow中的数据读取方法,在TensorFlow中主要有三种方法读取数据: Feeding:由Python提供数据. Preloaded data:预加载数据. Reading ...

  6. 【学习笔记】tensorflow队列和线程

    目录 Tensorflow队列 同步执行队列 队列管理器 异步执行队列 线程协调器 在使用TensorFlow进行异步计算时,队列是一种强大的机制. 为了感受一下队列,让我们来看一个简单的例子.我们先 ...

  7. Tensorflow 大规模数据集训练方法

    本文转自:Tensorflow]超大规模数据集解决方案:通过线程来预取 原文地址:https://blog.csdn.net/mao_xiao_feng/article/details/7399178 ...

  8. 大数据下基于Tensorflow框架的深度学习示例教程

    近几年,信息时代的快速发展产生了海量数据,诞生了无数前沿的大数据技术与应用.在当今大数据时代的产业界,商业决策日益基于数据的分析作出.当数据膨胀到一定规模时,基于机器学习对海量复杂数据的分析更能产生较 ...

  9. 第十二节,TensorFlow读取数据的几种方法以及队列的使用

    TensorFlow程序读取数据一共有3种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFlow图的起 ...

随机推荐

  1. codeforces476D

    Dreamoon and Sets CodeForces - 476D Dreamoon likes to play with sets, integers and .  is defined as ...

  2. 【DeepLearning】优化算法:SGD、GD、mini-batch GD、Moment、RMSprob、Adam

    优化算法 1 GD/SGD/mini-batch GD GD:Gradient Descent,就是传统意义上的梯度下降,也叫batch GD. SGD:随机梯度下降.一次只随机选择一个样本进行训练和 ...

  3. linux shell通配符及if语句判断

    $# 是传给脚本的参数个数 $0 是脚本本身的名字$1 是传递给该shell脚本的第一个参数$2 是传递给该shell脚本的第二个参数$@ 是传给脚本的所有参数的列表$* 是以一个单字符串显示所有向脚 ...

  4. vue router 修改title(IOS 下动态改变title失效)

    在ios下app  设置document.title = "titleName" 失效,原因是在IOS webview中网页标题只加载一次,动态改变是无效的. 在路由配置中添加   ...

  5. Monte Carlo Method(蒙特·卡罗方法)

    0-故事: 蒙特卡罗方法是计算模拟的基础,其名字来源于世界著名的赌城——摩纳哥的蒙特卡罗. 蒙特卡罗一词来源于意大利语,是为了纪念王子摩纳哥查理三世.蒙特卡罗(MonteCarlo)虽然是个赌城,但很 ...

  6. 在线SQL

    有时候为了简单测试一下SQL语法,自己机器上没有安装,可使用在线的SQL环境. 我找到了两个: https://rextester.com/l/sql_server_online_compiler ( ...

  7. 【AGC030D】Inversion Sum DP

    题目大意 有一个序列 \(a_1,a_2,\ldots,a_n\),有 \(q\) 次操作,每次操作给你两个数 \(x,y\),你可以交换 \(a_x,a_y\),或者什么都不做. 问你所有 \(2^ ...

  8. Oracle中的AWR,全称为Automatic Workload Repository

    Oracle中的AWR,全称为Automatic Workload Repository,自动负载信息库.它收集关于特定数据库的操作统计信息和其他统计信息,Oracle以固定的时间间隔(默认为1个小时 ...

  9. 「POI2011 R2 Day2」Tree Rotations【线段树合并】

    题目链接 [BZOJ] [洛谷] [LOJ] 题解 由于是前序遍历,那么讨论一棵树上的逆序对的情况. 两个节点都在左子树上 两个节点都在右子树上 两个节点分别在不同的子树上. 前两种情况其实也可以归结 ...

  10. CSS中各种居中的问题

    1.元素水平居中 1.1 在父元素上使用text-align: center; father { text-align: center; } 1.2 margin: 0 auto; 在上一个问题中,我 ...