Make transformed features available outside model_fn
See original GitHub issueI 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_weightsalready 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:
- Created 4 years ago
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
Hi Milad,
You can actually call the
transformed_fnto obtain the dense features before you feed them intomodel_fn, and only use identity transform function as themodel_fnargument. See below for an example:And I believe you can use
context_featuresandexample_featuresfor your purpose.Let me know if this works for you.
@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.