对于可变长度特征,使用tf.train.SequenceExample优于tf.train.Example有什么优势?

Bru*_* KM 5 tensorflow

最近,我读在TensorFlow无证featuers指南,因为我需要通过可变长度的序列作为输入。但是,我发现该协议tf.train.SequenceExample相对容易混淆(特别是由于缺乏文档),并设法使用tf.train.Example罚款来构建输入管道。

使用有什么好处tf.train.SequenceExample吗?在存在专用于可变长度序列的协议时,使用标准示例协议似乎是骗子,但这会带来任何后果吗?

Min*_*ark 14

以下是协议缓冲区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;
};
Run Code Online (Sandbox Code Playgroud)

AnExample包含 a Features,其中包含从功能名称到 的映射Feature,其中包含bytes列表、float列表或int64列表。

ASequenceExample还包含 a Features,但它也包含 a FeatureLists,其中包含从列表名称到 的映射FeatureList,其中包含Feature. 所以它可以做所有Example能做的事情,甚至更多。但是你真的需要那个额外的功能吗?它有什么作用?

由于每个都Feature包含一个值列表,因此 aFeatureList是一个列表列表。这就是关键:如果您需要值列表的列表,那么您需要SequenceExample.

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

from tensorflow.train import BytesList

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

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

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

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

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)])
Run Code Online (Sandbox Code Playgroud)

然后创建SequenceExample

from tensorflow.train import SequenceExample, FeatureLists

seq = SequenceExample(feature_lists=FeatureLists(feature_list={
    "sentences": fl
}))
Run Code Online (Sandbox Code Playgroud)

您可以将其序列化,或许可以将其保存到 TFRecord 文件中。

data = seq.SerializeToString()
Run Code Online (Sandbox Code Playgroud)

稍后,当您读取数据时,您可以使用tf.io.parse_single_sequence_example().


iga*_*iga 4

您提供的链接列出了一些好处。您可以在此处查看如何使用 parse_single_sequence_example https://github.com/tensorflow/magenta/blob/master/magenta/common/sequence_example_lib.py

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