【问题标题】:How to create windowed multivariate dataset from SequenceExample TFRecord如何从 SequenceExample TFRecord 创建窗口化多元数据集
【发布时间】:2020-02-18 16:22:58
【问题描述】:

我正在尝试使用 tf.data.datasets 设置 Tensorflow 管道,以便将一些 TFRecord 加载到 Keras 模型中。这些数据是多元时间序列。

我目前使用的是 Tensorflow 2.0

首先我从 TFRecord 获取我的数据集并解析它:

dataset = tf.data.TFRecordDataset('...')

context_features = {...}
sequence_features = {...}

def _parse_function(example_proto):
  _, sequence =  tf.io.parse_single_sequence_example(example_proto,context_features, sequence_features)
  return sequence


dataset = dataset.map(_parse_function)

现在的问题是它给了我一个 MapDataset,里面有 EagerTensor 的字典:

for data in dataset.take(3):
  print(type(data))

<class 'dict'>
<class 'dict'>
<class 'dict'>

# which look like : {feature1 : EagerTensor, feature2 : EagerTensor ...}

由于这些字典,我似乎无法设法将这些数据进行批处理、混洗……以便之后在 LSTM 层中使用它们。比如这个:

def make_window_dataset(ds, window_size=5, shift=1, stride=1):
  windows = ds.window(window_size, shift=shift, stride=stride)

  def sub_to_batch(sub):
    return sub.values().batch(window_size, drop_remainder=True)

  windows = windows.flat_map(sub_to_batch)
  return windows

ds = make_window_dataset(dataset, 10)

gives me :

AttributeError: 'dict_values' object has no attribute 'batch'

感谢您的帮助。我的研究基于这个和其他 Tensorflow 助手:

https://www.tensorflow.org/guide/data#time_series_windowing

编辑:

我找到了解决问题的方法。我最终在我的解析函数中使用堆栈将解析给出的字典转换为 (None,11) 形状的张量:

def _parse_function(example_proto):
  # Parse the input `tf.Example` proto using the dictionary above.
  _, sequence =  tf.io.parse_single_sequence_example(example_proto,context_features, sequence_features)
  return tf.stack(list(sequence.values()), axis=1)

【问题讨论】:

    标签: python-3.x tensorflow-datasets tensorflow2.0 tf.keras


    【解决方案1】:

    在此处(回答部分)提供解决方案,即使它出现在问题部分中,也是为了社区的利益。

    parse_function中使用tf.stack将字典转换为形状为(None,11)的张量已经解决了这个问题。

    更改代码

    def _parse_function(example_proto):
      _, sequence =  tf.io.parse_single_sequence_example(example_proto,context_features, sequence_features)
      return sequence
    

    def _parse_function(example_proto):
      _, sequence =  tf.io.parse_single_sequence_example(example_proto,context_features, sequence_features)
      return tf.stack(list(sequence.values()), axis=1)
    

    【讨论】:

      猜你喜欢
      • 2022-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      相关资源
      最近更新 更多