question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make transformed features available outside model_fn

See original GitHub issue

I have a pipeline similar to this example. More specifically, I use make_groupwise_ranking_fn to create my model_fn and I have a simple transform_fn which calls encode_listwise_features to turn the categorical input features into dense embeddings.

However, I need to implement a custom prediction routine and customize the export_keys. So as suggested in #6, I wrap the model function that I get from make_groupwise_ranking_fn in my own custom model function, which adds the extra prediction computations I need.

The problem is that this wrapper model function receives the sparse features in its features argument, but I need the dense ones. So, I tried calling the transform_fn again to turn these into dense embeddings. But this resulted in TF saying:

Variable encoding_layer/user_embedding/embedding_weights already exists. Disallowed.

And it recommends setting reuse properly for the scope. My understanding is that since the transform_fn has already been called (by the original model function) when it’s called a second time, it is trying to create a TF variable that exists. (But correct me if I’m wrong). So my current workaround is to modify the call to encode_listwise_features inside my transform_fn:

with tf.variable_scope('encoding', reuse=tf.AUTO_REUSE): context_features, example_features = tfr.feature.encode_listwise_features(...) I’m wondering if there is a way to make the dense features returned by the call to transform_fn (which currently happens inside model_fn) available outside the walls of TF-Ranking, so I won’t have to call the transform_fn again to get my dense features.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
HongleiZhuangcommented, Oct 1, 2019

Hi Milad,

You can actually call the transformed_fn to obtain the dense features before you feed them into model_fn, and only use identity transform function as the model_fn argument. See below for an example:

def redefined_model_fn(features, labels, mode, params, config):
  transform_fn = make_transform_fn()
  context_features, example_features = transform_fn(features, mode)
  transformed_features = example_features
  transformed_features.update(context_features)
  original_model_fn = tfr.model.make_groupwise_ranking_fn(
          group_score_fn=make_score_fn(),
          transform_fn=tfr.feature.make_identity_transform_fn(context_features.keys()),
          group_size=_GROUP_SIZE,
          ranking_head=ranking_head)
  estimator_spec = original_model_fn(transformed_features, labels, mode, params, config)
  return estimator_spec

And I believe you can use context_features and example_features for your purpose.

Let me know if this works for you.

0reactions
xuanhuiwangcommented, Nov 9, 2019

@MiladShahidi, sorry for my late reply. glad to know that @HongleiZhuang’s suggestion works. Let us know if there is additional need and we can figure out how to solve this inside the library.

Closing now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Polynomial Feature Transforms for Machine ...
The polynomial features transform is available in the scikit-learn Python machine learning library via the PolynomialFeatures class.
Read more >
Create New Features From Existing Features - OpenClassrooms
Feature engineering is the creation of new input or target features from existing features. The objective is to create ones that do a...
Read more >
9 Feature Transformation & Scaling Techniques| Boost Model ...
Feature Scaling and transformation help in bringing the features to the same scale and change into normal distribution.
Read more >
Modern comments in PowerPoint - Microsoft Support
Turn modern comments creation on or off. As we roll out this feature, the ability to create modern comments is on by default...
Read more >
Data types and transformations | Vertex AI - Google Cloud
The transformation indicates the function of a particular data feature. ... For details, see Feature type and availability at forecast.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found