TensorFlow Ranking
raw JSON → 0.5.5 verified Fri May 01 auth: no python
TensorFlow Ranking is a library for Learning to Rank (LTR) with TensorFlow. It provides ranking losses, metrics, layers, and pipelines for building and training ranking models. The current version is 0.5.5. It is actively maintained by TensorFlow, with releases approximately every few months.
pip install tensorflow-ranking Common errors
error ModuleNotFoundError: No module named 'tensorflow_ranking' ↓
cause The package is not installed or installed incorrectly.
fix
Run 'pip install tensorflow-ranking'.
error ImportError: cannot import name 'ApproxNDCGLoss' from 'tensorflow_ranking.python.keras.losses' ↓
cause Using an older version of TensorFlow Ranking that does not have the ApproxNDCGLoss (introduced in 0.5.0).
fix
Upgrade: 'pip install --upgrade tensorflow-ranking' or use a different loss like 'SoftmaxLoss'.
error AttributeError: module 'tensorflow_ranking' has no attribute 'estimator' ↓
cause The 'estimator' module was removed in TensorFlow Ranking 0.5.0.
fix
Migrate to the Keras API: use 'tensorflow_ranking.python.keras' instead.
Warnings
breaking TensorFlow Ranking 0.5.0 removed the old RankingNetwork-based API. Code using 'tfr.estimator' or 'tfr.feature' may break. ↓
fix Migrate to the Keras-based API using 'tfr.keras.losses', 'tfr.keras.metrics', and 'tfr.keras.model'.
breaking TensorFlow Ranking 0.4.0 introduced a new Keras API that is not backward compatible with the Estimator API. Old scripts using 'tfr.estimator.make_groupwise_ranking_estimator' require updates. ↓
fix Use the new Keras layers and losses. See migration guide at https://github.com/tensorflow/ranking/blob/master/README.md.
deprecated The 'tfr.estimator' module is deprecated as of 0.5.0 and will be removed in a future release. ↓
fix Use 'tfr.keras.model' or 'tfr.keras.task' for training.
gotcha TensorFlow Ranking requires TensorFlow 2.x. Installing with TensorFlow 1.x may lead to import errors. ↓
fix Ensure TensorFlow 2.x is installed: pip install 'tensorflow>=2.0'.
gotcha Importing 'tfr' directly without the full path can cause ambiguity. Always use 'import tensorflow_ranking as tfr'. ↓
fix Use the correct import statement as shown in quickstart.
Imports
- tfr wrong
import tensorflow_ranking as tf_rankingcorrectimport tensorflow_ranking as tfr - tfr.keras.losses wrong
from tfr.keras import lossescorrectfrom tensorflow_ranking.python.keras import losses - tfr.keras.metrics
from tensorflow_ranking.python.keras import metrics
Quickstart
import tensorflow as tf
import tensorflow_ranking as tfr
# Create a simple ranking model using Keras inputs
inputs = {
'example_feature': tf.keras.Input(shape=(10,), dtype=tf.float32),
'list_size': tf.keras.Input(shape=(), dtype=tf.int32)
}
# Build a simple two-layer DNN ranking model
hidden = tf.keras.layers.Dense(16, activation='relu')(inputs['example_feature'])
scores = tf.keras.layers.Dense(1)(hidden)
# Flatten scores per list for ranking loss
list_size = tf.squeeze(inputs['list_size'])
scores = tf.reshape(scores, [-1, list_size])
model = tf.keras.Model(inputs=inputs, outputs=scores)
# Use a ranking loss (e.g., approxNDCG loss)
loss = tfr.keras.losses.ApproxNDCGLoss()
model.compile(optimizer='adam', loss=loss)
print('Model compiled successfully.')