首页 > 解决方案 > 从示例创建 TensorFlow 数据集

问题描述

我正在关注本教程: https ://medium.com/mostly-ai/tensorflow-records-what-they-are-and-how-to-use-them-c46bc4bbb564

我已经创建了一些SequentialExamples,我怎样才能tf.data.Dataset从这些创建一个?

我有一个股票市场价值的一维 Python 列表,并希望最终向神经网络提供值 t1、t2、t3 并使其预测 t4 和 t5。我将调用以下方法并创建许多示例,其中 X 是输入,Y 是预测。

def make_example(x=[1,2,3], y=[1,2]):
  x_feature = [tf.train.Feature(float_list=tf.train.FloatList(value=x))]
  y_feature = [tf.train.Feature(float_list=tf.train.FloatList(value=y))]

  x_flist = tf.train.FeatureList(feature=x_feature)
  y_flist = tf.train.FeatureList(feature=y_feature) 

  feature_lists = tf.train.FeatureLists(feature_list={
      "input_x": x_flist,
      "output_y": y_flist
  })

  example = tf.train.SequenceExample(feature_lists=feature_lists)
  print(example)
  return example

我想tf.data.Dataset从 Python 列表中创建一个tf.train.SequenceExample.

标签: tensorflow

解决方案


你需要打开作家

writer = tf.python_io.TFRecordWriter(file_name.tfrecords)
example = tf.train.SequenceExample(feature_lists=f_lists)
writer.write(example.SerializeToString())
writer.close()

然后创建解析函数

def parse_fn(example):
    example_features = {"input_x": x_flist,
                        "output_y": y_flist}
    parsed = tf.parse_single_example(serialized=example, features=example_features)

    return input_x, output_y

然后创建数据集

dataset = tf.data.TFRecordDataset(file_name.tfrecords)
dataset = dataset.shuffle().repeat()
dataset = dataset.map(parse_fn)

https://www.tensorflow.org/guide/datasets


推荐阅读