关于tf.contrib.data.Dataset
(来自TensorFlow 1.2,请参见此处和此处)用法:如何获取数据的方式并不真正适合我通常如何获取数据.在我的情况下,我有一个线程,我在那里收到数据,我不知道它什么时候会结束但我看到它何时结束.然后我等到我处理完所有的缓冲区,然后我完成了一个纪元.我怎样才能得到这个逻辑Dataset
?
请注意,我更喜欢Dataset
接口而不是QueueBase
接口,因为它为我提供了迭代器接口,我可以重新初始化,甚至重置为不同的接口Dataset
.与在关闭后无法重新打开的队列相比,这更加强大(请参阅此处和此处).
也许是一个类似的问题,或者同样的问题:我如何绕过Dataset
一个队列?我有一些线程从某处读取一些数据,可以提供它并以某种方式排队.如何将数据输入Dataset
?我可以无限次重复一些虚拟张量然后map
用来返回我的queue.dequeue()
但是这真的只能让我回到队列的所有原始问题,即如何重新打开队列.
新Dataset.from_generator()
方法允许您定义Dataset
由Python生成器提供的内容.(要在目前使用此功能,您必须下载每晚TensorFlow版本或从源代码自行构建.它将成为TensorFlow 1.4的一部分.)
实现示例的最简单方法是使用生成器替换接收线程,伪代码如下:
def receiver():
while True:
next_element = ... # Receive next element from external source.
# Note that this method may block.
end_of_epoch = ... # Decide whether or not to stop based on next_element.
if not end_of_epoch:
yield next_element # Note: you may need to convert this to an array.
else:
return # Returning will signal OutOfRangeError on downstream iterators.
dataset = tf.contrib.data.Dataset.from_generator(receiver, output_types=...)
# You can chain other `Dataset` methods after the generator. For example:
dataset = dataset.prefetch(...) # This will start a background thread
# to prefetch elements from `receiver()`.
dataset = dataset.repeat(...) # Note that each repetition will call
# `receiver()` again, and start from
# a fresh state.
dataset = dataset.batch(...)
Run Code Online (Sandbox Code Playgroud)
更复杂的拓扑是可能的.例如,您可以使用Dataset.interleave()
并行创建许多接收器.
归档时间: |
|
查看次数: |
3185 次 |
最近记录: |