从TensorFlow图中的TFRecords文件读取顺序数据?

从TensorFlow图中的TFRecords文件读取顺序数据?,tensorflow,recurrent-neural-network,sequential,Tensorflow,Recurrent Neural Network,Sequential,我正在处理视频数据,但我相信这个问题应该适用于任何顺序数据。我想从TFRecords文件中传递我的RNN 10序列示例(视频帧)。当我第一次开始读取该文件时,我需要抓取10个示例,并使用它创建一个序列示例,然后将其推送到队列中,以便RNN在准备就绪时执行。但是,现在我已经有了10帧,下次我从TFRecords文件读取时,我只需要取一个示例,然后将其他9帧移到上面。但是当我到达第一个TFRecords文件的末尾时,我需要在第二个TFRecords文件上重新启动进程。我的理解是,condop将处理每

我正在处理视频数据,但我相信这个问题应该适用于任何顺序数据。我想从TFRecords文件中传递我的RNN 10序列示例(视频帧)。当我第一次开始读取该文件时,我需要抓取10个示例,并使用它创建一个序列示例,然后将其推送到队列中,以便RNN在准备就绪时执行。但是,现在我已经有了10帧,下次我从TFRecords文件读取时,我只需要取一个示例,然后将其他9帧移到上面。但是当我到达第一个TFRecords文件的末尾时,我需要在第二个TFRecords文件上重新启动进程。我的理解是,
cond
op将处理每个条件下所需的ops,即使该条件不是要使用的条件。当使用条件检查是阅读10个示例还是仅阅读1个示例时,这将是一个问题。是否仍有办法解决此问题,以获得上述所需的结果?

您可以使用TensorFlow 1.12中最近添加的转换来执行此操作:

filenames = tf.data.Dataset.list_files(...)

# Define a function that will be applied to each filename, and return the sequences in that
# file.
def get_examples_from_file(filename):
  # Read and parse the examples from the file using the appropriate logic.
  examples = tf.data.TFRecordDataset(filename).map(...)

  # Selects a sliding window of 10 examples, shifting along 1 example at a time.
  sequences = examples.window(size=10, shift=1, drop_remainder=True)

  # Each element of `sequences` is a nested dataset containing 10 consecutive examples.
  # Use `Dataset.batch()` and get the resulting tensor to convert it to a tensor value
  # (or values, if there are multiple features in an example).
  return sequences.map(
      lambda d: tf.data.experimental.get_single_element(d.batch(10)))

# Alternatively, you can use `filenames.interleave()` to mix together sequences from
# different files.
sequences = filenames.flat_map(get_examples_from_file)

序列上的映射函数不工作,我得到嵌套数据集上未实现的映射函数错误@高尔姆申克这个解决方案对你有用吗?如果是的话,你能发布你的解决方案片段吗?