Tf-Ranking Transform Data to Elwc - Examplelistwithcontext Form

Tf-Ranking Transform Data to Elwc - Examplelistwithcontext Form

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.

For example, a students profile is:

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

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)

1 Answer

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.

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.

Save using 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.