本文介绍了TF-Ranking 将数据转换为 ELWC - ExampleListWithContext 形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读所有指南、视频和所有内容,但我不知道如何将我的功能集转换为 ELWC 数据表格式以解决 TF-Rank ListWise 问题.没有对这种结构的描述.

I have read all the guides, videos, and everything, but I have no idea how to convert my feature set to an ELWC datasheet format for TF-Rank ListWise problem. There is no description of this structure.

例如,学生档案是:

Student ID  age  grade  math%   physics%   english%  art%  math_competit  language_competit Rank
  14588     16    k12     98      67         88      100   first_place        very_good       5

如果我在同一个班级有 20 名学生,我如何将这些数据转换为能够对每个年级进行列表预测(理论上每个年级有 3 个班级,20 名学生)

If I have 20 students in the same class, how can I transform this data to be able to make a listwise prediction for every grade ( theoretically in every grade has 3 class with 20 students)

推荐答案

ELWC 格式需要上下文"和示例功能".示例特征是对于查询列表中的每个项目都不同的特征.上下文特征是仅依赖于查询的特征.因此,每个查询都会有列表中每个项目的特征列表(示例特征),以及上下文特征的单个特征列表.

ELWC format requires 'context' and 'example features'. Example features are features that are different for every item in a query list. Context features are ones which are dependent only on the query.Therefore, every query will have a list of features for every item in the list (the Example features), and a single list of features for Context features.

要转换为 ELWC 格式,首先要收集给定查询的所有项目.下面的代码显示了一个包含两个项目以及一些上下文信息的查询.使用 input_pb2.ExampleListWithContext() 创建一个 ELWC 格式化程序的实例.然后,您所要做的就是提供上下文和示例.

To convert to ELWC format, start by gathering all the items for a given query. The code below shows a query with two items along with some context information. Use input_pb2.ExampleListWithContext() to create an instance of a ELWC formatter. Then all you have to do is feed in the context and examples.

使用 TFRecordWriter 保存.

from tensorflow_serving.apis import input_pb2
import tensorflow as tf

def _float_feature(value):
    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))


def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

context = {
    'custom_features_1': _float_feature(1.0),
    'utility': _int64_feature(1),
}
examples = [
        {
            'custom_features_1': _float_feature(1.0),
            'custom_features_2': _float_feature(1.5),
            'utility': _int64_feature(1),
        },
        {
            'custom_features_1': _float_feature(1.0),
            'custom_features_2': _float_feature(2.1),
            'utility': _int64_feature(0),
        }
]


def to_example(dictionary):
    return tf.train.Example(features=tf.train.Features(feature=dictionary))

ELWC = input_pb2.ExampleListWithContext()
ELWC.context.CopyFrom(to_example(context))
for expl in examples:
    example_features = ELWC.examples.add()
    example_features.CopyFrom(to_example(expl))

print(ELWC)

这篇关于TF-Ranking 将数据转换为 ELWC - ExampleListWithContext 形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:18