tensorflow - 如何以示例格式序列化数据以进行 tensorflow 排名?

标签 tensorflow protocol-buffers

我正在使用 tensorflow-ranking 构建排名模型.我正在尝试以 TFRecord 格式序列化数据集,并在训练时读回。

本教程没有说明如何执行此操作。有一些文档 here在示例数据格式上,但我很难理解:我不确定 serialized_contextserialized_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_specexample_feature_spec 是从特征名称到 tf.io.FixedLenFeaturetf.io.VarLenFeature.

关于tensorflow - 如何以示例格式序列化数据以进行 tensorflow 排名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57481869/

相关文章:

python - 将 protobuf 对象写入 JSON 文件

python - 获取 TypeError : can't pickle _thread. RLock 对象

tensorflow - 使用 TensorFlow 对象检测 API 确定最大批量大小

hadoop - 如何使用 LZO 以外的压缩处理 Hadoop Map/Reduce 中的 Protocol Buffer 文件?

java - Protocol Buffer : reading all serialized messages from file

python - 通过TCP在python中接收分隔的Protobuf消息

tensorflow - 将卡住模型(.pb)转换为保存模型

javascript - Tensorflow.js SymbolicTensor 不被接受为输入

python - 多次拟合时 keras fit() 的历史

compact-framework - 为紧凑框架预编译 protobuf-net 类型模型