对于可变长度特征,使用tf.train.SequenceExample而不是tf.train.Example有什么好处?

时间:2017-08-11 11:46:08

标签: tensorflow

最近我在TensorFlow中阅读了有关未记录的特征的this指南,因为我需要将可变长度序列作为输入传递。但是,我发现tf.train.SequenceExample的协议相对令人困惑(特别是由于缺少文档),并设法使用tf.train.Example构建输入管道,而不是很好。

使用tf.train.SequenceExample有什么好处吗?使用标准示例协议时,如果有一个用于可变长度序列的专用协议似乎是作弊,但它是否会产生任何后果?

2 个答案:

答案 0 :(得分:3)

您提供的链接列出了一些好处。您可以在parse_single_sequence_example

查看https://github.com/tensorflow/magenta/blob/master/magenta/common/sequence_example_lib.py的使用方式

如果您设法使用Example将数据导入模型,那么应该没问题。 SequenceExample只是为您的数据和一些使用它的实用程序提供了更多的结构。

答案 1 :(得分:0)

以下是ExampleSequenceExample协议缓冲区的定义,以及它们可能包含的所有原型:

message BytesList { repeated bytes value = 1; }
message FloatList { repeated float value = 1 [packed = true]; }
message Int64List { repeated int64 value = 1 [packed = true]; }
message Feature {
    oneof kind {
        BytesList bytes_list = 1;
        FloatList float_list = 2;
        Int64List int64_list = 3;
    }
};
message Features { map<string, Feature> feature = 1; };
message Example { Features features = 1; };

message FeatureList { repeated Feature feature = 1; };
message FeatureLists { map<string, FeatureList> feature_list = 1; };
message SequenceExample {
  Features context = 1;
  FeatureLists feature_lists = 2;
};

Example包含一个Features,其中包含从功能名称到Feature的映射,该映射包含一个bytes列表或一个float列表或int64列表。

SequenceExample还包含一个Features,但它还包含一个FeatureLists,其中包含从列表名称到FeatureList的映射,该映射包含一个{{ 1}}。因此,它可以完成Feature可以做的所有事情,甚至更多。但是,您真的需要额外的功能吗?它是做什么的?

由于每个Example都包含一个值列表,因此Feature是一个列表列表。这就是关键:如果您需要值列表列表,则需要FeatureList

例如,如果您处理文本,则可以将其表示为一个大字符串:

SequenceExample

或者您可以将其表示为单词和标记的列表:

from tensorflow.train import BytesList

BytesList(value=[b"This is the first sentence. And here's another."])

或者您可以分别代表每个句子。那是您需要列表列表的地方:

BytesList(value=[b"This", b"is", b"the", b"first", b"sentence", b".", b"And", b"here",
                 b"'s", b"another", b"."])

然后创建from tensorflow.train import BytesList, Feature, FeatureList s1 = BytesList(value=[b"This", b"is", b"the", b"first", b"sentence", b"."]) s2 = BytesList(value=[b"And", b"here", b"'s", b"another", b"."]) fl = FeatureList(feature=[Feature(bytes_list=s1), Feature(bytes_list=s2)])

SequenceExample

您可以序列化它,甚至可以将其保存到TFRecord文件中。

from tensorflow.train import SequenceExample, FeatureLists

seq = SequenceExample(feature_lists=FeatureLists(feature_list={
    "sentences": fl
}))

稍后,当您读取数据时,可以使用data = seq.SerializeToString() 对其进行解析。

相关问题