代码之家  ›  专栏  ›  技术社区  ›  krishnab

Tensorflow:序列对TFR记录的编码和解码有影响吗

  •  0
  • krishnab  · 技术社区  · 7 年前

    我有一些练习数据,我想编码成 TFRecord 格式化然后解码为 tf.features

    :是否需要以编码时的相同顺序解码数据集中的要素?换句话说,我似乎找不到在TFRecord中按字段名引用特性的方法。这一点非常重要,有两个原因。

    1. 我只是想让我的假设得到验证,这样我就知道如何避免将来破坏我的代码。下面是一些简单的代码,尽管这不是一个完整的示例。
    2. Python非常重视词典的使用 未订购 . 所以,当我使用一个应该是无序的数据结构时,如何保证序列呢?我不确定这是不是以我不知道的方式处理的。

    要将数据编码为TFRecord格式,可以执行以下操作:

    #Fields in Dataframe: ['DIVISION','SPORDER','PUMA','REGION']
    
    df = pd.DataFrame(...)
    with tf.python_io.TFRecordWriter('myfile.tfrecord') as writer:
    
        for row in df.itertuples():
            example = tf.train.Example(features=tf.train.Features(feature={
              'feat/division': tf.train.Feature(int64_list=tf.train.Int64List(value=row.DIVISION)),
              'label/sporder': tf.train.Feature(int64_list=tf.train.Int64List(value=row.SPORDER)),
              'feat/puma': tf.train.Feature(bytes_list=tf.train.BytesList(value=[row.PUMA])),
              'feat/region': tf.train.Feature(bytes_list=tf.train.BytesList(value=[row.REGION]))))
            writer.write(example.SerializeToString())
    

    然后要接收数据集,您需要下面的代码。请注意,这些字段是按顺序再次引用的。注意:我在TFRecords和解码表单中使用了相同的字典键,但我认为这不是必需的——只是一种方便。我不知道事情是不是应该这样?意思是,

    dataset = tf.data.TFRecordDataset('myfile.tfrecord')
    dataset = dataset.map(_parse_function)
    
    def _parse_function(example_proto):
        features = {'feat/division': tf.FixedLenFeature((), tf.string, default_value=""),
                    'label/sporder': tf.FixedLenFeature((), tf.int64, default_value=0),
                    'feat/puma': tf.VarLenFeature(dtype=tf.string),
                    'feat/region': tf.VarLenFeature(dtype=tf.string)}
    
        parsed_example = tf.parse_single_example(example_proto, features)
        parsed_label = parsed_example.pop("label/sporder", None)
    
    
        return parsed_example, parsed_label
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Vlad-HC    7 年前

    这个 tfrecord 格式使用 protobuf