我正在使用 tensorflow-ranking 构建排名模型.我正在尝试以 TFRecord 格式序列化数据集,并在训练时读回。
本教程没有说明如何执行此操作。有一些文档 here在示例数据格式上,但我很难理解:我不确定 serialized_context
或 serialized_examples
字段是什么,或者它们如何适合示例和我不确定代码块中的 Serialize()
函数是什么。
具体来说,如何以example-in-example格式写入和读取数据?
最佳答案
上下文是从特征名称到 tf.train.Feature
的映射。示例列表是从特征名称到 tf.train.Feature
的映射列表。一旦你有了这些,下面的代码将创建一个“example-in-example”:
context = {...}
examples = [{...}, {...}, ...]
serialized_context = tf.train.Example(features=tf.train.Features(feature=context)).SerializeToString()
serialized_examples = tf.train.BytesList()
for example in examples:
tf_example = tf.train.Example(features=tf.train.Features(feature=example))
serialized_examples.value.append(tf_example.SerializeToString())
example_in_example = tf.train.Example(features=tf.train.Features(feature={
'serialized_context': tf.train.Feature(bytes_list=tf.train.BytesList(value=[serialized_context])),
'serialized_examples': tf.train.Feature(bytes_list=serialized_examples)
}))
要回读示例,您可以调用
tfr.data.parse_from_example_in_example(example_pb,
context_feature_spec = context_feature_spec,
example_feature_spec = example_feature_spec)
其中 context_feature_spec
和 example_feature_spec
是从特征名称到 tf.io.FixedLenFeature
或 tf.io.VarLenFeature的映射
.
关于tensorflow - 如何以示例格式序列化数据以进行 tensorflow 排名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57481869/