【问题标题】:Reading sequential data from TFRecords files within the TensorFlow graph?从TensorFlow图中的TFRecords文件中读取顺序数据?
【发布时间】:2017-02-19 01:41:38
【问题描述】:

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

【问题讨论】:

    标签: tensorflow recurrent-neural-network sequential


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 序列上的地图功能不起作用,我得到地图功能未在嵌套数据集错误上实现。 @golmschenk 这个解决方案对你有用吗?如果是,您能否发布您的解决方案 sn-p?
    猜你喜欢
    • 2018-06-14
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多