{"id":24692,"library":"tensorflow-ranking","title":"TensorFlow Ranking","description":"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.","status":"active","version":"0.5.5","language":"python","source_language":"en","source_url":"https://github.com/tensorflow/ranking","tags":["ranking","learning-to-rank","tensorflow","keras","LTR","information-retrieval"],"install":[{"cmd":"pip install tensorflow-ranking","lang":"bash","label":"Install via pip"}],"dependencies":[{"reason":"Required core library for all operations.","package":"tensorflow","optional":false},{"reason":"Required for serving models.","package":"tensorflow-serving-api","optional":false}],"imports":[{"note":"tfr is the standard alias in all official examples.","wrong":"import tensorflow_ranking as tf_ranking","symbol":"tfr","correct":"import tensorflow_ranking as tfr"},{"note":"Full path required; 'tfr' is an alias, not a module.","wrong":"from tfr.keras import losses","symbol":"tfr.keras.losses","correct":"from tensorflow_ranking.python.keras import losses"},{"note":"Use the full module path for consistency.","wrong":"","symbol":"tfr.keras.metrics","correct":"from tensorflow_ranking.python.keras import metrics"}],"quickstart":{"code":"import tensorflow as tf\nimport tensorflow_ranking as tfr\n\n# Create a simple ranking model using Keras inputs\ninputs = {\n    'example_feature': tf.keras.Input(shape=(10,), dtype=tf.float32),\n    'list_size': tf.keras.Input(shape=(), dtype=tf.int32)\n}\n\n# Build a simple two-layer DNN ranking model\nhidden = tf.keras.layers.Dense(16, activation='relu')(inputs['example_feature'])\nscores = tf.keras.layers.Dense(1)(hidden)\n\n# Flatten scores per list for ranking loss\nlist_size = tf.squeeze(inputs['list_size'])\nscores = tf.reshape(scores, [-1, list_size])\n\nmodel = tf.keras.Model(inputs=inputs, outputs=scores)\n\n# Use a ranking loss (e.g., approxNDCG loss)\nloss = tfr.keras.losses.ApproxNDCGLoss()\nmodel.compile(optimizer='adam', loss=loss)\nprint('Model compiled successfully.')","lang":"python","description":"Minimal example of building and compiling a ranking model using TensorFlow Ranking with a DNN and approxNDCG loss."},"warnings":[{"fix":"Migrate to the Keras-based API using 'tfr.keras.losses', 'tfr.keras.metrics', and 'tfr.keras.model'.","message":"TensorFlow Ranking 0.5.0 removed the old RankingNetwork-based API. Code using 'tfr.estimator' or 'tfr.feature' may break.","severity":"breaking","affected_versions":">=0.5.0"},{"fix":"Use the new Keras layers and losses. See migration guide at https://github.com/tensorflow/ranking/blob/master/README.md.","message":"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.","severity":"breaking","affected_versions":">=0.4.0"},{"fix":"Use 'tfr.keras.model' or 'tfr.keras.task' for training.","message":"The 'tfr.estimator' module is deprecated as of 0.5.0 and will be removed in a future release.","severity":"deprecated","affected_versions":">=0.5.0"},{"fix":"Ensure TensorFlow 2.x is installed: pip install 'tensorflow>=2.0'.","message":"TensorFlow Ranking requires TensorFlow 2.x. Installing with TensorFlow 1.x may lead to import errors.","severity":"gotcha","affected_versions":"all"},{"fix":"Use the correct import statement as shown in quickstart.","message":"Importing 'tfr' directly without the full path can cause ambiguity. Always use 'import tensorflow_ranking as tfr'.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run 'pip install tensorflow-ranking'.","cause":"The package is not installed or installed incorrectly.","error":"ModuleNotFoundError: No module named 'tensorflow_ranking'"},{"fix":"Upgrade: 'pip install --upgrade tensorflow-ranking' or use a different loss like 'SoftmaxLoss'.","cause":"Using an older version of TensorFlow Ranking that does not have the ApproxNDCGLoss (introduced in 0.5.0).","error":"ImportError: cannot import name 'ApproxNDCGLoss' from 'tensorflow_ranking.python.keras.losses'"},{"fix":"Migrate to the Keras API: use 'tensorflow_ranking.python.keras' instead.","cause":"The 'estimator' module was removed in TensorFlow Ranking 0.5.0.","error":"AttributeError: module 'tensorflow_ranking' has no attribute 'estimator'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}