【问题标题】:How to serialize data in example-in-example format for tensorflow-ranking?如何以示例格式序列化数据以进行张量流排名?
【发布时间】:2019-08-17 14:58:42
【问题描述】:

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

本教程没有说明如何执行此操作。有一些关于示例数据格式的文档here,但我很难理解:我不确定serialized_contextserialized_examples 字段是什么或者它们如何适合示例,我不确定代码块中的Serialize() 函数是什么。

具体来说,如何以示例格式写入和读取数据?

【问题讨论】:

    标签: tensorflow protocol-buffers


    【解决方案1】:

    上下文是从特征名称到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 的映射。

    【讨论】:

    • 我无法让它工作。您能否提供一个包含一些随机数据的完整示例? (例如如何生成 tf.train.Feature,如何初始化 tf.io.FixedLenFeature)
    【解决方案2】:

    首先,我推荐阅读这篇文章,以确保你知道如何创建tf.Example 以及tf.SequenceExample(顺便说一下,这是 TF-Ranking 支持的另一种数据格式):

    Tensorflow Records? What they are and how to use them

    在本文的第二部分,您将看到tf.SequenceExample 有两个组成部分:1)上下文和2)序列(或示例)。这与Example-in-Example 试图实现的想法相同。基本上,上下文是独立于您想要排名的项目的一组特征(搜索中的搜索查询,或推荐系统中的用户特征),序列部分是项目列表(又名例子)。这可以是文档列表(搜索中)或电影列表(推荐中)。

    一旦你熟悉了tf.ExampleExample-in-Example 就会更容易理解。看看这段代码,了解如何创建EIE 实例:

    https://www.gitmemory.com/issue/tensorflow/ranking/95/518480361

    1) 将上下文特征捆绑在一个 tf.Example 对象中并对其进行序列化

    2) 在另一个 tf.Example 对象中捆绑序列(示例)特征(每个都可以包含值列表)并序列化这个。

    3) 将这些包装在父 tf.Example

    4)(如果您正在写入 tfrecords)序列化父 tf.Example 对象并写入您的 tfrecord 文件。

    【讨论】:

      猜你喜欢
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 2018-06-25
      • 2022-01-15
      • 1970-01-01
      • 2021-07-08
      相关资源
      最近更新 更多