├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── WORKSPACE
├── docs
├── BUILD
├── _book.yaml
├── _index.yaml
├── api_docs
│ └── python
│ │ ├── tfr.md
│ │ └── tfr
│ │ ├── _api_cache.json
│ │ ├── _redirects.yaml
│ │ ├── _toc.yaml
│ │ ├── all_symbols.md
│ │ ├── api_report.pb
│ │ ├── data.md
│ │ ├── data
│ │ ├── build_ranking_dataset.md
│ │ ├── build_ranking_dataset_with_parsing_fn.md
│ │ ├── build_ranking_serving_input_receiver_fn.md
│ │ ├── build_ranking_serving_input_receiver_fn_with_parsing_fn.md
│ │ ├── build_sequence_example_serving_input_receiver_fn.md
│ │ ├── build_tf_example_serving_input_receiver_fn.md
│ │ ├── make_parsing_fn.md
│ │ ├── parse_from_example_in_example.md
│ │ ├── parse_from_example_list.md
│ │ ├── parse_from_sequence_example.md
│ │ ├── parse_from_tf_example.md
│ │ └── read_batched_sequence_example_dataset.md
│ │ ├── extension.md
│ │ ├── extension
│ │ ├── premade.md
│ │ ├── premade
│ │ │ ├── TFRBertConfig.md
│ │ │ ├── TFRBertDataConfig.md
│ │ │ ├── TFRBertDataLoader.md
│ │ │ ├── TFRBertModelBuilder.md
│ │ │ ├── TFRBertModelConfig.md
│ │ │ ├── TFRBertScorer.md
│ │ │ ├── TFRBertTask.md
│ │ │ ├── TensorDict.md
│ │ │ └── tfrbert_task.md
│ │ ├── task.md
│ │ └── task
│ │ │ ├── FeatureSpec.md
│ │ │ ├── RankingDataConfig.md
│ │ │ ├── RankingDataLoader.md
│ │ │ ├── RankingTask.md
│ │ │ └── RankingTaskConfig.md
│ │ ├── keras.md
│ │ ├── keras
│ │ ├── layers.md
│ │ ├── layers
│ │ │ ├── Bilinear.md
│ │ │ ├── ConcatFeatures.md
│ │ │ ├── DocumentInteractionAttention.md
│ │ │ ├── FlattenList.md
│ │ │ ├── GAMLayer.md
│ │ │ ├── RestoreList.md
│ │ │ ├── SelfAttentionMask.md
│ │ │ └── create_tower.md
│ │ ├── losses.md
│ │ ├── losses
│ │ │ ├── ApproxMRRLoss.md
│ │ │ ├── ApproxNDCGLoss.md
│ │ │ ├── CalibratedSoftmaxLoss.md
│ │ │ ├── ClickEMLoss.md
│ │ │ ├── CoupledRankDistilLoss.md
│ │ │ ├── DCGLambdaWeight.md
│ │ │ ├── GumbelApproxNDCGLoss.md
│ │ │ ├── LabelDiffLambdaWeight.md
│ │ │ ├── ListMLELambdaWeight.md
│ │ │ ├── ListMLELoss.md
│ │ │ ├── MeanSquaredLoss.md
│ │ │ ├── MixtureEMLoss.md
│ │ │ ├── NDCGLambdaWeight.md
│ │ │ ├── NDCGLambdaWeightV2.md
│ │ │ ├── OrdinalLoss.md
│ │ │ ├── PairwiseHingeLoss.md
│ │ │ ├── PairwiseLogisticLoss.md
│ │ │ ├── PairwiseMSELoss.md
│ │ │ ├── PairwiseSoftZeroOneLoss.md
│ │ │ ├── PrecisionLambdaWeight.md
│ │ │ ├── RankingLossKey.md
│ │ │ ├── SigmoidCrossEntropyLoss.md
│ │ │ ├── SoftmaxLoss.md
│ │ │ ├── UniqueSoftmaxLoss.md
│ │ │ ├── YetiDCGLambdaWeight.md
│ │ │ ├── YetiLogisticLoss.md
│ │ │ └── get.md
│ │ ├── metrics.md
│ │ ├── metrics
│ │ │ ├── ARPMetric.md
│ │ │ ├── AlphaDCGMetric.md
│ │ │ ├── DCGMetric.md
│ │ │ ├── HitsMetric.md
│ │ │ ├── MRRMetric.md
│ │ │ ├── MeanAveragePrecisionMetric.md
│ │ │ ├── NDCGMetric.md
│ │ │ ├── OPAMetric.md
│ │ │ ├── PrecisionIAMetric.md
│ │ │ ├── PrecisionMetric.md
│ │ │ ├── RankingMetricKey.md
│ │ │ ├── RecallMetric.md
│ │ │ ├── default_keras_metrics.md
│ │ │ └── get.md
│ │ ├── model.md
│ │ ├── model
│ │ │ ├── AbstractModelBuilder.md
│ │ │ ├── DNNScorer.md
│ │ │ ├── FeatureSpecInputCreator.md
│ │ │ ├── GAMScorer.md
│ │ │ ├── InputCreator.md
│ │ │ ├── ModelBuilder.md
│ │ │ ├── ModelBuilderWithMask.md
│ │ │ ├── Preprocessor.md
│ │ │ ├── PreprocessorWithSpec.md
│ │ │ ├── Scorer.md
│ │ │ ├── TensorDict.md
│ │ │ ├── TensorLike.md
│ │ │ ├── TypeSpecInputCreator.md
│ │ │ ├── UnivariateScorer.md
│ │ │ └── create_keras_model.md
│ │ ├── pipeline.md
│ │ ├── pipeline
│ │ │ ├── AbstractDatasetBuilder.md
│ │ │ ├── AbstractPipeline.md
│ │ │ ├── BaseDatasetBuilder.md
│ │ │ ├── DatasetHparams.md
│ │ │ ├── DatasetHparams
│ │ │ │ └── dataset_reader.md
│ │ │ ├── ModelFitPipeline.md
│ │ │ ├── MultiLabelDatasetBuilder.md
│ │ │ ├── MultiTaskPipeline.md
│ │ │ ├── NullDatasetBuilder.md
│ │ │ ├── PipelineHparams.md
│ │ │ ├── SimpleDatasetBuilder.md
│ │ │ └── SimplePipeline.md
│ │ ├── saved_model.md
│ │ ├── saved_model
│ │ │ └── Signatures.md
│ │ ├── strategy_utils.md
│ │ ├── strategy_utils
│ │ │ ├── NullContextManager.md
│ │ │ ├── get_output_filepath.md
│ │ │ ├── get_strategy.md
│ │ │ └── strategy_scope.md
│ │ ├── utils.md
│ │ └── utils
│ │ │ ├── GainFunction.md
│ │ │ ├── deserialize_keras_object.md
│ │ │ ├── identity.md
│ │ │ ├── inverse.md
│ │ │ ├── is_greater_equal_1.md
│ │ │ ├── log2_inverse.md
│ │ │ ├── pow_minus_1.md
│ │ │ ├── serialize_keras_object.md
│ │ │ └── symmetric_log1p.md
│ │ ├── utils.md
│ │ └── utils
│ │ ├── LossFunction.md
│ │ ├── de_noise.md
│ │ ├── gather_per_row.md
│ │ ├── is_label_valid.md
│ │ ├── organize_valid_indices.md
│ │ ├── padded_nd_indices.md
│ │ ├── parse_keys_and_weights.md
│ │ ├── ragged_to_dense.md
│ │ ├── reshape_first_ndims.md
│ │ ├── reshape_to_2d.md
│ │ ├── shuffle_valid_indices.md
│ │ ├── sort_by_scores.md
│ │ └── sorted_ranks.md
├── build_docs.py
├── images
│ ├── gam-example.png
│ ├── ranking-overview.jpeg
│ ├── tensorflow_ranking_pipeline_class.jpg
│ ├── tfr-bert.jpeg
│ └── tfr-distributed-modules.jpeg
├── install.md
├── overview.md
├── ranking_pipeline.md
└── tutorials
│ ├── quickstart.ipynb
│ ├── ranking_dnn_distributed.ipynb
│ └── tfr_bert.ipynb
└── tensorflow_ranking
├── BUILD
├── __init__.py
├── datasets
└── rd_suite
│ ├── RD_Suite_Eval_Script.ipynb
│ └── README.md
├── examples
├── BUILD
├── data
│ ├── eval_elwc.tfrecord
│ ├── test.txt
│ ├── test_numerical_elwc.tfrecord
│ ├── train.txt
│ ├── train_elwc.tfrecord
│ ├── train_numerical_elwc.tfrecord
│ ├── vali.txt
│ ├── vali_numerical_elwc.tfrecord
│ └── vocab.txt
├── handling_sparse_features.ipynb
├── keras
│ ├── BUILD
│ ├── README.md
│ ├── antique_kpl_din.py
│ ├── antique_kpl_din_test.py
│ ├── antique_ragged.py
│ ├── antique_ragged_test.py
│ ├── keras_dnn_tfrecord.py
│ ├── keras_dnn_tfrecord_test.py
│ ├── landingpage_example.ipynb
│ ├── tfrbert_antique_train.py
│ ├── tfrbert_antique_train_config.yaml
│ ├── tfrbert_task_experiments.py
│ └── tfrbert_task_experiments_test.py
├── tf_ranking_canned_dnn.py
├── tf_ranking_canned_dnn_test.py
├── tf_ranking_canned_gam.py
├── tf_ranking_canned_gam_test.py
├── tf_ranking_libsvm.py
├── tf_ranking_libsvm_test.py
├── tf_ranking_tfrecord.py
└── tf_ranking_tfrecord_test.py
├── extension
├── BUILD
├── README.md
├── __init__.py
├── examples
│ ├── BUILD
│ ├── pipeline_example.py
│ └── pipeline_example_test.py
├── pipeline.py
├── pipeline_test.py
├── premade
│ ├── BUILD
│ ├── __init__.py
│ ├── tfrbert_task.py
│ └── tfrbert_task_test.py
├── task.py
├── task_test.py
└── testdata
│ ├── bert_lite_config.json
│ ├── bert_lite_tf2.ckpt.data-00000-of-00001
│ ├── bert_lite_tf2.ckpt.index
│ ├── bert_lite_vocab.txt
│ ├── tfrbert_elwc_test.tfrecord
│ └── tfrbert_elwc_train.tfrecord
├── python
├── BUILD
├── __init__.py
├── data.py
├── data_test.py
├── estimator.py
├── estimator_test.py
├── feature.py
├── feature_test.py
├── head.py
├── head_test.py
├── keras
│ ├── BUILD
│ ├── __init__.py
│ ├── canned
│ │ ├── BUILD
│ │ ├── __init__.py
│ │ ├── dnn.py
│ │ ├── dnn_test.py
│ │ ├── gam.py
│ │ └── gam_test.py
│ ├── estimator.py
│ ├── estimator_test.py
│ ├── feature.py
│ ├── feature_test.py
│ ├── layers.py
│ ├── layers_test.py
│ ├── losses.py
│ ├── losses_test.py
│ ├── metrics.py
│ ├── metrics_test.py
│ ├── model.py
│ ├── model_test.py
│ ├── network.py
│ ├── network_test.py
│ ├── pipeline.py
│ ├── pipeline_test.py
│ ├── saved_model.py
│ ├── saved_model_test.py
│ ├── strategy_utils.py
│ ├── strategy_utils_test.py
│ ├── utils.py
│ └── utils_test.py
├── losses.py
├── losses_impl.py
├── losses_impl_test.py
├── losses_test.py
├── metrics.py
├── metrics_impl.py
├── metrics_impl_test.py
├── metrics_test.py
├── model.py
├── model_test.py
├── utils.py
├── utils_test.py
└── version.py
├── research
├── BUILD
├── dasalc.py
└── dasalc_test.py
└── tools
└── pip_package
├── BUILD
├── build_pip_package.sh
└── setup.py
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to Contribute
2 |
3 | We'd love to accept your patches and contributions to this project. There are
4 | just a few small guidelines you need to follow.
5 |
6 | ## Contributor License Agreement
7 |
8 | Contributions to this project must be accompanied by a Contributor License
9 | Agreement. You (or your employer) retain the copyright to your contribution;
10 | this simply gives us permission to use and redistribute your contributions as
11 | part of the project. Head over to to see
12 | your current agreements on file or to sign a new one.
13 |
14 | You generally only need to submit a CLA once, so if you've already submitted one
15 | (even if it was for a different project), you probably don't need to do it
16 | again.
17 |
18 | ## Code Reviews
19 |
20 | All submissions, including submissions by project members, require review. We
21 | use GitHub pull requests for this purpose. Consult
22 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
23 | information on using pull requests.
24 |
25 | ## Community Guidelines
26 |
27 | This project follows
28 | [Google's Open Source Community Guidelines](https://opensource.google.com/conduct/).
29 |
--------------------------------------------------------------------------------
/WORKSPACE:
--------------------------------------------------------------------------------
1 | workspace(name = "org_tensorflow_ranking")
2 |
--------------------------------------------------------------------------------
/docs/BUILD:
--------------------------------------------------------------------------------
1 | # Docs
2 |
3 | # Placeholder: load py_binary
4 |
5 | licenses(["notice"])
6 |
7 | py_binary(
8 | name = "build_docs",
9 | srcs = ["build_docs.py"],
10 | python_version = "PY3",
11 | srcs_version = "PY2AND3",
12 | deps = [
13 | # py/absl:app dep,
14 | # py/absl/flags dep,
15 | # Placeholder: proto upb dep
16 | # py/tensorflow_docs/api_generator:generate_lib dep,
17 | # py/tensorflow_docs/api_generator:public_api dep,
18 | # py/tensorflow_models dep,
19 | "//tensorflow_ranking",
20 | "//tensorflow_ranking/extension/premade",
21 | ],
22 | )
23 |
--------------------------------------------------------------------------------
/docs/_book.yaml:
--------------------------------------------------------------------------------
1 | upper_tabs:
2 | # Tabs left of dropdown menu
3 | - include: /_upper_tabs_left.yaml
4 | - include: /api_docs/_upper_tabs_api.yaml
5 | # Dropdown menu
6 | - name: Resources
7 | path: /resources
8 | is_default: true
9 | menu:
10 | - include: /resources/_menu_toc.yaml
11 | lower_tabs:
12 |
13 | # Subsite tabs
14 | other:
15 | # Overview added automatically
16 | - name: Guide & Tutorials
17 | contents:
18 | - title: Overview
19 | path: /ranking/overview
20 | - title: Install
21 | path: /ranking/install
22 | - title: Ranking pipeline
23 | path: /ranking/ranking_pipeline
24 | - heading: Tutorials
25 | - title: Quickstart
26 | path: /ranking/tutorials/quickstart
27 | - title: Distributed ranking
28 | path: /ranking/tutorials/ranking_dnn_distributed
29 | - title: Passage ranking using TFR-BERT
30 | path: /ranking/tutorials/tfr_bert
31 |
32 | - name: API
33 | skip_translation: true
34 | contents:
35 | - title: All Symbols
36 | path: /ranking/api_docs/python/tfr/all_symbols
37 | - include: /ranking/api_docs/python/tfr/_toc.yaml
38 |
39 | - include: /_upper_tabs_right.yaml
40 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr.md:
--------------------------------------------------------------------------------
1 | description: TensorFlow Ranking, the project to build ranking models on
2 | TensorFlow.
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # Module: tfr
11 |
12 |
13 |
14 |
22 |
23 | TensorFlow Ranking, the project to build ranking models on TensorFlow.
24 |
25 | ## Modules
26 |
27 | [`data`](./tfr/data.md) module: Input data parsing for tf-ranking library.
28 |
29 | [`extension`](./tfr/extension.md) module: TensorFlow Ranking library.
30 |
31 | [`keras`](./tfr/keras.md) module: TensorFlow Ranking Keras Module.
32 |
33 | [`utils`](./tfr/utils.md) module: Utility functions for ranking library.
34 |
35 |
36 |
37 |
38 | Other Members |
39 |
40 |
41 |
42 | **version**
43 | |
44 |
45 | `'0.5.5.dev'`
46 | |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/_redirects.yaml:
--------------------------------------------------------------------------------
1 | redirects:
2 | - from: /ranking/api_docs/python/tfr/extension/premade/TensorLike
3 | to: /ranking/api_docs/python/tfr/keras/model/TensorLike
4 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TFRBertConfig
5 | to: /ranking/api_docs/python/tfr/extension/premade/TFRBertConfig
6 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TFRBertDataConfig
7 | to: /ranking/api_docs/python/tfr/extension/premade/TFRBertDataConfig
8 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TFRBertDataLoader
9 | to: /ranking/api_docs/python/tfr/extension/premade/TFRBertDataLoader
10 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TFRBertModelBuilder
11 | to: /ranking/api_docs/python/tfr/extension/premade/TFRBertModelBuilder
12 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TFRBertModelConfig
13 | to: /ranking/api_docs/python/tfr/extension/premade/TFRBertModelConfig
14 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TFRBertScorer
15 | to: /ranking/api_docs/python/tfr/extension/premade/TFRBertScorer
16 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TFRBertTask
17 | to: /ranking/api_docs/python/tfr/extension/premade/TFRBertTask
18 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TensorDict
19 | to: /ranking/api_docs/python/tfr/extension/premade/TensorDict
20 | - from: /ranking/api_docs/python/tfr/extension/premade/tfrbert_task/TensorLike
21 | to: /ranking/api_docs/python/tfr/keras/model/TensorLike
22 | - from: /ranking/api_docs/python/tfr/keras/utils/PositiveFunction
23 | to: /ranking/api_docs/python/tfr/keras/utils/GainFunction
24 | - from: /ranking/api_docs/python/tfr/keras/utils/RankDiscountFunction
25 | to: /ranking/api_docs/python/tfr/keras/utils/GainFunction
26 | - from: /ranking/api_docs/python/tfr/keras/utils/TensorLike
27 | to: /ranking/api_docs/python/tfr/keras/model/TensorLike
28 | - from: /ranking/api_docs/python/tfr/utils/MetricFunction
29 | to: /ranking/api_docs/python/tfr/utils/LossFunction
30 | - from: /ranking/api_docs/python/tfr/utils/TensorLike
31 | to: /ranking/api_docs/python/tfr/keras/model/TensorLike
32 | - from: /ranking/api_docs/python/tfr/utils/TransformationFunction
33 | to: /ranking/api_docs/python/tfr/keras/utils/GainFunction
34 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/api_report.pb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/docs/api_docs/python/tfr/api_report.pb
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/data/build_ranking_serving_input_receiver_fn_with_parsing_fn.md:
--------------------------------------------------------------------------------
1 | description: Returns a receiver function with the provided parsing_fn.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.data.build_ranking_serving_input_receiver_fn_with_parsing_fn
9 |
10 |
11 |
12 |
20 |
21 | Returns a receiver function with the provided `parsing_fn`.
22 |
23 |
24 | tfr.data.build_ranking_serving_input_receiver_fn_with_parsing_fn(
25 | parsing_fn, receiver_name, default_batch_size=None
26 | )
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `parsing_fn`
39 | |
40 |
41 | (function) It has a single argument parsing_fn(serialized).
42 | Users can customize this for their own data formats.
43 | |
44 |
45 |
46 | `receiver_name`
47 | |
48 |
49 | (string) The name for the receiver Tensor that contains the
50 | serialized data.
51 | |
52 |
53 |
54 | `default_batch_size`
55 | |
56 |
57 | (int) Number of instances expected per batch. Leave
58 | unset for variable batch size (recommended).
59 | |
60 |
61 |
62 |
63 |
64 |
65 |
66 | Returns |
67 |
68 |
69 | A `tf.estimator.export.ServingInputReceiver` object, which packages the
70 | placeholders and the resulting feature Tensors together.
71 | |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/data/build_sequence_example_serving_input_receiver_fn.md:
--------------------------------------------------------------------------------
1 | description: Creates a serving_input_receiver_fn for SequenceExample inputs.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.data.build_sequence_example_serving_input_receiver_fn
9 |
10 |
11 |
12 |
20 |
21 | Creates a serving_input_receiver_fn for `SequenceExample` inputs.
22 |
23 |
24 | tfr.data.build_sequence_example_serving_input_receiver_fn(
25 | input_size,
26 | context_feature_spec,
27 | example_feature_spec,
28 | default_batch_size=None
29 | )
30 |
31 |
32 |
33 |
34 | A string placeholder is used for inputs. Note that the context_feature_spec and
35 | example_feature_spec shouldn't contain weights, labels or training only features
36 | in general.
37 |
38 |
39 |
40 |
41 | Args |
42 |
43 |
44 |
45 | `input_size`
46 | |
47 |
48 | (int) The number of frames to keep in a SequenceExample. If
49 | specified, truncation or padding may happen. Otherwise, set it to None to
50 | allow dynamic list size (recommended).
51 | |
52 |
53 |
54 | `context_feature_spec`
55 | |
56 |
57 | (dict) Map from feature keys to `FixedLenFeature` or
58 | `VarLenFeature` values.
59 | |
60 |
61 |
62 | `example_feature_spec`
63 | |
64 |
65 | (dict) Map from feature keys to `FixedLenFeature` or
66 | `VarLenFeature` values.
67 | |
68 |
69 |
70 | `default_batch_size`
71 | |
72 |
73 | (int) Number of query examples expected per batch. Leave
74 | unset for variable batch size (recommended).
75 | |
76 |
77 |
78 |
79 |
80 |
81 |
82 | Returns |
83 |
84 |
85 | A `tf.estimator.export.ServingInputReceiver` object, which packages the
86 | placeholders and the resulting feature Tensors together.
87 | |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/data/build_tf_example_serving_input_receiver_fn.md:
--------------------------------------------------------------------------------
1 | description: Builds a serving input fn for tensorflow.training.Example.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.data.build_tf_example_serving_input_receiver_fn
9 |
10 |
11 |
12 |
20 |
21 | Builds a serving input fn for `tensorflow.training.Example`.
22 |
23 |
24 | tfr.data.build_tf_example_serving_input_receiver_fn(
25 | context_feature_spec,
26 | example_feature_spec,
27 | size_feature_name=None,
28 | mask_feature_name=None,
29 | default_batch_size=None
30 | )
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | Args |
39 |
40 |
41 |
42 | `context_feature_spec`
43 | |
44 |
45 | (dict) Map from feature keys to `FixedLenFeature`,
46 | `VarLenFeature` or `RaggedFeature` values.
47 | |
48 |
49 |
50 | `example_feature_spec`
51 | |
52 |
53 | (dict) Map from feature keys to `FixedLenFeature`,
54 | `VarLenFeature` or `RaggedFeature` values.
55 | |
56 |
57 |
58 | `size_feature_name`
59 | |
60 |
61 | (str) Name of feature for example list sizes. Populates
62 | the feature dictionary with a `tf.int32` Tensor of value 1, and of shape
63 | [batch_size] for this feature name. If None, which is default, this
64 | feature is not generated.
65 | |
66 |
67 |
68 | `mask_feature_name`
69 | |
70 |
71 | (str) Name of feature for example list masks. Populates
72 | the feature dictionary with a `tf.bool` Tensor of shape [batch_size,
73 | list_size] for this feature name. If None, which is default, this feature
74 | is not generated.
75 | |
76 |
77 |
78 | `default_batch_size`
79 | |
80 |
81 | (int) Number of instances expected per batch. Leave
82 | unset for variable batch size (recommended).
83 | |
84 |
85 |
86 |
87 |
88 |
89 |
90 | Returns |
91 |
92 |
93 | A `tf.estimator.export.ServingInputReceiver` object, which packages the
94 | placeholders and the resulting feature Tensors together.
95 | |
96 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/data/parse_from_tf_example.md:
--------------------------------------------------------------------------------
1 | description: Parse function to convert tf.train.Example to feature maps.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.data.parse_from_tf_example
9 |
10 |
11 |
12 |
20 |
21 | Parse function to convert `tf.train.Example` to feature maps.
22 |
23 |
24 | tfr.data.parse_from_tf_example(
25 | serialized,
26 | context_feature_spec=None,
27 | example_feature_spec=None,
28 | size_feature_name=None,
29 | mask_feature_name=None
30 | )
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | Args |
39 |
40 |
41 |
42 | `serialized`
43 | |
44 |
45 | (`tf.train.Example`) A serialized proto object containing
46 | context and example features.
47 | |
48 |
49 |
50 | `context_feature_spec`
51 | |
52 |
53 | (dict) A mapping from feature keys to
54 | `FixedLenFeature`, `VarLenFeature` or `RaggedFeature` values for context
55 | in `tf.train.Example` proto.
56 | |
57 |
58 |
59 | `example_feature_spec`
60 | |
61 |
62 | (dict) A mapping from feature keys to
63 | `FixedLenFeature`, `VarLenFeature` or `RaggedFeature` values for examples
64 | in `tf.train.Example` proto.
65 | |
66 |
67 |
68 | `size_feature_name`
69 | |
70 |
71 | (str) Name of feature for example list sizes. Populates
72 | the feature dictionary with a `tf.int32` Tensor of shape [batch_size] for
73 | this feature name. If None, which is default, this feature is not
74 | generated.
75 | |
76 |
77 |
78 | `mask_feature_name`
79 | |
80 |
81 | (str) Name of feature for example list masks. Populates
82 | the feature dictionary with a `tf.bool` Tensor of shape [batch_size,
83 | list_size] for this feature name. If None, which is default, this feature
84 | is not generated.
85 | |
86 |
87 |
88 |
89 |
90 |
91 |
92 | Returns |
93 |
94 |
95 | A mapping from feature keys to `Tensor`, `SparseTensor` or `RaggedTensor`.
96 | |
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension.md:
--------------------------------------------------------------------------------
1 | description: TensorFlow Ranking library.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | # Module: tfr.extension
12 |
13 |
14 |
15 |
23 |
24 | TensorFlow Ranking library.
25 |
26 | ## Modules
27 |
28 | [`premade`](../tfr/extension/premade.md) module: TensorFlow Ranking Premade
29 | Orbit Task Module.
30 |
31 | [`task`](../tfr/extension/task.md) module: Orbit task for TF-Ranking.
32 |
33 |
34 |
35 |
36 |
37 | Other Members |
38 |
39 |
40 |
41 | absolute_import
42 | |
43 |
44 | Instance of `__future__._Feature`
45 | |
46 |
47 |
48 | division
49 | |
50 |
51 | Instance of `__future__._Feature`
52 | |
53 |
54 |
55 | print_function
56 | |
57 |
58 | Instance of `__future__._Feature`
59 | |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension/premade.md:
--------------------------------------------------------------------------------
1 | description: TensorFlow Ranking Premade Orbit Task Module.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # Module: tfr.extension.premade
11 |
12 |
13 |
14 |
22 |
23 | TensorFlow Ranking Premade Orbit Task Module.
24 |
25 | Note: First - These APIs require These APS require the
26 | `tensorflow_models`package. You can install it with `pip install
27 | tf-models-official`. Second - Nothing under
28 | `tensorflow_ranking.extension.premade` is imported by default. To use these APIs
29 | import `premade` in your code: `import tensorflow_ranking.extension.premade`.
30 |
31 | ## Modules
32 |
33 | [`tfrbert_task`](../../tfr/extension/premade/tfrbert_task.md) module: TF-Ranking
34 | BERT task.
35 |
36 | ## Classes
37 |
38 | [`class TFRBertConfig`](../../tfr/extension/premade/TFRBertConfig.md): The
39 | tf-ranking BERT task config.
40 |
41 | [`class TFRBertDataConfig`](../../tfr/extension/premade/TFRBertDataConfig.md):
42 | Data config for TFR-BERT task.
43 |
44 | [`class TFRBertDataLoader`](../../tfr/extension/premade/TFRBertDataLoader.md): A
45 | class to load dataset for TFR-BERT task.
46 |
47 | [`class TFRBertModelBuilder`](../../tfr/extension/premade/TFRBertModelBuilder.md):
48 | Model builder for TFR-BERT models.
49 |
50 | [`class TFRBertModelConfig`](../../tfr/extension/premade/TFRBertModelConfig.md):
51 | A TFR-BERT model configuration.
52 |
53 | [`class TFRBertScorer`](../../tfr/extension/premade/TFRBertScorer.md):
54 | Univariate BERT-based scorer.
55 |
56 | [`class TFRBertTask`](../../tfr/extension/premade/TFRBertTask.md): Task object
57 | for tf-ranking BERT.
58 |
59 | ## Type Aliases
60 |
61 | [`TensorDict`](../../tfr/extension/premade/TensorDict.md)
62 |
63 | [`TensorLike`](../../tfr/keras/model/TensorLike.md)
64 |
65 |
66 |
67 |
68 | Other Members |
69 |
70 |
71 |
72 | DOCUMENT_ID
73 | |
74 |
75 | `'document_id'`
76 | |
77 |
78 |
79 | QUERY_ID
80 | |
81 |
82 | `'query_id'`
83 | |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension/premade/TFRBertDataLoader.md:
--------------------------------------------------------------------------------
1 | description: A class to load dataset for TFR-BERT task.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.extension.premade.TFRBertDataLoader
11 |
12 |
13 |
14 |
22 |
23 | A class to load dataset for TFR-BERT task.
24 |
25 | Inherits From:
26 | [`RankingDataLoader`](../../../tfr/extension/task/RankingDataLoader.md)
27 |
28 |
29 | View aliases
30 |
31 | Main aliases
32 |
`tfr.extension.premade.tfrbert_task.TFRBertDataLoader`
33 |
34 |
35 |
36 |
37 | tfr.extension.premade.TFRBertDataLoader(
38 | params, label_spec: Tuple[str, tf.io.FixedLenFeature] = None, **kwargs
39 | )
40 |
41 |
42 |
43 |
44 | ## Methods
45 |
46 | load
47 |
48 | View
49 | source
50 |
51 |
52 | load(
53 | input_context: Optional[tf.distribute.InputContext] = None
54 | ) -> tf.data.Dataset
55 |
56 |
57 | Returns a tf.dataset.Dataset.
58 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension/premade/TFRBertScorer.md:
--------------------------------------------------------------------------------
1 | description: Univariate BERT-based scorer.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.extension.premade.TFRBertScorer
11 |
12 |
13 |
14 |
22 |
23 | Univariate BERT-based scorer.
24 |
25 | Inherits From:
26 | [`UnivariateScorer`](../../../tfr/keras/model/UnivariateScorer.md),
27 | [`Scorer`](../../../tfr/keras/model/Scorer.md)
28 |
29 |
30 | View aliases
31 |
32 | Main aliases
33 |
`tfr.extension.premade.tfrbert_task.TFRBertScorer`
34 |
35 |
36 |
37 |
38 | tfr.extension.premade.TFRBertScorer(
39 | encoder: tf.keras.Model,
40 | bert_output_dropout: float,
41 | name: str = 'tfrbert',
42 | **kwargs
43 | )
44 |
45 |
46 |
47 |
48 | ## Methods
49 |
50 | __call__
51 |
52 | View
53 | source
54 |
55 |
56 | __call__(
57 | context_features: tfr.keras.model.TensorDict
,
58 | example_features: tfr.keras.model.TensorDict
,
59 | mask: tf.Tensor
60 | ) -> Union[tf.Tensor, tfr.keras.model.TensorDict
]
61 |
62 |
63 | See `Scorer`.
64 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension/premade/TensorDict.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | # tfr.extension.premade.TensorDict
7 |
8 |
9 | This symbol is a **type alias**.
10 |
11 | #### Source:
12 |
13 |
14 | TensorDict = dict[
15 | str,
16 | tfr.keras.model.TensorLike
17 | ]
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension/premade/tfrbert_task.md:
--------------------------------------------------------------------------------
1 | description: TF-Ranking BERT task.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # Module: tfr.extension.premade.tfrbert_task
11 |
12 |
13 |
14 |
22 |
23 | TF-Ranking BERT task.
24 |
25 | ## Classes
26 |
27 | [`class TFRBertConfig`](../../../tfr/extension/premade/TFRBertConfig.md): The
28 | tf-ranking BERT task config.
29 |
30 | [`class TFRBertDataConfig`](../../../tfr/extension/premade/TFRBertDataConfig.md):
31 | Data config for TFR-BERT task.
32 |
33 | [`class TFRBertDataLoader`](../../../tfr/extension/premade/TFRBertDataLoader.md):
34 | A class to load dataset for TFR-BERT task.
35 |
36 | [`class TFRBertModelBuilder`](../../../tfr/extension/premade/TFRBertModelBuilder.md):
37 | Model builder for TFR-BERT models.
38 |
39 | [`class TFRBertModelConfig`](../../../tfr/extension/premade/TFRBertModelConfig.md):
40 | A TFR-BERT model configuration.
41 |
42 | [`class TFRBertScorer`](../../../tfr/extension/premade/TFRBertScorer.md):
43 | Univariate BERT-based scorer.
44 |
45 | [`class TFRBertTask`](../../../tfr/extension/premade/TFRBertTask.md): Task
46 | object for tf-ranking BERT.
47 |
48 | ## Type Aliases
49 |
50 | [`TensorDict`](../../../tfr/extension/premade/TensorDict.md)
51 |
52 | [`TensorLike`](../../../tfr/keras/model/TensorLike.md)
53 |
54 |
55 |
56 |
57 | Other Members |
58 |
59 |
60 |
61 | DOCUMENT_ID
62 | |
63 |
64 | `'document_id'`
65 | |
66 |
67 |
68 | QUERY_ID
69 | |
70 |
71 | `'query_id'`
72 | |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension/task.md:
--------------------------------------------------------------------------------
1 | description: Orbit task for TF-Ranking.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # Module: tfr.extension.task
11 |
12 |
13 |
14 |
22 |
23 | Orbit task for TF-Ranking.
24 |
25 | Note: First - These APIs require These APS require the
26 | `tensorflow_models`package. You can install it with `pip install
27 | tf-models-official`. Second - Nothing under
28 | `tensorflow_ranking.extension.premade` is imported by default. To use these APIs
29 | import `premade` in your code: `import tensorflow_ranking.extension.premade`.
30 |
31 | ## Classes
32 |
33 | [`class RankingDataConfig`](../../tfr/extension/task/RankingDataConfig.md): Data
34 | set config.
35 |
36 | [`class RankingDataLoader`](../../tfr/extension/task/RankingDataLoader.md): A
37 | class to load dataset for ranking task.
38 |
39 | [`class RankingTask`](../../tfr/extension/task/RankingTask.md): Task object for
40 | TF-Ranking.
41 |
42 | [`class RankingTaskConfig`](../../tfr/extension/task/RankingTaskConfig.md): The
43 | TF-Ranking task config.
44 |
45 | ## Type Aliases
46 |
47 | [`FeatureSpec`](../../tfr/extension/task/FeatureSpec.md)
48 |
49 |
50 |
51 |
52 | Other Members |
53 |
54 |
55 |
56 | DATASET_FN_MAP
57 | |
58 |
59 | `{
60 | 'textline': ,
61 | 'tfrecord':
62 | }`
63 | |
64 |
65 |
66 | MASK
67 | |
68 |
69 | `'example_list_mask'`
70 | |
71 |
72 |
73 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension/task/FeatureSpec.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | # tfr.extension.task.FeatureSpec
7 |
8 |
9 | This symbol is a **type alias**.
10 |
11 | #### Source:
12 |
13 |
14 | FeatureSpec = dict[
15 | str,
16 | Union[tensorflow.python.ops.parsing_config.FixedLenFeature, tensorflow.python.ops.parsing_config.VarLenFeature, tensorflow.python.ops.parsing_config.RaggedFeature]
17 | ]
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/extension/task/RankingDataLoader.md:
--------------------------------------------------------------------------------
1 | description: A class to load dataset for ranking task.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.extension.task.RankingDataLoader
11 |
12 |
13 |
14 |
22 |
23 | A class to load dataset for ranking task.
24 |
25 |
26 | tfr.extension.task.RankingDataLoader(
27 | params,
28 | context_feature_spec: tfr.extension.task.FeatureSpec
= None,
29 | example_feature_spec: tfr.extension.task.FeatureSpec
= None,
30 | label_spec: Tuple[str, tf.io.FixedLenFeature] = None,
31 | dataset_fn: Optional[Callable[[], tf.data.Dataset]] = None
32 | )
33 |
34 |
35 |
36 |
37 | ## Methods
38 |
39 | load
40 |
41 | View
42 | source
43 |
44 |
45 | load(
46 | input_context: Optional[tf.distribute.InputContext] = None
47 | ) -> tf.data.Dataset
48 |
49 |
50 | Returns a tf.dataset.Dataset.
51 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras.md:
--------------------------------------------------------------------------------
1 | description: TensorFlow Ranking Keras Module.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # Module: tfr.keras
9 |
10 |
11 |
12 |
20 |
21 | TensorFlow Ranking Keras Module.
22 |
23 | ## Modules
24 |
25 | [`layers`](../tfr/keras/layers.md) module: Defines Keras Layers for TF-Ranking.
26 |
27 | [`losses`](../tfr/keras/losses.md) module: Keras losses in TF-Ranking.
28 |
29 | [`metrics`](../tfr/keras/metrics.md) module: Keras metrics in TF-Ranking.
30 |
31 | [`model`](../tfr/keras/model.md) module: Ranking model utilities and classes in
32 | tfr.keras.
33 |
34 | [`pipeline`](../tfr/keras/pipeline.md) module: Ranking pipeline to train
35 | tf.keras.Model in tfr.keras.
36 |
37 | [`saved_model`](../tfr/keras/saved_model.md) module: SavedModel utilities for
38 | TF-Ranking.
39 |
40 | [`strategy_utils`](../tfr/keras/strategy_utils.md) module: tf.distribute
41 | strategy utils for Ranking pipeline in tfr.keras.
42 |
43 | [`utils`](../tfr/keras/utils.md) module: Utils for tfr.keras.
44 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/layers.md:
--------------------------------------------------------------------------------
1 | description: Defines Keras Layers for TF-Ranking.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # Module: tfr.keras.layers
9 |
10 |
11 |
12 |
20 |
21 | Defines Keras Layers for TF-Ranking.
22 |
23 | ## Classes
24 |
25 | [`class Bilinear`](../../tfr/keras/layers/Bilinear.md): A Keras Layer makes
26 | bilinear interaction of two vectors.
27 |
28 | [`class ConcatFeatures`](../../tfr/keras/layers/ConcatFeatures.md): Concatenates
29 | context features and example features in a listwise manner.
30 |
31 | [`class DocumentInteractionAttention`](../../tfr/keras/layers/DocumentInteractionAttention.md):
32 | Cross Document Interaction Attention layer.
33 |
34 | [`class FlattenList`](../../tfr/keras/layers/FlattenList.md): Layer to flatten
35 | the example list.
36 |
37 | [`class GAMLayer`](../../tfr/keras/layers/GAMLayer.md): Defines a generalized
38 | additive model (GAM) layer.
39 |
40 | [`class RestoreList`](../../tfr/keras/layers/RestoreList.md): Output layer to
41 | restore listwise output shape.
42 |
43 | [`class SelfAttentionMask`](../../tfr/keras/layers/SelfAttentionMask.md): Create
44 | 3D attention mask from a 2D tensor mask.
45 |
46 | ## Functions
47 |
48 | [`create_tower(...)`](../../tfr/keras/layers/create_tower.md): Creates a
49 | feed-forward network as `tf.keras.Sequential`.
50 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/losses/LabelDiffLambdaWeight.md:
--------------------------------------------------------------------------------
1 | description: Keras serializable class for LabelDiffLambdaWeight.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | # tfr.keras.losses.LabelDiffLambdaWeight
13 |
14 |
15 |
16 |
24 |
25 | Keras serializable class for LabelDiffLambdaWeight.
26 |
27 |
28 | tfr.keras.losses.LabelDiffLambdaWeight(
29 | **kwargs
30 | )
31 |
32 |
33 |
34 |
35 | ## Methods
36 |
37 | get_config
38 |
39 | View
40 | source
41 |
42 |
43 | get_config() -> Dict[str, Any]
44 |
45 |
46 | individual_weights
47 |
48 | View
49 | source
50 |
51 |
52 | individual_weights(
53 | labels, ranks
54 | )
55 |
56 |
57 | Returns the weight `Tensor` for individual examples.
58 |
59 |
60 |
61 |
62 | Args |
63 |
64 |
65 |
66 | `labels`
67 | |
68 |
69 | A dense `Tensor` of labels with shape [batch_size, list_size].
70 | |
71 |
72 |
73 | `ranks`
74 | |
75 |
76 | A dense `Tensor` of ranks with the same shape as `labels` that are
77 | sorted by logits.
78 | |
79 |
80 |
81 |
82 |
83 |
84 |
85 | Returns |
86 |
87 |
88 | A `Tensor` that can weight individual examples.
89 | |
90 |
91 |
92 |
93 |
94 | pair_weights
95 |
96 | View
97 | source
98 |
99 |
100 | pair_weights(
101 | labels, ranks
102 | )
103 |
104 |
105 | Returns the absolute label difference for each pair.
106 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/losses/ListMLELambdaWeight.md:
--------------------------------------------------------------------------------
1 | description: LambdaWeight for ListMLE cost function.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | # tfr.keras.losses.ListMLELambdaWeight
13 |
14 |
15 |
16 |
24 |
25 | LambdaWeight for ListMLE cost function.
26 |
27 |
28 | tfr.keras.losses.ListMLELambdaWeight(
29 | rank_discount_fn: Optional[tfr.keras.utils.GainFunction
] = None,
30 | **kwargs
31 | )
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | Args |
40 |
41 |
42 |
43 | `rank_discount_fn`
44 | |
45 |
46 | (function) The rank discount function.
47 | |
48 |
49 |
50 |
51 | ## Methods
52 |
53 | get_config
54 |
55 | View
56 | source
57 |
58 |
59 | get_config() -> Dict[str, Any]
60 |
61 |
62 | individual_weights
63 |
64 | View
65 | source
66 |
67 |
68 | individual_weights(
69 | labels, ranks
70 | )
71 |
72 |
73 | See `_LambdaWeight`.
74 |
75 | pair_weights
76 |
77 | View
78 | source
79 |
80 |
81 | pair_weights(
82 | labels, ranks
83 | )
84 |
85 |
86 | See `_LambdaWeight`.
87 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/losses/NDCGLambdaWeightV2.md:
--------------------------------------------------------------------------------
1 | description: Keras serializable class for NDCG LambdaWeight V2 for topn.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | # tfr.keras.losses.NDCGLambdaWeightV2
13 |
14 |
15 |
16 |
24 |
25 | Keras serializable class for NDCG LambdaWeight V2 for topn.
26 |
27 |
28 | tfr.keras.losses.NDCGLambdaWeightV2(
29 | topn: Optional[int] = None,
30 | gain_fn: Optional[tfr.keras.utils.GainFunction
] = None,
31 | rank_discount_fn: Optional[tfr.keras.utils.GainFunction
] = None,
32 | **kwargs
33 | )
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | Args |
42 |
43 |
44 |
45 | `topn`
46 | |
47 |
48 | (int) The topn for the DCG metric.
49 | |
50 |
51 |
52 | `gain_fn`
53 | |
54 |
55 | (function) Transforms labels.
56 | |
57 |
58 |
59 | `rank_discount_fn`
60 | |
61 |
62 | (function) The rank discount function.
63 | |
64 |
65 |
66 | `normalized`
67 | |
68 |
69 | (bool) If True, normalize weight by the max DCG.
70 | |
71 |
72 |
73 |
74 | ## Methods
75 |
76 | get_config
77 |
78 | View
79 | source
80 |
81 |
82 | get_config() -> Dict[str, Any]
83 |
84 |
85 | individual_weights
86 |
87 | View
88 | source
89 |
90 |
91 | individual_weights(
92 | labels, ranks
93 | )
94 |
95 |
96 | See `_LambdaWeight`.
97 |
98 | pair_weights
99 |
100 | View
101 | source
102 |
103 |
104 | pair_weights(
105 | labels, ranks
106 | )
107 |
108 |
109 | See `_LambdaWeight`.
110 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/losses/YetiDCGLambdaWeight.md:
--------------------------------------------------------------------------------
1 | description: Keras serializable class for YetiDCGLambdaWeight.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | # tfr.keras.losses.YetiDCGLambdaWeight
13 |
14 |
15 |
16 |
24 |
25 | Keras serializable class for YetiDCGLambdaWeight.
26 |
27 |
28 | tfr.keras.losses.YetiDCGLambdaWeight(
29 | topn: Optional[int] = None,
30 | gain_fn: Optional[tfr.keras.utils.GainFunction
] = None,
31 | rank_discount_fn: Optional[tfr.keras.utils.GainFunction
] = None,
32 | normalized: bool = False,
33 | **kwargs
34 | )
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | Args |
43 |
44 |
45 |
46 | `topn`
47 | |
48 |
49 | (int) The topn for the DCG metric.
50 | |
51 |
52 |
53 | `gain_fn`
54 | |
55 |
56 | (function) Transforms labels.
57 | |
58 |
59 |
60 | `rank_discount_fn`
61 | |
62 |
63 | (function) The rank discount function.
64 | |
65 |
66 |
67 | `normalized`
68 | |
69 |
70 | (bool) If True, normalize weight by the max DCG.
71 | |
72 |
73 |
74 |
75 | ## Methods
76 |
77 | get_config
78 |
79 | View
80 | source
81 |
82 |
83 | get_config() -> Dict[str, Any]
84 |
85 |
86 | individual_weights
87 |
88 | View
89 | source
90 |
91 |
92 | individual_weights(
93 | labels, ranks
94 | )
95 |
96 |
97 | See `_LambdaWeight`.
98 |
99 | pair_weights
100 |
101 | View
102 | source
103 |
104 |
105 | pair_weights(
106 | labels: tf.Tensor, ranks: tf.Tensor
107 | ) -> tf.Tensor
108 |
109 |
110 | See `_LambdaWeight`.
111 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/losses/get.md:
--------------------------------------------------------------------------------
1 | description: Factory method to get a ranking loss class.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.losses.get
9 |
10 |
11 |
12 |
20 |
21 | Factory method to get a ranking loss class.
22 |
23 |
24 | tfr.keras.losses.get(
25 | loss: str,
26 | reduction: tf.losses.Reduction = tf.losses.Reduction.AUTO,
27 | lambda_weight: Optional[losses_impl._LambdaWeight] = None,
28 | name: Optional[str] = None,
29 | **kwargs
30 | ) -> tf.keras.losses.Loss
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 | Args |
39 |
40 |
41 |
42 | `loss`
43 | |
44 |
45 | (str) An attribute of `RankingLossKey`, defining which loss object to
46 | return.
47 | |
48 |
49 |
50 | `reduction`
51 | |
52 |
53 | (enum) An enum of strings indicating the loss reduction type.
54 | See type definition in the `tf.compat.v2.losses.Reduction`.
55 | |
56 |
57 |
58 | `lambda_weight`
59 | |
60 |
61 | (losses_impl._LambdaWeight) A lambda object for ranking
62 | metric optimization.
63 | |
64 |
65 |
66 | `name`
67 | |
68 |
69 | (optional) (str) Name of loss.
70 | |
71 |
72 |
73 | `**kwargs`
74 | |
75 |
76 | Keyword arguments for the loss object.
77 | |
78 |
79 |
80 |
81 |
82 |
83 |
84 | Returns |
85 |
86 |
87 | A ranking loss instance. See `_RankingLoss` signature for more details.
88 | |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 | Raises |
97 |
98 |
99 |
100 | `ValueError`
101 | |
102 |
103 | If loss_key is unsupported.
104 | |
105 |
106 |
107 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/metrics.md:
--------------------------------------------------------------------------------
1 | description: Keras metrics in TF-Ranking.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | # Module: tfr.keras.metrics
12 |
13 |
14 |
15 |
23 |
24 | Keras metrics in TF-Ranking.
25 |
26 | NOTE: For metrics that compute a ranking, ties are broken randomly. This means
27 | that metrics may be stochastic if items with equal scores are provided.
28 |
29 | WARNING: Some metrics (e.g. Recall or MRR) are not well-defined when there are
30 | no relevant items (e.g. if `y_true` has a row of only zeroes). For these cases,
31 | the TF-Ranking metrics will evaluate to `0`.
32 |
33 | ## Classes
34 |
35 | [`class ARPMetric`](../../tfr/keras/metrics/ARPMetric.md): Average relevance
36 | position (ARP).
37 |
38 | [`class AlphaDCGMetric`](../../tfr/keras/metrics/AlphaDCGMetric.md): Alpha
39 | discounted cumulative gain (alphaDCG).
40 |
41 | [`class DCGMetric`](../../tfr/keras/metrics/DCGMetric.md): Discounted cumulative
42 | gain (DCG).
43 |
44 | [`class HitsMetric`](../../tfr/keras/metrics/HitsMetric.md): Hits@k metric.
45 |
46 | [`class MRRMetric`](../../tfr/keras/metrics/MRRMetric.md): Mean reciprocal rank
47 | (MRR).
48 |
49 | [`class MeanAveragePrecisionMetric`](../../tfr/keras/metrics/MeanAveragePrecisionMetric.md):
50 | Mean average precision (MAP).
51 |
52 | [`class NDCGMetric`](../../tfr/keras/metrics/NDCGMetric.md): Normalized
53 | discounted cumulative gain (NDCG).
54 |
55 | [`class OPAMetric`](../../tfr/keras/metrics/OPAMetric.md): Ordered pair accuracy
56 | (OPA).
57 |
58 | [`class PrecisionIAMetric`](../../tfr/keras/metrics/PrecisionIAMetric.md):
59 | Precision-IA@k (Pre-IA@k).
60 |
61 | [`class PrecisionMetric`](../../tfr/keras/metrics/PrecisionMetric.md):
62 | Precision@k (P@k).
63 |
64 | [`class RankingMetricKey`](../../tfr/keras/metrics/RankingMetricKey.md): Ranking
65 | metric key strings.
66 |
67 | [`class RecallMetric`](../../tfr/keras/metrics/RecallMetric.md): Recall@k (R@k).
68 |
69 | ## Functions
70 |
71 | [`default_keras_metrics(...)`](../../tfr/keras/metrics/default_keras_metrics.md):
72 | Returns a list of ranking metrics.
73 |
74 | [`get(...)`](../../tfr/keras/metrics/get.md): Factory method to get a list of
75 | ranking metrics.
76 |
77 |
78 |
79 |
80 | Other Members |
81 |
82 |
83 |
84 | absolute_import
85 | |
86 |
87 | Instance of `__future__._Feature`
88 | |
89 |
90 |
91 | division
92 | |
93 |
94 | Instance of `__future__._Feature`
95 | |
96 |
97 |
98 | print_function
99 | |
100 |
101 | Instance of `__future__._Feature`
102 | |
103 |
104 |
105 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/metrics/RankingMetricKey.md:
--------------------------------------------------------------------------------
1 | description: Ranking metric key strings.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | # tfr.keras.metrics.RankingMetricKey
19 |
20 |
21 |
22 |
30 |
31 | Ranking metric key strings.
32 |
33 |
34 |
35 |
36 |
37 |
38 | Class Variables |
39 |
40 |
41 |
42 | ALPHA_DCG
43 | |
44 |
45 | `'alpha_dcg'`
46 | |
47 |
48 |
49 | ARP
50 | |
51 |
52 | `'arp'`
53 | |
54 |
55 |
56 | DCG
57 | |
58 |
59 | `'dcg'`
60 | |
61 |
62 |
63 | HITS
64 | |
65 |
66 | `'hits'`
67 | |
68 |
69 |
70 | MAP
71 | |
72 |
73 | `'map'`
74 | |
75 |
76 |
77 | MRR
78 | |
79 |
80 | `'mrr'`
81 | |
82 |
83 |
84 | NDCG
85 | |
86 |
87 | `'ndcg'`
88 | |
89 |
90 |
91 | ORDERED_PAIR_ACCURACY
92 | |
93 |
94 | `'ordered_pair_accuracy'`
95 | |
96 |
97 |
98 | PRECISION
99 | |
100 |
101 | `'precision'`
102 | |
103 |
104 |
105 | PRECISION_IA
106 | |
107 |
108 | `'precision_ia'`
109 | |
110 |
111 |
112 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/metrics/default_keras_metrics.md:
--------------------------------------------------------------------------------
1 | description: Returns a list of ranking metrics.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.metrics.default_keras_metrics
9 |
10 |
11 |
12 |
20 |
21 | Returns a list of ranking metrics.
22 |
23 |
24 | tfr.keras.metrics.default_keras_metrics(
25 | **kwargs
26 | ) -> List[tf.keras.metrics.Metric]
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `**kwargs`
39 | |
40 |
41 | Additional kwargs to pass to each keras metric.
42 | |
43 |
44 |
45 |
46 |
47 |
48 |
49 | Returns |
50 |
51 |
52 | A list of metrics of type `tf.keras.metrics.Metric`.
53 | |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/metrics/get.md:
--------------------------------------------------------------------------------
1 | description: Factory method to get a list of ranking metrics.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.metrics.get
9 |
10 |
11 |
12 |
20 |
21 | Factory method to get a list of ranking metrics.
22 |
23 |
24 | tfr.keras.metrics.get(
25 | key: str,
26 | name: Optional[str] = None,
27 | dtype: Optional[tf.dtypes.DType] = None,
28 | topn: Optional[int] = None,
29 | **kwargs
30 | ) -> tf.keras.metrics.Metric
31 |
32 |
33 |
34 |
35 | #### Example Usage:
36 |
37 | ```python
38 | metric = tfr.keras.metics.get(tfr.keras.metrics.RankingMetricKey.MRR)
39 | ```
40 |
41 | to get Mean Reciprocal Rank. `python metric =
42 | tfr.keras.metics.get(tfr.keras.metrics.RankingMetricKey.MRR, topn=2)` to get
43 | MRR@2.
44 |
45 |
46 |
47 |
48 | Args |
49 |
50 |
51 |
52 | `key`
53 | |
54 |
55 | An attribute of `RankingMetricKey`, defining which metric objects to
56 | return.
57 | |
58 |
59 |
60 | `name`
61 | |
62 |
63 | Name of metrics.
64 | |
65 |
66 |
67 | `dtype`
68 | |
69 |
70 | Dtype of the metrics.
71 | |
72 |
73 |
74 | `topn`
75 | |
76 |
77 | Cutoff of how many items are considered in the metric.
78 | |
79 |
80 |
81 | `**kwargs`
82 | |
83 |
84 | Keyword arguments for the metric object.
85 | |
86 |
87 |
88 |
89 |
90 |
91 |
92 | Returns |
93 |
94 |
95 | A tf.keras.metrics.Metric. See `_RankingMetric` signature for more details.
96 | |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 | Raises |
105 |
106 |
107 |
108 | `ValueError`
109 | |
110 |
111 | If key is unsupported.
112 | |
113 |
114 |
115 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model.md:
--------------------------------------------------------------------------------
1 | description: Ranking model utilities and classes in tfr.keras.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # Module: tfr.keras.model
9 |
10 |
11 |
12 |
20 |
21 | Ranking model utilities and classes in tfr.keras.
22 |
23 | ## Classes
24 |
25 | [`class AbstractModelBuilder`](../../tfr/keras/model/AbstractModelBuilder.md):
26 | Interface to build a `tf.keras.Model` for ranking.
27 |
28 | [`class DNNScorer`](../../tfr/keras/model/DNNScorer.md): Univariate scorer using
29 | DNN.
30 |
31 | [`class FeatureSpecInputCreator`](../../tfr/keras/model/FeatureSpecInputCreator.md):
32 | InputCreator with feature specs.
33 |
34 | [`class GAMScorer`](../../tfr/keras/model/GAMScorer.md): Univariate scorer using
35 | GAM.
36 |
37 | [`class InputCreator`](../../tfr/keras/model/InputCreator.md): Interface for
38 | input creator.
39 |
40 | [`class ModelBuilder`](../../tfr/keras/model/ModelBuilder.md): Builds a
41 | `tf.keras.Model`.
42 |
43 | [`class ModelBuilderWithMask`](../../tfr/keras/model/ModelBuilderWithMask.md):
44 | Interface to build a `tf.keras.Model` for ranking with a mask Tensor.
45 |
46 | [`class Preprocessor`](../../tfr/keras/model/Preprocessor.md): Interface for
47 | feature preprocessing.
48 |
49 | [`class PreprocessorWithSpec`](../../tfr/keras/model/PreprocessorWithSpec.md):
50 | Preprocessing inputs with provided spec.
51 |
52 | [`class Scorer`](../../tfr/keras/model/Scorer.md): Interface for scorer.
53 |
54 | [`class TypeSpecInputCreator`](../../tfr/keras/model/TypeSpecInputCreator.md):
55 | InputCreator with tensor type specs.
56 |
57 | [`class UnivariateScorer`](../../tfr/keras/model/UnivariateScorer.md): Interface
58 | for univariate scorer.
59 |
60 | ## Functions
61 |
62 | [`create_keras_model(...)`](../../tfr/keras/model/create_keras_model.md):
63 | Creates a Functional Keras ranking model.
64 |
65 | ## Type Aliases
66 |
67 | [`TensorDict`](../../tfr/keras/model/TensorDict.md)
68 |
69 | [`TensorLike`](../../tfr/keras/model/TensorLike.md)
70 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/AbstractModelBuilder.md:
--------------------------------------------------------------------------------
1 | description: Interface to build a tf.keras.Model for ranking.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | # tfr.keras.model.AbstractModelBuilder
10 |
11 |
12 |
13 |
21 |
22 | Interface to build a `tf.keras.Model` for ranking.
23 |
24 |
25 |
26 | The `AbstractModelBuilder` serves as the interface between model building and
27 | training. The training pipeline just calls the `build()` method to get the model
28 | constructed in the strategy scope used in the training pipeline, so for all
29 | variables in the model, optimizers, and metrics. See `ModelFitPipeline` in
30 | `pipeline.py` for example.
31 |
32 | The `build()` method is to be implemented in a subclass. The simplest example is
33 | just to define everything inside the build function when you define a
34 | tf.keras.Model.
35 |
36 | ```python
37 | class MyModelBuilder(AbstractModelBuilder):
38 |
39 | def build(self) -> tf.keras.Model:
40 | inputs = ...
41 | outputs = ...
42 | return tf.keras.Model(inputs=inputs, outputs=outputs)
43 | ```
44 |
45 | The `MyModelBuilder` should work with `ModelFitPipeline`. To make the model
46 | building more structured for ranking problems, we also define subclasses like
47 | `ModelBuilderWithMask` in the following.
48 |
49 | ## Methods
50 |
51 | build
52 |
53 | View
54 | source
55 |
56 |
57 | @abc.abstractmethod
58 | build() -> tf.keras.Model
59 |
60 |
61 | The build method to be implemented by a subclass.
62 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/DNNScorer.md:
--------------------------------------------------------------------------------
1 | description: Univariate scorer using DNN.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.keras.model.DNNScorer
11 |
12 |
13 |
14 |
22 |
23 | Univariate scorer using DNN.
24 |
25 | Inherits From:
26 | [`UnivariateScorer`](../../../tfr/keras/model/UnivariateScorer.md),
27 | [`Scorer`](../../../tfr/keras/model/Scorer.md)
28 |
29 |
30 | tfr.keras.model.DNNScorer(
31 | **dnn_kwargs
32 | )
33 |
34 |
35 |
36 |
37 | #### Example usage:
38 |
39 | ```python
40 | scorer=DNNScorer(hidden_layer_dims=[16])
41 | ```
42 |
43 |
44 |
45 |
46 | Args |
47 |
48 |
49 |
50 | `**dnn_kwargs`
51 | |
52 |
53 | A dict of keyward arguments for dense neural network layers.
54 | Please see tfr.keras.layers.create_tower for specific list of keyword
55 | arguments.
56 | |
57 |
58 |
59 |
60 | ## Methods
61 |
62 | __call__
63 |
64 | View
65 | source
66 |
67 |
68 | __call__(
69 | context_features: tfr.keras.model.TensorDict
,
70 | example_features: tfr.keras.model.TensorDict
,
71 | mask: tf.Tensor
72 | ) -> Union[tf.Tensor, tfr.keras.model.TensorDict
]
73 |
74 |
75 | See `Scorer`.
76 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/FeatureSpecInputCreator.md:
--------------------------------------------------------------------------------
1 | description: InputCreator with feature specs.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.keras.model.FeatureSpecInputCreator
11 |
12 |
13 |
14 |
22 |
23 | InputCreator with feature specs.
24 |
25 | Inherits From: [`InputCreator`](../../../tfr/keras/model/InputCreator.md)
26 |
27 |
28 | tfr.keras.model.FeatureSpecInputCreator(
29 | context_feature_spec: Dict[str, Union[tf.io.FixedLenFeature, tf.io.VarLenFeature, tf.io.
30 | RaggedFeature]],
31 | example_feature_spec: Dict[str, Union[tf.io.FixedLenFeature, tf.io.VarLenFeature, tf.io.
32 | RaggedFeature]]
33 | )
34 |
35 |
36 |
37 |
38 | #### Example usage:
39 |
40 | ```python
41 | input_creator=FeatureSpecInputCreator(
42 | {},
43 | {"example_feature_1": tf.io.FixedLenFeature(
44 | shape=(1,), dtype=tf.float32, default_value=0.0)})
45 | ```
46 |
47 |
48 |
49 |
50 | Args |
51 |
52 |
53 |
54 | `context_feature_spec`
55 | |
56 |
57 | A dict maps the context feature keys to the
58 | corresponding context feature specs.
59 | |
60 |
61 |
62 | `example_feature_spec`
63 | |
64 |
65 | A dict maps the example feature keys to the
66 | corresponding example feature specs.
67 | |
68 |
69 |
70 |
71 | ## Methods
72 |
73 | __call__
74 |
75 | View
76 | source
77 |
78 |
79 | __call__() -> Tuple[tfr.keras.model.TensorDict
, tfr.keras.model.TensorDict
]
80 |
81 |
82 | See `InputCreator`.
83 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/GAMScorer.md:
--------------------------------------------------------------------------------
1 | description: Univariate scorer using GAM.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.keras.model.GAMScorer
11 |
12 |
13 |
14 |
22 |
23 | Univariate scorer using GAM.
24 |
25 | Inherits From:
26 | [`UnivariateScorer`](../../../tfr/keras/model/UnivariateScorer.md),
27 | [`Scorer`](../../../tfr/keras/model/Scorer.md)
28 |
29 |
30 | tfr.keras.model.GAMScorer(
31 | **gam_kwargs
32 | )
33 |
34 |
35 |
36 |
37 | The scorer implements Neural Generalized Additive Ranking Model, which is an
38 | additive ranking model. See the [paper](https://arxiv.org/abs/2005.02553) for
39 | more details.
40 |
41 | #### Example usage:
42 |
43 | ```python
44 | scorer=GAMScorer(hidden_layer_dims=[16])
45 | ```
46 |
47 |
48 |
49 |
50 | Args |
51 |
52 |
53 |
54 | `**gam_kwargs`
55 | |
56 |
57 | A dict of keyward arguments for GAM layers. Please see
58 | `tfr.keras.layers.GAMlayer` for specific list of keyword arguments.
59 | |
60 |
61 |
62 |
63 | ## Methods
64 |
65 | __call__
66 |
67 | View
68 | source
69 |
70 |
71 | __call__(
72 | context_features: tfr.keras.model.TensorDict
,
73 | example_features: tfr.keras.model.TensorDict
,
74 | mask: tf.Tensor
75 | ) -> Union[tf.Tensor, tfr.keras.model.TensorDict
]
76 |
77 |
78 | See `Scorer`.
79 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/InputCreator.md:
--------------------------------------------------------------------------------
1 | description: Interface for input creator.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | # tfr.keras.model.InputCreator
10 |
11 |
12 |
13 |
21 |
22 | Interface for input creator.
23 |
24 |
25 |
26 | The `InputCreator` class is an abstract class to implement `create_inputs` in
27 | `ModelBuilder` in tfr.keras.
28 |
29 | To be implemented by subclasses:
30 |
31 | * `__call__()`: Contains the logic to create `tf.keras.Input` for context and
32 | example inputs.
33 |
34 | Example subclass implementation:
35 |
36 | ```python
37 | class SimpleInputCreator(InputCreator):
38 |
39 | def __call__(self):
40 | return {}, {"example_feature_1": tf.keras.Input((1,), dtype=tf.float32)}
41 | ```
42 |
43 | ## Methods
44 |
45 | __call__
46 |
47 | View
48 | source
49 |
50 |
51 | @abc.abstractmethod
52 | __call__() -> Tuple[tfr.keras.model.TensorDict
, tfr.keras.model.TensorDict
]
53 |
54 |
55 | Invokes the `InputCreator` instance.
56 |
57 |
58 |
59 |
60 | Returns |
61 |
62 |
63 | A tuple of two dicts which map the context and example feature keys to
64 | the corresponding `tf.keras.Input`.
65 | |
66 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/Preprocessor.md:
--------------------------------------------------------------------------------
1 | description: Interface for feature preprocessing.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | # tfr.keras.model.Preprocessor
10 |
11 |
12 |
13 |
21 |
22 | Interface for feature preprocessing.
23 |
24 |
25 |
26 | The `Preprocessor` class is an abstract class to implement `preprocess` in
27 | `ModelBuilder` in tfr.keras.
28 |
29 | To be implemented by subclasses:
30 |
31 | * `__call__()`: Contains the logic to preprocess context and example inputs.
32 |
33 | Example subclass implementation:
34 |
35 | ```python
36 | class SimplePreprocessor(Preprocessor):
37 |
38 | def __call__(self, context_inputs, example_inputs, mask):
39 | context_features = {
40 | name: tf.math.log1p(
41 | tf.abs(tensor)) for name, tensor in context_inputs.items()
42 | }
43 | example_features = {
44 | name: tf.math.log1p(
45 | tf.abs(tensor)) for name, tensor in example_inputs.items()
46 | }
47 | return context_features, example_features
48 | ```
49 |
50 | ## Methods
51 |
52 | __call__
53 |
54 | View
55 | source
56 |
57 |
58 | @abc.abstractmethod
59 | __call__(
60 | context_inputs: tfr.keras.model.TensorDict
,
61 | example_inputs: tfr.keras.model.TensorDict
,
62 | mask: tf.Tensor
63 | ) -> Tuple[tfr.keras.model.TensorDict
, tfr.keras.model.TensorDict
]
64 |
65 |
66 | Invokes the `Preprocessor` instance.
67 |
68 |
69 |
70 |
71 | Args |
72 |
73 |
74 |
75 | `context_inputs`
76 | |
77 |
78 | maps context feature keys to `tf.keras.Input`.
79 | |
80 |
81 |
82 | `example_inputs`
83 | |
84 |
85 | maps example feature keys to `tf.keras.Input`.
86 | |
87 |
88 |
89 | `mask`
90 | |
91 |
92 | [batch_size, list_size]-tensor of mask for valid examples.
93 | |
94 |
95 |
96 |
97 |
98 |
99 |
100 | Returns |
101 |
102 |
103 | A tuple of two dicts which map the context and example feature keys to
104 | the corresponding `tf.Tensor`s.
105 | |
106 |
107 |
108 |
109 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/PreprocessorWithSpec.md:
--------------------------------------------------------------------------------
1 | description: Preprocessing inputs with provided spec.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.keras.model.PreprocessorWithSpec
11 |
12 |
13 |
14 |
22 |
23 | Preprocessing inputs with provided spec.
24 |
25 | Inherits From: [`Preprocessor`](../../../tfr/keras/model/Preprocessor.md)
26 |
27 |
28 | tfr.keras.model.PreprocessorWithSpec(
29 | preprocess_spec: Optional[Dict[str, Callable[[Any], Any]]] = None,
30 | default_value_spec: Optional[Dict[str, float]] = None
31 | )
32 |
33 |
34 |
35 |
36 | Transformation including KPL or customized transformation like log1p can be
37 | defined and passed in `preprocess_spec` with the following example usage:
38 |
39 | ```python
40 | preprocess_spec = {
41 | **{name: lambda t: tf.math.log1p(t * tf.sign(t)) * tf.sign(t)
42 | for name in example_feature_spec.keys()},
43 | **{name: tf.reduce_mean(
44 | tf.keras.layers.Embedding(input_dim=10, output_dim=4)(x), axis=-2)
45 | for name in context_feature_spec.keys()}
46 | }
47 | preprocessor = PreprocessorWithSpec(preprocess_spec)
48 | ```
49 |
50 |
51 |
52 |
53 | Args |
54 |
55 |
56 |
57 | `preprocess_spec`
58 | |
59 |
60 | maps a feature key to a callable to preprocess a feature.
61 | Only include those features that need preprocessing.
62 | |
63 |
64 |
65 | `default_value_spec`
66 | |
67 |
68 | maps a feature key to a default value to convert a
69 | RaggedTensor to Tensor. Default to 0. if not specified.
70 | |
71 |
72 |
73 |
74 | ## Methods
75 |
76 | __call__
77 |
78 | View
79 | source
80 |
81 |
82 | __call__(
83 | context_inputs: tfr.keras.model.TensorDict
,
84 | example_inputs: tfr.keras.model.TensorDict
,
85 | mask: tf.Tensor
86 | ) -> Tuple[tfr.keras.model.TensorDict
, tfr.keras.model.TensorDict
]
87 |
88 |
89 | See `Preprocessor`.
90 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/Scorer.md:
--------------------------------------------------------------------------------
1 | description: Interface for scorer.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | # tfr.keras.model.Scorer
10 |
11 |
12 |
13 |
21 |
22 | Interface for scorer.
23 |
24 |
25 |
26 | The `Scorer` class is an abstract class to implement `score` in `ModelBuilder`
27 | in tfr.keras.
28 |
29 | To be implemented by subclasses:
30 |
31 | * `__call__()`: Contains the logic to score based on the context and example
32 | features.
33 |
34 | Example subclass implementation:
35 |
36 | ```python
37 | class SimpleScorer(Scorer):
38 |
39 | def __call__(self, context_features, example_features, mask):
40 | x = tf.concat([tensor for tensor in example_features.values()], -1)
41 | return tf.keras.layers.Dense(1)(x)
42 | ```
43 |
44 | ## Methods
45 |
46 | __call__
47 |
48 | View
49 | source
50 |
51 |
52 | @abc.abstractmethod
53 | __call__(
54 | context_features: tfr.keras.model.TensorDict
,
55 | example_features: tfr.keras.model.TensorDict
,
56 | mask: tf.Tensor
57 | ) -> Union[TensorLike, TensorDict]
58 |
59 |
60 | Scores all examples given context and returns logits.
61 |
62 |
63 |
64 |
65 | Args |
66 |
67 |
68 |
69 | `context_features`
70 | |
71 |
72 | maps from context feature keys to [batch_size,
73 | feature_dims]-tensors of preprocessed context features.
74 | |
75 |
76 |
77 | `example_features`
78 | |
79 |
80 | maps from example feature keys to [batch_size,
81 | list_size, feature_dims]-tensors of preprocessed example features.
82 | |
83 |
84 |
85 | `mask`
86 | |
87 |
88 | [batch_size, list_size]-tensor of mask for valid examples.
89 | |
90 |
91 |
92 |
93 |
94 |
95 |
96 | Returns |
97 |
98 |
99 | A [batch_size, list_size]-tensor of logits or a dict mapping task name to
100 | logits in the multi-task setting.
101 | |
102 |
103 |
104 |
105 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/TensorDict.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | # tfr.keras.model.TensorDict
7 |
8 |
9 | This symbol is a **type alias**.
10 |
11 | #### Source:
12 |
13 |
14 | TensorDict = dict[
15 | str,
16 | tfr.keras.model.TensorLike
17 | ]
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/TensorLike.md:
--------------------------------------------------------------------------------
1 | description: Union of all types that can be converted to a tf.Tensor by
2 | tf.convert_to_tensor.
3 |
4 |
5 |
6 |
7 |
8 |
9 | # tfr.keras.model.TensorLike
10 |
11 |
12 | This symbol is a **type alias**.
13 |
14 | Union of all types that can be converted to a `tf.Tensor` by
15 | `tf.convert_to_tensor`.
16 |
17 | #### Source:
18 |
19 |
20 | TensorLike = Union[
21 | tensorflow.python.types.core.Tensor,
22 | tensorflow.python.types.core.TensorProtocol,
23 | int,
24 | float,
25 | bool,
26 | str,
27 | bytes,
28 | complex,
29 | tuple,
30 | list,
31 | numpy.ndarray,
32 | numpy.generic
33 | ]
34 |
35 |
36 |
37 |
38 | This definition may be used in user code. Additional types may be added in the
39 | future as more input types are supported.
40 |
41 | #### Example:
42 |
43 | ```
44 | def foo(x: TensorLike):
45 | pass
46 | ```
47 |
48 | This definition passes static type verification for:
49 |
50 | ```
51 | foo(tf.constant([1, 2, 3]))
52 | foo([1, 2, 3])
53 | foo(np.array([1, 2, 3]))
54 | ```
55 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/TypeSpecInputCreator.md:
--------------------------------------------------------------------------------
1 | description: InputCreator with tensor type specs.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.keras.model.TypeSpecInputCreator
11 |
12 |
13 |
14 |
22 |
23 | InputCreator with tensor type specs.
24 |
25 | Inherits From: [`InputCreator`](../../../tfr/keras/model/InputCreator.md)
26 |
27 |
28 | tfr.keras.model.TypeSpecInputCreator(
29 | type_spec: Dict[str, Union[tf.TensorSpec, tf.RaggedTensorSpec]],
30 | context_feature_names: Optional[List[str]] = None,
31 | example_feature_names: Optional[List[str]] = None
32 | )
33 |
34 |
35 |
36 |
37 | #### Example usage:
38 |
39 | ```python
40 | input_creator=TypeSpecInputCreator(
41 | {"example_feature_1": tf.TensorSpec(shape=[None, 1], dtype=tf.float32)},
42 | example_feature_names=["example_feature_1"])
43 | ```
44 |
45 |
46 |
47 |
48 | Args |
49 |
50 |
51 |
52 | `type_spec`
53 | |
54 |
55 | A dict maps the context and example feature keys to the
56 | corresponding context and example type specs.
57 | |
58 |
59 |
60 | `context_feature_names`
61 | |
62 |
63 | A list of context feature keys.
64 | |
65 |
66 |
67 | `example_feature_names`
68 | |
69 |
70 | A list of example feature keys.
71 | |
72 |
73 |
74 |
75 | ## Methods
76 |
77 | __call__
78 |
79 | View
80 | source
81 |
82 |
83 | __call__() -> Tuple[tfr.keras.model.TensorDict
, tfr.keras.model.TensorDict
]
84 |
85 |
86 | See `InputCreator`.
87 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/UnivariateScorer.md:
--------------------------------------------------------------------------------
1 | description: Interface for univariate scorer.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | # tfr.keras.model.UnivariateScorer
10 |
11 |
12 |
13 |
21 |
22 | Interface for univariate scorer.
23 |
24 | Inherits From: [`Scorer`](../../../tfr/keras/model/Scorer.md)
25 |
26 |
27 |
28 | The `UnivariateScorer` class is an abstract class to implement `score` in
29 | `ModelBuilder` in tfr.keras with a univariate scoring function.
30 |
31 | To be implemented by subclasses:
32 |
33 | * `_score_flattened()`: Contains the logic to do the univariate scoring on
34 | flattened context and example features.
35 |
36 | Example subclass implementation:
37 |
38 | ```python
39 | class SimpleUnivariateScorer(UnivariateScorer):
40 |
41 | def _score_flattened(self, context_features, example_features):
42 | x = tf.concat([tensor for tensor in example_features.values()], -1)
43 | return tf.keras.layers.Dense(1)(x)
44 | ```
45 |
46 | ## Methods
47 |
48 | __call__
49 |
50 | View
51 | source
52 |
53 |
54 | __call__(
55 | context_features: tfr.keras.model.TensorDict
,
56 | example_features: tfr.keras.model.TensorDict
,
57 | mask: tf.Tensor
58 | ) -> Union[tf.Tensor, tfr.keras.model.TensorDict
]
59 |
60 |
61 | See `Scorer`.
62 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/model/create_keras_model.md:
--------------------------------------------------------------------------------
1 | description: Creates a Functional Keras ranking model.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.model.create_keras_model
9 |
10 |
11 |
12 |
20 |
21 | Creates a Functional Keras ranking model.
22 |
23 |
24 | tfr.keras.model.create_keras_model(
25 | network, loss, metrics, optimizer, size_feature_name, list_size=None
26 | )
27 |
28 |
29 |
30 |
31 | A mask is inferred from size_feature_name and passed to the network, along with
32 | feature dictionary as inputs.
33 |
34 |
35 |
36 |
37 | Args |
38 |
39 |
40 |
41 | `network`
42 | |
43 |
44 | (`tfr.keras.network.RankingNetwork`) A ranking network which
45 | generates a list of scores.
46 | |
47 |
48 |
49 | `loss`
50 | |
51 |
52 | (`tfr.keras.losses._RankingLoss`) A ranking loss.
53 | |
54 |
55 |
56 | `metrics`
57 | |
58 |
59 | (list) List of ranking metrics, `tfr.keras.metrics._RankingMetric`
60 | instances.
61 | |
62 |
63 |
64 | `optimizer`
65 | |
66 |
67 | (`tf.keras.optimizer.Optimizer`) Optimizer to minimize ranking
68 | loss.
69 | |
70 |
71 |
72 | `size_feature_name`
73 | |
74 |
75 | (str) Name of feature for example list sizes. If not
76 | None, this feature name corresponds to a `tf.int32` Tensor of size
77 | [batch_size] corresponding to sizes of example lists. If `None`, all
78 | examples are treated as valid.
79 | |
80 |
81 |
82 | `list_size`
83 | |
84 |
85 | (int) The list size for example features. If None, use dynamic
86 | list size. A fixed list size is required for TPU training.
87 | |
88 |
89 |
90 |
91 |
92 |
93 |
94 | Returns |
95 |
96 |
97 | A compiled ranking Keras model, a `tf.keras.Model` instance.
98 | |
99 |
100 |
101 |
102 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/pipeline.md:
--------------------------------------------------------------------------------
1 | description: Ranking pipeline to train tf.keras.Model in tfr.keras.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # Module: tfr.keras.pipeline
9 |
10 |
11 |
12 |
20 |
21 | Ranking pipeline to train tf.keras.Model in tfr.keras.
22 |
23 | ## Classes
24 |
25 | [`class AbstractDatasetBuilder`](../../tfr/keras/pipeline/AbstractDatasetBuilder.md):
26 | Interface for datasets and signatures.
27 |
28 | [`class AbstractPipeline`](../../tfr/keras/pipeline/AbstractPipeline.md):
29 | Interface for ranking pipeline to train a `tf.keras.Model`.
30 |
31 | [`class BaseDatasetBuilder`](../../tfr/keras/pipeline/BaseDatasetBuilder.md):
32 | Builds datasets from feature specs.
33 |
34 | [`class DatasetHparams`](../../tfr/keras/pipeline/DatasetHparams.md):
35 | Hyperparameters used in `BaseDatasetBuilder`.
36 |
37 | [`class ModelFitPipeline`](../../tfr/keras/pipeline/ModelFitPipeline.md):
38 | Pipeline using `model.fit` to train a ranking `tf.keras.Model`.
39 |
40 | [`class MultiLabelDatasetBuilder`](../../tfr/keras/pipeline/MultiLabelDatasetBuilder.md):
41 | Builds datasets for multi-task training.
42 |
43 | [`class MultiTaskPipeline`](../../tfr/keras/pipeline/MultiTaskPipeline.md):
44 | Pipeline for multi-task training.
45 |
46 | [`class NullDatasetBuilder`](../../tfr/keras/pipeline/NullDatasetBuilder.md): A
47 | no-op wrapper of datasets and signatures.
48 |
49 | [`class PipelineHparams`](../../tfr/keras/pipeline/PipelineHparams.md):
50 | Hyperparameters used in `ModelFitPipeline`.
51 |
52 | [`class SimpleDatasetBuilder`](../../tfr/keras/pipeline/SimpleDatasetBuilder.md):
53 | Builds datasets from feature specs with a single label spec.
54 |
55 | [`class SimplePipeline`](../../tfr/keras/pipeline/SimplePipeline.md): Pipleine
56 | for single-task training.
57 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/saved_model.md:
--------------------------------------------------------------------------------
1 | description: SavedModel utilities for TF-Ranking.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # Module: tfr.keras.saved_model
9 |
10 |
11 |
12 |
20 |
21 | SavedModel utilities for TF-Ranking.
22 |
23 | ## Classes
24 |
25 | [`class Signatures`](../../tfr/keras/saved_model/Signatures.md): Defines
26 | signatures to support regress and predict serving.
27 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/strategy_utils.md:
--------------------------------------------------------------------------------
1 | description: tf.distribute strategy utils for Ranking pipeline in tfr.keras.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | # Module: tfr.keras.strategy_utils
13 |
14 |
15 |
16 |
24 |
25 | tf.distribute strategy utils for Ranking pipeline in tfr.keras.
26 |
27 | In TF2, the distributed training can be easily handled with Strategy offered in
28 | tf.distribute. Depending on device and MapReduce technique, there are four
29 | strategies are currently supported. They are: MirroredStrategy: synchronous
30 | strategy on a single CPU/GPU worker. MultiWorkerMirroredStrategy: synchronous
31 | strategy on multiple CPU/GPU workers. TPUStrategy: distributed strategy working
32 | on TPU. ParameterServerStrategy: asynchronous distributed strategy on CPU/GPU
33 | workers.
34 |
35 | Note: ParameterServerStrategy is not fully compatible with `model.fit` in
36 | current version of tensorflow, thus not supported.
37 |
38 | Please check https://www.tensorflow.org/guide/distributed_training for more
39 | information.
40 |
41 | ## Classes
42 |
43 | [`class NullContextManager`](../../tfr/keras/strategy_utils/NullContextManager.md):
44 | A null context manager for local training.
45 |
46 | ## Functions
47 |
48 | [`get_output_filepath(...)`](../../tfr/keras/strategy_utils/get_output_filepath.md):
49 | Gets filepaths for different workers to resolve conflict of MWMS.
50 |
51 | [`get_strategy(...)`](../../tfr/keras/strategy_utils/get_strategy.md): Creates
52 | and initializes the requested tf.distribute strategy.
53 |
54 | [`strategy_scope(...)`](../../tfr/keras/strategy_utils/strategy_scope.md): Gets
55 | the strategy.scope() for training with strategy.
56 |
57 |
58 |
59 |
60 | Other Members |
61 |
62 |
63 |
64 | MIRRORED_STRATEGY
65 | |
66 |
67 | `'MirroredStrategy'`
68 | |
69 |
70 |
71 | MWMS_STRATEGY
72 | |
73 |
74 | `'MultiWorkerMirroredStrategy'`
75 | |
76 |
77 |
78 | PS_STRATEGY
79 | |
80 |
81 | `'ParameterServerStrategy'`
82 | |
83 |
84 |
85 | TPU_STRATEGY
86 | |
87 |
88 | `'TPUStrategy'`
89 | |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/strategy_utils/NullContextManager.md:
--------------------------------------------------------------------------------
1 | description: A null context manager for local training.
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | # tfr.keras.strategy_utils.NullContextManager
11 |
12 |
13 |
14 |
22 |
23 | A null context manager for local training.
24 |
25 |
26 |
27 | #### Example usage:
28 |
29 | ```python
30 | with NullContextManager():
31 | model = ...
32 | ```
33 |
34 | ## Methods
35 |
36 | __enter__
37 |
38 | View
39 | source
40 |
41 |
42 | __enter__()
43 |
44 |
45 | __exit__
46 |
47 | View
48 | source
49 |
50 |
51 | __exit__(
52 | *args
53 | )
54 |
55 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/strategy_utils/get_output_filepath.md:
--------------------------------------------------------------------------------
1 | description: Gets filepaths for different workers to resolve conflict of MWMS.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.strategy_utils.get_output_filepath
9 |
10 |
11 |
12 |
20 |
21 | Gets filepaths for different workers to resolve conflict of MWMS.
22 |
23 |
24 | tfr.keras.strategy_utils.get_output_filepath(
25 | filepath: str, strategy: Optional[tf.distribute.Strategy]
26 | ) -> str
27 |
28 |
29 |
30 |
31 | #### Example usage:
32 |
33 | ```python
34 | strategy = get_strategy("MultiWorkerMirroredStrategy")
35 | worker_filepath = get_output_filepath("model/", strategy)
36 | ```
37 |
38 |
39 |
40 |
41 | Args |
42 |
43 |
44 |
45 | `filepath`
46 | |
47 |
48 | Path to output model files.
49 | |
50 |
51 |
52 | `strategy`
53 | |
54 |
55 | Distributed training strategy is used.
56 | |
57 |
58 |
59 |
60 |
61 |
62 |
63 | Returns |
64 |
65 |
66 | Output path that is compatible with strategy and the specific worker.
67 | |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/strategy_utils/strategy_scope.md:
--------------------------------------------------------------------------------
1 | description: Gets the strategy.scope() for training with strategy.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.strategy_utils.strategy_scope
9 |
10 |
11 |
12 |
20 |
21 | Gets the strategy.scope() for training with strategy.
22 |
23 |
24 | tfr.keras.strategy_utils.strategy_scope(
25 | strategy: Optional[tf.distribute.Strategy]
26 | ) -> Any
27 |
28 |
29 |
30 |
31 | #### Example usage:
32 |
33 | ```python
34 | with strategy_scope(strategy):
35 | model = ...
36 | ```
37 |
38 |
39 |
40 |
41 | Args |
42 |
43 |
44 |
45 | `strategy`
46 | |
47 |
48 | Distributed training strategy is used.
49 | |
50 |
51 |
52 |
53 |
54 |
55 |
56 | Returns |
57 |
58 |
59 | ContextManager for the distributed training strategy.
60 | |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils.md:
--------------------------------------------------------------------------------
1 | description: Utils for tfr.keras.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # Module: tfr.keras.utils
9 |
10 |
11 |
12 |
20 |
21 | Utils for tfr.keras.
22 |
23 | ## Functions
24 |
25 | [`deserialize_keras_object(...)`](../../tfr/keras/utils/deserialize_keras_object.md)
26 |
27 | [`identity(...)`](../../tfr/keras/utils/identity.md): Identity function that
28 | returns the input label.
29 |
30 | [`inverse(...)`](../../tfr/keras/utils/inverse.md): Computes the inverse of
31 | input rank.
32 |
33 | [`is_greater_equal_1(...)`](../../tfr/keras/utils/is_greater_equal_1.md):
34 | Computes whether label is greater or equal to 1.
35 |
36 | [`log2_inverse(...)`](../../tfr/keras/utils/log2_inverse.md): Computes
37 | `1./log2(1+x)` element-wise for each label.
38 |
39 | [`pow_minus_1(...)`](../../tfr/keras/utils/pow_minus_1.md): Computes `2**x - 1`
40 | element-wise for each label.
41 |
42 | [`serialize_keras_object(...)`](../../tfr/keras/utils/serialize_keras_object.md)
43 |
44 | [`symmetric_log1p(...)`](../../tfr/keras/utils/symmetric_log1p.md): Computes
45 | `sign(x) * log(1 + sign(x))`.
46 |
47 | ## Type Aliases
48 |
49 | [`GainFunction`](../../tfr/keras/utils/GainFunction.md)
50 |
51 | [`PositiveFunction`](../../tfr/keras/utils/GainFunction.md)
52 |
53 | [`RankDiscountFunction`](../../tfr/keras/utils/GainFunction.md)
54 |
55 | [`TensorLike`](../../tfr/keras/model/TensorLike.md)
56 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/GainFunction.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | # tfr.keras.utils.GainFunction
7 |
8 |
9 | This symbol is a **type alias**.
10 |
11 | #### Source:
12 |
13 |
14 | GainFunction = Callable[
15 | tfr.keras.model.TensorLike
,
16 | tensorflow.python.framework.ops.Tensor
17 | ]
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/deserialize_keras_object.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | # tfr.keras.utils.deserialize_keras_object
7 |
8 |
9 |
10 |
18 |
19 |
20 | tfr.keras.utils.deserialize_keras_object(
21 | config,
22 | module_objects=None,
23 | custom_objects=None,
24 | printable_module_name=None
25 | )
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/identity.md:
--------------------------------------------------------------------------------
1 | description: Identity function that returns the input label.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.utils.identity
9 |
10 |
11 |
12 |
20 |
21 | Identity function that returns the input label.
22 |
23 |
24 | tfr.keras.utils.identity(
25 | label: tfr.keras.model.TensorLike
26 | ) -> tf.Tensor
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `label`
39 | |
40 |
41 | A `Tensor` or anything that can be converted to a tensor using
42 | `tf.convert_to_tensor`.
43 | |
44 |
45 |
46 |
47 |
48 |
49 |
50 | Returns |
51 |
52 |
53 | The input label.
54 | |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/inverse.md:
--------------------------------------------------------------------------------
1 | description: Computes the inverse of input rank.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.utils.inverse
9 |
10 |
11 |
12 |
20 |
21 | Computes the inverse of input rank.
22 |
23 |
24 | tfr.keras.utils.inverse(
25 | rank: tfr.keras.model.TensorLike
26 | ) -> tf.Tensor
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `rank`
39 | |
40 |
41 | A `Tensor` or anything that can be converted to a tensor using
42 | `tf.convert_to_tensor`.
43 | |
44 |
45 |
46 |
47 |
48 |
49 |
50 | Returns |
51 |
52 |
53 | A `Tensor` that has each input element transformed as `x` to `1/x`.
54 | |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/is_greater_equal_1.md:
--------------------------------------------------------------------------------
1 | description: Computes whether label is greater or equal to 1.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.utils.is_greater_equal_1
9 |
10 |
11 |
12 |
20 |
21 | Computes whether label is greater or equal to 1.
22 |
23 |
24 | tfr.keras.utils.is_greater_equal_1(
25 | label: tfr.keras.model.TensorLike
26 | ) -> tf.Tensor
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `label`
39 | |
40 |
41 | A `Tensor` or anything that can be converted to a tensor using
42 | `tf.convert_to_tensor`.
43 | |
44 |
45 |
46 |
47 |
48 |
49 |
50 | Returns |
51 |
52 |
53 | A `Tensor` that has each input element transformed as `x` to `I(x > 1)`.
54 | |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/log2_inverse.md:
--------------------------------------------------------------------------------
1 | description: Computes 1./log2(1+x) element-wise for each label.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.utils.log2_inverse
9 |
10 |
11 |
12 |
20 |
21 | Computes `1./log2(1+x)` element-wise for each label.
22 |
23 |
24 | tfr.keras.utils.log2_inverse(
25 | rank: tfr.keras.model.TensorLike
26 | ) -> tf.Tensor
27 |
28 |
29 |
30 |
31 | Can be used to define `rank_discount_fn` for
32 | tfr.keras.metrics.NDCGMetric
.
33 |
34 |
35 |
36 |
37 | Args |
38 |
39 |
40 |
41 | `rank`
42 | |
43 |
44 | A `Tensor` or anything that can be converted to a tensor using
45 | `tf.convert_to_tensor`.
46 | |
47 |
48 |
49 |
50 |
51 |
52 |
53 | Returns |
54 |
55 |
56 | A `Tensor` that has each input element transformed as `x` to `1./log2(1+x)`.
57 | |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/pow_minus_1.md:
--------------------------------------------------------------------------------
1 | description: Computes 2**x - 1 element-wise for each label.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.utils.pow_minus_1
9 |
10 |
11 |
12 |
20 |
21 | Computes `2**x - 1` element-wise for each label.
22 |
23 |
24 | tfr.keras.utils.pow_minus_1(
25 | label: tfr.keras.model.TensorLike
26 | ) -> tf.Tensor
27 |
28 |
29 |
30 |
31 | Can be used to define `gain_fn` for
32 | tfr.keras.metrics.NDCGMetric
.
33 |
34 |
35 |
36 |
37 | Args |
38 |
39 |
40 |
41 | `label`
42 | |
43 |
44 | A `Tensor` or anything that can be converted to a tensor using
45 | `tf.convert_to_tensor`.
46 | |
47 |
48 |
49 |
50 |
51 |
52 |
53 | Returns |
54 |
55 |
56 | A `Tensor` that has each input element transformed as `x` to `2**x - 1`.
57 | |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/serialize_keras_object.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | # tfr.keras.utils.serialize_keras_object
7 |
8 |
9 |
10 |
18 |
19 |
20 | tfr.keras.utils.serialize_keras_object(
21 | obj
22 | )
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/keras/utils/symmetric_log1p.md:
--------------------------------------------------------------------------------
1 | description: Computes sign(x) * log(1 + sign(x)).
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.keras.utils.symmetric_log1p
9 |
10 |
11 |
12 |
20 |
21 | Computes `sign(x) * log(1 + sign(x))`.
22 |
23 |
24 | tfr.keras.utils.symmetric_log1p(
25 | t: tfr.keras.model.TensorLike
26 | ) -> tf.Tensor
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `t`
39 | |
40 |
41 | A `Tensor` or anything that can be converted to a tensor using
42 | `tf.convert_to_tensor`.
43 | |
44 |
45 |
46 |
47 |
48 |
49 |
50 | Returns |
51 |
52 |
53 | A `Tensor` that has each input element transformed as `x` to `I(x > 1)`.
54 | |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils.md:
--------------------------------------------------------------------------------
1 | description: Utility functions for ranking library.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # Module: tfr.utils
9 |
10 |
11 |
12 |
20 |
21 | Utility functions for ranking library.
22 |
23 | ## Functions
24 |
25 | [`de_noise(...)`](../tfr/utils/de_noise.md): Returns a float `Tensor` as the
26 | de-noised `counts`.
27 |
28 | [`gather_per_row(...)`](../tfr/utils/gather_per_row.md): Gathers the values from
29 | input tensor based on per-row indices.
30 |
31 | [`is_label_valid(...)`](../tfr/utils/is_label_valid.md): Returns a boolean
32 | `Tensor` for label validity.
33 |
34 | [`organize_valid_indices(...)`](../tfr/utils/organize_valid_indices.md):
35 | Organizes indices in such a way that valid items appear first.
36 |
37 | [`padded_nd_indices(...)`](../tfr/utils/padded_nd_indices.md): Pads the invalid
38 | entries by valid ones and returns the nd_indices.
39 |
40 | [`parse_keys_and_weights(...)`](../tfr/utils/parse_keys_and_weights.md): Parses
41 | the encoded key to keys and weights.
42 |
43 | [`ragged_to_dense(...)`](../tfr/utils/ragged_to_dense.md): Converts given inputs
44 | from ragged tensors to dense tensors.
45 |
46 | [`reshape_first_ndims(...)`](../tfr/utils/reshape_first_ndims.md): Reshapes the
47 | first n dims of the input `tensor` to `new shape`.
48 |
49 | [`reshape_to_2d(...)`](../tfr/utils/reshape_to_2d.md): Converts the given
50 | `tensor` to a 2-D `Tensor`.
51 |
52 | [`shuffle_valid_indices(...)`](../tfr/utils/shuffle_valid_indices.md): Returns a
53 | shuffle of indices with valid ones on top.
54 |
55 | [`sort_by_scores(...)`](../tfr/utils/sort_by_scores.md): Sorts list of features
56 | according to per-example scores.
57 |
58 | [`sorted_ranks(...)`](../tfr/utils/sorted_ranks.md): Returns an int `Tensor` as
59 | the ranks (1-based) after sorting scores.
60 |
61 | ## Type Aliases
62 |
63 | [`LossFunction`](../tfr/utils/LossFunction.md)
64 |
65 | [`MetricFunction`](../tfr/utils/LossFunction.md)
66 |
67 | [`TensorLike`](../tfr/keras/model/TensorLike.md)
68 |
69 | [`TransformationFunction`](../tfr/keras/utils/GainFunction.md)
70 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/LossFunction.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | # tfr.utils.LossFunction
7 |
8 |
9 | This symbol is a **type alias**.
10 |
11 | #### Source:
12 |
13 |
14 | LossFunction = Callable[
15 | tfr.keras.model.TensorLike
,
16 | tfr.keras.model.TensorLike
,
17 | dict[str, tfr.keras.model.TensorLike
], tensorflow.python.framework.ops.Tensor
18 | ]
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/de_noise.md:
--------------------------------------------------------------------------------
1 | description: Returns a float Tensor as the de-noised counts.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.de_noise
9 |
10 |
11 |
12 |
20 |
21 | Returns a float `Tensor` as the de-noised `counts`.
22 |
23 |
24 | tfr.utils.de_noise(
25 | counts, noise, ratio=0.9
26 | )
27 |
28 |
29 |
30 |
31 | The implementation is based on the the paper by Zhang and Xu: "Fast Exact
32 | Maximum Likelihood Estimation for Mixture of Language Models." It assumes that
33 | the observed `counts` are generated from a mixture of `noise` and the true
34 | distribution: `ratio * noise_distribution + (1 - ratio) * true_distribution`,
35 | where the contribution of `noise` is controlled by `ratio`. This method returns
36 | the true distribution.
37 |
38 |
39 |
40 |
41 | Args |
42 |
43 |
44 |
45 | `counts`
46 | |
47 |
48 | A 2-D `Tensor` representing the observations. All values should be
49 | nonnegative.
50 | |
51 |
52 |
53 | `noise`
54 | |
55 |
56 | A 2-D `Tensor` representing the noise distribution. This should be
57 | the same shape as `counts`. All values should be positive and are
58 | normalized to a simplex per row.
59 | |
60 |
61 |
62 | `ratio`
63 | |
64 |
65 | A float in (0, 1) representing the contribution from noise.
66 | |
67 |
68 |
69 |
70 |
71 |
72 |
73 | Returns |
74 |
75 |
76 | A 2-D float `Tensor` and each row is a simplex.
77 | |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 | Raises |
86 |
87 |
88 |
89 | `ValueError`
90 | |
91 |
92 | if `ratio` is not in (0,1).
93 | |
94 |
95 |
96 | `InvalidArgumentError`
97 | |
98 |
99 | if any of `counts` is negative or any of `noise` is
100 | not positive.
101 | |
102 |
103 |
104 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/gather_per_row.md:
--------------------------------------------------------------------------------
1 | description: Gathers the values from input tensor based on per-row indices.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.gather_per_row
9 |
10 |
11 |
12 |
20 |
21 | Gathers the values from input tensor based on per-row indices.
22 |
23 |
24 | tfr.utils.gather_per_row(
25 | inputs, indices
26 | )
27 |
28 |
29 |
30 |
31 | #### Example Usage:
32 |
33 | ```python
34 | scores = [[1., 3., 2.], [1., 2., 3.]]
35 | indices = [[1, 2], [2, 1]]
36 | tfr.utils.gather_per_row(scores, indices)
37 | ```
38 | Returns [[3., 2.], [3., 2.]]
39 |
40 |
41 |
42 |
43 | Args |
44 |
45 |
46 |
47 | `inputs`
48 | |
49 |
50 | (tf.Tensor) A tensor of shape [batch_size, list_size] or
51 | [batch_size, list_size, feature_dims].
52 | |
53 |
54 |
55 | `indices`
56 | |
57 |
58 | (tf.Tensor) A tensor of shape [batch_size, size] of positions to
59 | gather inputs from. Each index corresponds to a row entry in input_tensor.
60 | |
61 |
62 |
63 |
64 |
65 |
66 |
67 | Returns |
68 |
69 |
70 | A tensor of values gathered from inputs, of shape [batch_size, size] or
71 | [batch_size, size, feature_dims], depending on whether the input was 2D or
72 | 3D.
73 | |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/is_label_valid.md:
--------------------------------------------------------------------------------
1 | description: Returns a boolean Tensor for label validity.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.is_label_valid
9 |
10 |
11 |
12 |
20 |
21 | Returns a boolean `Tensor` for label validity.
22 |
23 |
24 | tfr.utils.is_label_valid(
25 | labels
26 | )
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/organize_valid_indices.md:
--------------------------------------------------------------------------------
1 | description: Organizes indices in such a way that valid items appear first.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.organize_valid_indices
9 |
10 |
11 |
12 |
20 |
21 | Organizes indices in such a way that valid items appear first.
22 |
23 |
24 | tfr.utils.organize_valid_indices(
25 | is_valid, shuffle=True, seed=None
26 | )
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `is_valid`
39 | |
40 |
41 | A boolean `Tensor` for entry validity with shape [batch_size,
42 | list_size].
43 | |
44 |
45 |
46 | `shuffle`
47 | |
48 |
49 | A boolean indicating whether valid items should be shuffled.
50 | |
51 |
52 |
53 | `seed`
54 | |
55 |
56 | An int for random seed at the op level. It works together with the
57 | seed at global graph level together to determine the random number
58 | generation. See `tf.set_random_seed`.
59 | |
60 |
61 |
62 |
63 |
64 |
65 |
66 | Returns |
67 |
68 |
69 | A tensor of indices with shape [batch_size, list_size, 2]. The returned
70 | tensor can be used with `tf.gather_nd` and `tf.scatter_nd` to compose a new
71 | [batch_size, list_size] tensor. The values in the last dimension are the
72 | indices for an element in the input tensor.
73 | |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/padded_nd_indices.md:
--------------------------------------------------------------------------------
1 | description: Pads the invalid entries by valid ones and returns the nd_indices.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.padded_nd_indices
9 |
10 |
11 |
12 |
20 |
21 | Pads the invalid entries by valid ones and returns the nd_indices.
22 |
23 |
24 | tfr.utils.padded_nd_indices(
25 | is_valid, shuffle=False, seed=None
26 | )
27 |
28 |
29 |
30 |
31 | For example, when we have a batch_size = 1 and list_size = 3. Only the first 2
32 | entries are valid. We have: `is_valid = [[True, True, False]] nd_indices, mask =
33 | padded_nd_indices(is_valid)` nd_indices has a shape [1, 3, 2] and mask has a
34 | shape [1, 3].
35 |
36 | ```
37 | nd_indices = [[[0, 0], [0, 1], [0, 0]]]
38 | mask = [[True, True, False]]
39 | ```
40 |
41 | nd_indices can be used by gather_nd on a Tensor t `padded_t = tf.gather_nd(t,
42 | nd_indices)` and get the following Tensor with first 2 dims are [1, 3]:
43 | `padded_t = [[t(0, 0), t(0, 1), t(0, 0)]]`
44 |
45 |
46 |
47 |
48 | Args |
49 |
50 |
51 |
52 | `is_valid`
53 | |
54 |
55 | A boolean `Tensor` for entry validity with shape [batch_size,
56 | list_size].
57 | |
58 |
59 |
60 | `shuffle`
61 | |
62 |
63 | A boolean that indicates whether valid indices should be shuffled.
64 | |
65 |
66 |
67 | `seed`
68 | |
69 |
70 | Random seed for shuffle.
71 | |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Returns |
79 |
80 |
81 | A tuple of Tensors (nd_indices, mask). The first has shape [batch_size,
82 | list_size, 2] and it can be used in gather_nd or scatter_nd. The second has
83 | the shape of [batch_size, list_size] with value True for valid indices.
84 | |
85 |
86 |
87 |
88 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/parse_keys_and_weights.md:
--------------------------------------------------------------------------------
1 | description: Parses the encoded key to keys and weights.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.parse_keys_and_weights
9 |
10 |
11 |
12 |
20 |
21 | Parses the encoded key to keys and weights.
22 |
23 |
24 | tfr.utils.parse_keys_and_weights(
25 | key: str
26 | ) -> Dict[str, float]
27 |
28 |
29 |
30 |
31 | This parse function will remove all spaces. Different keys are split by "," and
32 | then weight associated with key is split by ":".
33 |
34 |
35 |
36 |
37 | Args |
38 |
39 |
40 |
41 | `key`
42 | |
43 |
44 | A string represents a key, or a string of multiple keys, split by ",",
45 | and weighted by the weights split by ":". For example, key =
46 | 'softmax_loss:0.9,sigmoid_cross_entropy_loss:0.1'.
47 | |
48 |
49 |
50 |
51 |
52 |
53 |
54 | Returns |
55 |
56 |
57 | A dict from keys to weights.
58 | |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/ragged_to_dense.md:
--------------------------------------------------------------------------------
1 | description: Converts given inputs from ragged tensors to dense tensors.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.ragged_to_dense
9 |
10 |
11 |
12 |
20 |
21 | Converts given inputs from ragged tensors to dense tensors.
22 |
23 |
24 | tfr.utils.ragged_to_dense(
25 | labels, predictions, weights
26 | )
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `labels`
39 | |
40 |
41 | A `tf.RaggedTensor` of the same shape as `predictions` representing
42 | relevance.
43 | |
44 |
45 |
46 | `predictions`
47 | |
48 |
49 | A `tf.RaggedTensor` with shape [batch_size, (list_size)]. Each
50 | value is the ranking score of the corresponding example.
51 | |
52 |
53 |
54 | `weights`
55 | |
56 |
57 | An optional `tf.RaggedTensor` of the same shape of predictions or a
58 | `tf.Tensor` of shape [batch_size, 1]. The former case is per-example and
59 | the latter case is per-list.
60 | |
61 |
62 |
63 |
64 |
65 |
66 |
67 | Returns |
68 |
69 |
70 | A tuple (labels, predictions, weights, mask) of dense `tf.Tensor`s.
71 | |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/reshape_first_ndims.md:
--------------------------------------------------------------------------------
1 | description: Reshapes the first n dims of the input tensor to new shape.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.reshape_first_ndims
9 |
10 |
11 |
12 |
20 |
21 | Reshapes the first n dims of the input `tensor` to `new shape`.
22 |
23 |
24 | tfr.utils.reshape_first_ndims(
25 | tensor, first_ndims, new_shape
26 | )
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `tensor`
39 | |
40 |
41 | The input `Tensor`.
42 | |
43 |
44 |
45 | `first_ndims`
46 | |
47 |
48 | A int denoting the first n dims.
49 | |
50 |
51 |
52 | `new_shape`
53 | |
54 |
55 | A list of int representing the new shape.
56 | |
57 |
58 |
59 |
60 |
61 |
62 |
63 | Returns |
64 |
65 |
66 | A reshaped `Tensor`.
67 | |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/reshape_to_2d.md:
--------------------------------------------------------------------------------
1 | description: Converts the given tensor to a 2-D Tensor.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.reshape_to_2d
9 |
10 |
11 |
12 |
20 |
21 | Converts the given `tensor` to a 2-D `Tensor`.
22 |
23 |
24 | tfr.utils.reshape_to_2d(
25 | tensor
26 | )
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/shuffle_valid_indices.md:
--------------------------------------------------------------------------------
1 | description: Returns a shuffle of indices with valid ones on top.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.shuffle_valid_indices
9 |
10 |
11 |
12 |
20 |
21 | Returns a shuffle of indices with valid ones on top.
22 |
23 |
24 | tfr.utils.shuffle_valid_indices(
25 | is_valid, seed=None
26 | )
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/sort_by_scores.md:
--------------------------------------------------------------------------------
1 | description: Sorts list of features according to per-example scores.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.sort_by_scores
9 |
10 |
11 |
12 |
20 |
21 | Sorts list of features according to per-example scores.
22 |
23 |
24 | tfr.utils.sort_by_scores(
25 | scores, features_list, topn=None, shuffle_ties=True, seed=None, mask=None
26 | )
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | Args |
35 |
36 |
37 |
38 | `scores`
39 | |
40 |
41 | A `Tensor` of shape [batch_size, list_size] representing the
42 | per-example scores.
43 | |
44 |
45 |
46 | `features_list`
47 | |
48 |
49 | A list of `Tensor`s to be sorted. The shape of the `Tensor`
50 | can be [batch_size, list_size] or [batch_size, list_size, feature_dims].
51 | The latter is applicable for example features.
52 | |
53 |
54 |
55 | `topn`
56 | |
57 |
58 | An integer as the cutoff of examples in the sorted list.
59 | |
60 |
61 |
62 | `shuffle_ties`
63 | |
64 |
65 | A boolean. If True, randomly shuffle before the sorting.
66 | |
67 |
68 |
69 | `seed`
70 | |
71 |
72 | The ops-level random seed used when `shuffle_ties` is True.
73 | |
74 |
75 |
76 | `mask`
77 | |
78 |
79 | An optional `Tensor` of shape [batch_size, list_size] representing
80 | which entries are valid for sorting. Invalid entries will be pushed to the
81 | end.
82 | |
83 |
84 |
85 |
86 |
87 |
88 |
89 | Returns |
90 |
91 |
92 | A list of `Tensor`s as the list of sorted features by `scores`.
93 | |
94 |
95 |
96 |
97 |
--------------------------------------------------------------------------------
/docs/api_docs/python/tfr/utils/sorted_ranks.md:
--------------------------------------------------------------------------------
1 | description: Returns an int Tensor as the ranks (1-based) after sorting scores.
2 |
3 |
4 |
5 |
6 |
7 |
8 | # tfr.utils.sorted_ranks
9 |
10 |
11 |
12 |
20 |
21 | Returns an int `Tensor` as the ranks (1-based) after sorting scores.
22 |
23 |
24 | tfr.utils.sorted_ranks(
25 | scores, shuffle_ties=True, seed=None
26 | )
27 |
28 |
29 |
30 |
31 | Example: Given scores = [[1.0, 3.5, 2.1]], the returned ranks will be [[3, 1,
32 | 2]]. It means that scores 1.0 will be ranked at position 3, 3.5 will be ranked
33 | at position 1, and 2.1 will be ranked at position 2.
34 |
35 |
36 |
37 |
38 | Args |
39 |
40 |
41 |
42 | `scores`
43 | |
44 |
45 | A `Tensor` of shape [batch_size, list_size] representing the
46 | per-example scores.
47 | |
48 |
49 |
50 | `shuffle_ties`
51 | |
52 |
53 | See `sort_by_scores`.
54 | |
55 |
56 |
57 | `seed`
58 | |
59 |
60 | See `sort_by_scores`.
61 | |
62 |
63 |
64 |
65 |
66 |
67 |
68 | Returns |
69 |
70 |
71 | A 1-based int `Tensor`s as the ranks.
72 | |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/docs/build_docs.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | r"""Generates api_docs for tensorflow_ranking.
16 |
17 | ```shell
18 | pip install git+http://github.com/tensorflow/docs
19 | python build_docs.py --output_dir=/tmp/tfr_api
20 | ```
21 | """
22 |
23 | import os
24 | import sys
25 |
26 | from absl import app
27 | from absl import flags
28 |
29 | from tensorflow_docs.api_generator import generate_lib
30 | from tensorflow_docs.api_generator import public_api
31 |
32 | import tensorflow_ranking as tfr
33 |
34 | # pylint: disable=g-import-not-at-top
35 | try:
36 | import tensorflow_models
37 | except ImportError:
38 | import subprocess
39 | subprocess.check_call(
40 | [sys.executable, '-m', 'pip', 'install', '-U', 'tf-models-official'])
41 | import tensorflow_models
42 | del tensorflow_models
43 |
44 | import tensorflow_ranking.extension.premade # pylint: disable=unused-import
45 | # pylint: enable=g-import-not-at-top
46 |
47 | # Hide these from the documentation. Nobody should be accessing things through
48 | # `tfr.python.*`.
49 | del tfr.python
50 |
51 | # `losses_impl` and `metrics_impl` are not available under tfr namespace, see
52 | # `dir(tfr)` for available APIs. These must be removed from the documentation.
53 | del tfr.losses_impl
54 | del tfr.losses.losses_impl
55 | del tfr.metrics_impl
56 | del tfr.metrics.metrics_impl
57 |
58 | # Removing references to `estimator` or feature columns APIs in docstrings.
59 | del tfr.estimator
60 | del tfr.ext
61 | del tfr.feature
62 | del tfr.head
63 | del tfr.keras.estimator
64 | del tfr.keras.feature
65 | del tfr.keras.network
66 | del tfr.keras.canned
67 | del tfr.losses # Keras losses available via tfr.keras.losses.
68 | del tfr.metrics # Keras metrics available via tfr.keras.metrics.
69 | del tfr.model
70 |
71 |
72 | FLAGS = flags.FLAGS
73 |
74 |
75 | flags.DEFINE_string('output_dir', '/tmp/tfr_api',
76 | 'Where to write the resulting docs to.')
77 | flags.DEFINE_string(
78 | 'code_url_prefix',
79 | ('https://github.com/tensorflow/ranking/tree/master/tensorflow_ranking'),
80 | 'The url prefix for links to code.')
81 |
82 | flags.DEFINE_bool('search_hints', True,
83 | 'Include metadata search hints in the generated files.')
84 |
85 | flags.DEFINE_string('site_path', 'ranking/api_docs/python',
86 | 'Path prefix in the _toc.yaml')
87 |
88 |
89 | def build_docs(output_dir):
90 | """Build api docs for TensorFlow Ranking package."""
91 | doc_generator = generate_lib.DocGenerator(
92 | root_title='TensorFlow Ranking',
93 | py_modules=[('tfr', tfr)],
94 | base_dir=os.path.dirname(tfr.__file__),
95 | search_hints=FLAGS.search_hints,
96 | code_url_prefix=FLAGS.code_url_prefix,
97 | site_path=FLAGS.site_path,
98 | callbacks=[public_api.local_definitions_filter])
99 |
100 | doc_generator.build(output_dir)
101 | print('Output docs to: ', FLAGS.output_dir)
102 |
103 |
104 | def main(_):
105 | build_docs(FLAGS.output_dir)
106 |
107 |
108 | if __name__ == '__main__':
109 | app.run(main)
110 |
--------------------------------------------------------------------------------
/docs/images/gam-example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/docs/images/gam-example.png
--------------------------------------------------------------------------------
/docs/images/ranking-overview.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/docs/images/ranking-overview.jpeg
--------------------------------------------------------------------------------
/docs/images/tensorflow_ranking_pipeline_class.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/docs/images/tensorflow_ranking_pipeline_class.jpg
--------------------------------------------------------------------------------
/docs/images/tfr-bert.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/docs/images/tfr-bert.jpeg
--------------------------------------------------------------------------------
/docs/images/tfr-distributed-modules.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/docs/images/tfr-distributed-modules.jpeg
--------------------------------------------------------------------------------
/docs/install.md:
--------------------------------------------------------------------------------
1 | # Install TensorFlow Ranking
2 |
3 | There are several ways to set up your environment to use the TensorFlow Ranking
4 | library.
5 |
6 | + The easiest way to learn and use TensorFlow Ranking is run any of the
7 | tutorials Google Colab. Select the link at the top of the
8 | [Quickstart tutorial](/ranking/tutorials/quickstart)).
9 | + To use the Ranking library on a local machine, install the
10 | `tensorflow_ranking` pip package.
11 | + If you have a unique machine configuration, you can build the package
12 | from source, using the [Build from source](#build-source) instructions.
13 |
14 | ## Install TensorFlow Ranking using pip
15 |
16 | Install using pip.
17 |
18 | ```posix-terminal
19 | pip install --upgrade tensorflow_ranking
20 | ```
21 |
22 | ## Build from source {:#build-source}
23 |
24 | You can also install from source, which requires the
25 | [Bazel](https://bazel.build/) build system.
26 |
27 | 1. Install Bazel, Git and Pip.
28 | ```posix-terminal
29 | sudo apt-get update
30 |
31 | sudo apt-get install bazel git python3-pip python3-venv
32 | ```
33 | 1. Clone the TensorFlow Ranking repository.
34 |
35 | git clone https://github.com/tensorflow/ranking.git
36 | 1. Build TensorFlow Ranking wheel file and store them in a
37 | `/tmp/ranking_pip` folder.
38 | ```posix-terminal
39 | cd ranking # folder cloned in Step 2.
40 |
41 | bazel build //tensorflow_ranking/tools/pip_package:build_pip_package
42 |
43 | bazel-bin/tensorflow_ranking/tools/pip_package/build_pip_package \
44 | /tmp/ranking_pip
45 | ```
46 | 1. Activate a `venv` environment.
47 | ```posix-terminal
48 | python3 -m venv --system-site-packages venv
49 |
50 | source venv/bin/activate
51 | ```
52 | 1. Install the wheel package in your `venv` environment.
53 | ```devsite-terminal {:.tfo-terminal-venv}
54 | pip install /tmp/ranking_pip/tensorflow_ranking*.whl
55 | ```
56 | 1. Optionally, run all TensorFlow Ranking tests.
57 | ```devsite-terminal {:.tfo-terminal-venv}
58 | bazel test //tensorflow_ranking/...
59 | ```
60 |
61 | For more information about installing Python, pip, TensorFlow, and working with
62 | Python virtual environments, see
63 | [Install TensorFlow with pip](/install/pip#2.-create-a-virtual-environment-recommended).
64 |
--------------------------------------------------------------------------------
/tensorflow_ranking/BUILD:
--------------------------------------------------------------------------------
1 | # TensorFlow Ranking, the project to build ranking models on TensorFlow.
2 |
3 | # Placeholder: load py_library
4 |
5 | package(default_visibility = ["//visibility:public"])
6 |
7 | licenses(["notice"])
8 |
9 | exports_files(["LICENSE"])
10 |
11 | py_library(
12 | name = "tensorflow_ranking",
13 | srcs = ["__init__.py"],
14 | srcs_version = "PY2AND3",
15 | visibility = ["//visibility:public"],
16 | deps = [
17 | "//tensorflow_ranking/extension",
18 | "//tensorflow_ranking/python",
19 | "//tensorflow_ranking/python:version",
20 | ],
21 | )
22 |
--------------------------------------------------------------------------------
/tensorflow_ranking/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """TensorFlow Ranking, the project to build ranking models on TensorFlow."""
16 |
17 | # Contributors to the `python/` dir should not alter this file; instead update
18 | # `python/__init__.py` as necessary.
19 |
20 | from tensorflow_ranking import extension as ext
21 | from tensorflow_ranking.python import * # pylint: disable=wildcard-import
22 | from tensorflow_ranking.python.version import __version__
23 |
--------------------------------------------------------------------------------
/tensorflow_ranking/datasets/rd_suite/README.md:
--------------------------------------------------------------------------------
1 | # RD-Suite: A Benchmark for Ranking Distillation
2 |
3 | RD-Suite provides a dataset for ranking distillation research.
4 |
5 | ## Evaluation Script
6 | RD_Suite_Eval_Script.ipynb uses open-sourced operations to reproduce teacher
7 | ranker metrics, using the TREC data formats provided below.
8 |
9 | ## Data
10 | We provide data in TREC format. The evaluation script automatically downloads
11 | rd_suite_test.zip, which contains dev/test data for all tasks, and can be used
12 | to run the evaluation script end-to-end. The other 4 datasets are self-contained
13 | as listed below. For NQ, we also provide teacher scores from a ranker trained on
14 | MSMARCO for the distillation transfer task.
15 |
16 | | Dataset | Size | Link |
17 | |:---------------:|:------:|:-----------:|
18 | | MSMARCO | 513M | [msmarco.zip](https://storage.googleapis.com/gresearch/rd-suite/msmarco.zip) |
19 | | NQ | 125M | [nq.zip](https://storage.googleapis.com/gresearch/rd-suite/nq.zip) |
20 | | Web30K | 65M | [web30k.zip](https://storage.googleapis.com/gresearch/rd-suite/web30k.zip) |
21 | | Istella | 173M | [istella.zip](https://storage.googleapis.com/gresearch/rd-suite/istella.zip) |
22 |
23 |
24 | ## Citation
25 | You can cite our work by copying the following bibtex:
26 |
27 | ```
28 | @article{qin2023rd,
29 | title={RD-Suite: A Benchmark for Ranking Distillation},
30 | author={Qin, Zhen and Jagerman, Rolf and Pasumarthi, Rama and Zhuang, Honglei and Zhang, He and Bai, Aijun and Hui, Kai and Yan, Le and Wang, Xuanhui},
31 | journal={arXiv preprint arXiv:2306.04455},
32 | year={2023}
33 | }
34 | ```
35 |
36 |
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/data/eval_elwc.tfrecord:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/examples/data/eval_elwc.tfrecord
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/data/test_numerical_elwc.tfrecord:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/examples/data/test_numerical_elwc.tfrecord
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/data/train_elwc.tfrecord:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/examples/data/train_elwc.tfrecord
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/data/train_numerical_elwc.tfrecord:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/examples/data/train_numerical_elwc.tfrecord
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/data/vali_numerical_elwc.tfrecord:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/examples/data/vali_numerical_elwc.tfrecord
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/data/vocab.txt:
--------------------------------------------------------------------------------
1 | relevant
2 | question
3 | answer
4 | irrelevant
5 | data
6 |
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/keras/README.md:
--------------------------------------------------------------------------------
1 | # TF-Ranking Keras Examples.
2 |
3 | This folder contains the example scripts for running Keras ranking models, and
4 | the landing page example on TensorFlow subsite for
5 | [Ranking](https://tensorflow.org/ranking). Please go to the website for more
6 | details on guides, tutorial and API documentation.
7 |
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/keras/tfrbert_antique_train_config.yaml:
--------------------------------------------------------------------------------
1 | task:
2 | model:
3 | dropout_rate: 0.1
4 | init_checkpoint: '/tmp/tfrbert/uncased_L-12_H-768_A-12/bert_model.ckpt'
5 | train_data:
6 | drop_remainder: true
7 | global_batch_size: 4
8 | input_path: '/tmp/tfrbert/data/antique_train_seq_64_elwc.tfrecords'
9 | is_training: true
10 | seq_length: 64
11 | list_size: 10
12 | validation_data:
13 | drop_remainder: false
14 | global_batch_size: 4
15 | input_path: '/tmp/tfrbert/data/antique_test_seq_64_elwc.tfrecords'
16 | is_training: false
17 | seq_length: 64
18 | list_size: 10
19 | loss: 'softmax_loss'
20 | output_preds: false
21 | trainer:
22 | checkpoint_interval: 500
23 | optimizer_config:
24 | learning_rate:
25 | polynomial:
26 | # 100% of train_steps.
27 | decay_steps: 10000
28 | end_learning_rate: 0.0
29 | initial_learning_rate: 3.0e-05
30 | power: 1.0
31 | type: polynomial
32 | optimizer:
33 | type: adamw
34 | warmup:
35 | polynomial:
36 | power: 1
37 | # ~10% of train_steps.
38 | warmup_steps: 1000
39 | type: polynomial
40 | steps_per_loop: 50
41 | summary_interval: 50
42 | train_steps: 10000
43 | validation_interval: 50
44 | validation_steps: -1
45 | best_checkpoint_export_subdir: 'best_ckpt'
46 | best_checkpoint_eval_metric: 'MAP'
47 | best_checkpoint_metric_comp: 'higher'
48 |
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/keras/tfrbert_task_experiments.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """TFR-BERT experiment configurations."""
16 | # pylint: disable=g-doc-return-or-yield,line-too-long
17 |
18 | from tensorflow_ranking.extension import premade
19 |
20 | # pylint: disable=g-import-not-at-top
21 | try:
22 | from official.core import config_definitions as cfg
23 | from official.core import exp_factory
24 | from official.modeling import optimization
25 | except ModuleNotFoundError:
26 | raise ModuleNotFoundError(
27 | 'tf-models-official needs to be installed. Run command: '
28 | '`pip install tf-models-official`.') from None
29 | # pylint: enable=g-import-not-at-top
30 |
31 |
32 | @exp_factory.register_config_factory('tfr_bert')
33 | def tfrbert_exp() -> cfg.ExperimentConfig:
34 | """Defines a TFR-BERT experiment."""
35 | config = cfg.ExperimentConfig(
36 | task=premade.TFRBertConfig(
37 | train_data=premade.TFRBertDataConfig(),
38 | validation_data=premade.TFRBertDataConfig(
39 | is_training=False, drop_remainder=False)),
40 | trainer=cfg.TrainerConfig(
41 | optimizer_config=optimization.OptimizationConfig({
42 | 'optimizer': {
43 | 'type': 'adamw',
44 | 'adamw': {
45 | 'weight_decay_rate':
46 | 0.01,
47 | 'exclude_from_weight_decay':
48 | ['LayerNorm', 'layer_norm', 'bias'],
49 | }
50 | },
51 | 'learning_rate': {
52 | 'type': 'polynomial',
53 | 'polynomial': {
54 | 'initial_learning_rate': 3e-5,
55 | 'end_learning_rate': 0.0,
56 | }
57 | },
58 | 'warmup': {
59 | 'type': 'polynomial'
60 | }
61 | })),
62 | restrictions=[
63 | 'task.train_data.is_training != None',
64 | 'task.validation_data.is_training != None'
65 | ])
66 | config.task.model.encoder.type = 'bert'
67 | return config
68 |
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/keras/tfrbert_task_experiments_test.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Tests for tfrbert_task_experiments."""
16 |
17 | import tensorflow as tf
18 |
19 | from official.core import config_definitions as cfg
20 | from tensorflow_ranking.examples.keras import tfrbert_task_experiments
21 |
22 |
23 | class TfrbertTaskExperimentsTest(tf.test.TestCase):
24 |
25 | def test_create_experiment_config(self):
26 | exp_config = tfrbert_task_experiments.tfrbert_exp()
27 | self.assertIsInstance(exp_config, cfg.ExperimentConfig)
28 |
29 |
30 | if __name__ == '__main__':
31 | tf.test.main()
32 |
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/tf_ranking_canned_gam_test.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Tests for tf_ranking_canned_gam.py."""
16 |
17 | import os
18 |
19 | from absl import flags
20 | from absl.testing import flagsaver
21 | from absl.testing import parameterized
22 |
23 | import tensorflow as tf
24 |
25 | from google.protobuf import text_format
26 | from tensorflow_ranking.examples import tf_ranking_canned_gam
27 | from tensorflow_serving.apis import input_pb2
28 |
29 | FLAGS = flags.FLAGS
30 |
31 | ELWC = text_format.Parse(
32 | """
33 | examples {
34 | features {
35 | feature {
36 | key: "custom_features_1"
37 | value { float_list { value: 1.0 } }
38 | }
39 | feature {
40 | key: "custom_features_2"
41 | value { float_list { value: 1.5 } }
42 | }
43 | feature {
44 | key: "utility"
45 | value { int64_list { value: 1 } }
46 | }
47 | }
48 | }
49 | examples {
50 | features {
51 | feature {
52 | key: "custom_features_1"
53 | value { float_list { value: 1.0 } }
54 | }
55 | feature {
56 | key: "custom_features_3"
57 | value { float_list { value: 2.1 } }
58 | }
59 | feature {
60 | key: "utility"
61 | value { int64_list { value: 0 } }
62 | }
63 | }
64 | }""", input_pb2.ExampleListWithContext())
65 |
66 |
67 | def _write_tfrecord_files(path):
68 | elwc_list = [ELWC.SerializeToString()] * 10
69 | if tf.io.gfile.exists(path):
70 | tf.io.gfile.remove(path)
71 |
72 | with tf.io.TFRecordWriter(path) as writer:
73 | for elwc in elwc_list:
74 | writer.write(elwc)
75 |
76 |
77 | class TFRankingCannedGAMTest(tf.test.TestCase, parameterized.TestCase):
78 |
79 | def setUp(self):
80 | super(TFRankingCannedGAMTest, self).setUp()
81 | tf.compat.v1.reset_default_graph()
82 |
83 | # Prepares model directory, and train and eval data.
84 | self._base_model_dir = tf.compat.v1.test.get_temp_dir() + "/model/"
85 | tf.io.gfile.makedirs(self._base_model_dir)
86 | self._data_file = os.path.join(self._base_model_dir, "elwc.tfrecord")
87 | _write_tfrecord_files(self._data_file)
88 |
89 | def tearDown(self):
90 | super(TFRankingCannedGAMTest, self).tearDown()
91 | if self._base_model_dir:
92 | tf.io.gfile.rmtree(self._base_model_dir)
93 | self._base_model_dir = None
94 |
95 | @parameterized.named_parameters(("enable_listwise_inference", True),
96 | ("disable_listwise_inference", False))
97 | def test_train_and_eval(self, listwise_inference):
98 | self._model_dir = self._base_model_dir + "/" + str(listwise_inference)
99 | with flagsaver.flagsaver(
100 | train_input_pattern=self._data_file,
101 | eval_input_pattern=self._data_file,
102 | num_features=3,
103 | model_dir=self._model_dir,
104 | num_train_steps=10,
105 | listwise_inference=listwise_inference):
106 | tf_ranking_canned_gam.train_and_eval()
107 |
108 |
109 | if __name__ == "__main__":
110 | tf.test.main()
111 |
--------------------------------------------------------------------------------
/tensorflow_ranking/examples/tf_ranking_libsvm_test.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Tests for tf_ranking_libsvm.py."""
16 |
17 | import os
18 |
19 | from absl import flags
20 | from absl.testing import flagsaver
21 | from absl.testing import parameterized
22 |
23 | import tensorflow as tf
24 |
25 | from tensorflow_ranking.examples import tf_ranking_libsvm
26 |
27 | FLAGS = flags.FLAGS
28 |
29 | LIBSVM_DATA = """1 qid:10 32:0.14 48:0.97 51:0.45
30 | 0 qid:10 1:0.15 31:0.75 32:0.24 49:0.6
31 | 2 qid:10 1:0.71 2:0.36 31:0.58 51:0.12
32 | 0 qid:20 4:0.79 31:0.01 33:0.05 35:0.27
33 | 3 qid:20 1:0.42 28:0.79 35:0.30 42:0.76
34 | """
35 |
36 |
37 | class TfRankingLibSVMTest(tf.test.TestCase, parameterized.TestCase):
38 |
39 | @parameterized.named_parameters(("single_head", None),
40 | ("multi_head", "softmax_loss"))
41 | def test_train_and_eval(self, secondary_loss):
42 | data_dir = tf.compat.v1.test.get_temp_dir()
43 | data_file = os.path.join(data_dir, "libvsvm.txt")
44 | if tf.io.gfile.exists(data_file):
45 | tf.io.gfile.remove(data_file)
46 |
47 | with open(data_file, "wt") as writer:
48 | writer.write(LIBSVM_DATA)
49 |
50 | output_dir = os.path.join(data_dir, secondary_loss or "")
51 |
52 | with flagsaver.flagsaver(
53 | train_path=data_file,
54 | vali_path=data_file,
55 | test_path=data_file,
56 | output_dir=output_dir,
57 | loss="pairwise_logistic_loss",
58 | secondary_loss=secondary_loss,
59 | num_train_steps=10,
60 | list_size=10,
61 | group_size=2,
62 | num_features=100):
63 | tf_ranking_libsvm.train_and_eval()
64 |
65 | if tf.io.gfile.exists(output_dir):
66 | tf.io.gfile.rmtree(output_dir)
67 |
68 |
69 | if __name__ == "__main__":
70 | tf.test.main()
71 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/BUILD:
--------------------------------------------------------------------------------
1 | # Description:
2 | # Folder for TensorFlow ranking extensions.
3 |
4 | # Placeholder: load py_library
5 | # Placeholder: load py_test
6 |
7 | package(
8 | default_visibility = [
9 | "//visibility:public",
10 | ],
11 | )
12 |
13 | licenses(["notice"])
14 |
15 | py_library(
16 | name = "extension",
17 | srcs = ["__init__.py"],
18 | srcs_version = "PY2AND3",
19 | visibility = ["//tensorflow_ranking:__pkg__"],
20 | deps = [
21 | "//tensorflow_ranking/extension/premade",
22 | ],
23 | )
24 |
25 | py_library(
26 | name = "pipeline",
27 | srcs = ["pipeline.py"],
28 | srcs_version = "PY2AND3",
29 | deps = [
30 | # py/tensorflow dep,
31 | # py/tensorflow:tensorflow_compat_v1_estimator dep,
32 | # py/tensorflow:tensorflow_estimator dep,
33 | "//tensorflow_ranking/python:data",
34 | ],
35 | )
36 |
37 | py_test(
38 | name = "pipeline_test",
39 | size = "large",
40 | srcs = ["pipeline_test.py"],
41 | python_version = "PY3",
42 | shard_count = 2,
43 | srcs_version = "PY3",
44 | tags = [
45 | "no_pip",
46 | "notsan",
47 | ],
48 | visibility = ["//visibility:private"],
49 | deps = [
50 | ":pipeline",
51 | # py/absl/testing:parameterized dep,
52 | # Placeholder: proto upb dep
53 | # py/tensorflow dep,
54 | # py/tensorflow:tensorflow_estimator dep,
55 | "//tensorflow_ranking",
56 | # tensorflow_serving/apis:input_proto_py_pb2 dep,
57 | ],
58 | )
59 |
60 | filegroup(
61 | name = "testdata",
62 | srcs = glob(["testdata/**"]),
63 | )
64 |
65 | py_library(
66 | name = "task",
67 | srcs = ["task.py"],
68 | srcs_version = "PY3",
69 | deps = [
70 | # py/absl/logging dep,
71 | # py/orbit dep,
72 | # py/tensorflow dep,
73 | # tensorflow_models/official/core:base_task dep,
74 | # tensorflow_models/official/core:config_definitions dep,
75 | # tensorflow_models/official/core:input_reader dep,
76 | # tensorflow_models/official/core:task_factory dep,
77 | # tensorflow_models/official/modeling:tf_utils dep,
78 | # tensorflow_models/official/nlp/data:data_loader dep,
79 | # tensorflow_models/official/nlp/modeling dep,
80 | "//tensorflow_ranking/python:data",
81 | "//tensorflow_ranking/python/keras:losses",
82 | "//tensorflow_ranking/python/keras:metrics",
83 | "//tensorflow_ranking/python/keras:model",
84 | ],
85 | )
86 |
87 | py_test(
88 | name = "task_test",
89 | size = "large",
90 | srcs = ["task_test.py"],
91 | python_version = "PY3",
92 | srcs_version = "PY3",
93 | deps = [
94 | ":task",
95 | # py/absl/testing:parameterized dep,
96 | # Placeholder: proto upb dep
97 | # py/tensorflow dep,
98 | "//tensorflow_ranking/python/keras:model",
99 | # tensorflow_serving/apis:input_proto_py_pb2 dep,
100 | ],
101 | )
102 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """TensorFlow Ranking library."""
16 |
17 | from __future__ import absolute_import
18 | from __future__ import division
19 | from __future__ import print_function
20 |
21 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/examples/BUILD:
--------------------------------------------------------------------------------
1 | # Description:
2 | # Example usage of tensorflow ranking extensions.
3 |
4 | # Placeholder: load py_binary
5 | # Placeholder: load py_library
6 | # Placeholder: load py_test
7 |
8 | package(
9 | default_visibility = [
10 | "//visibility:public",
11 | ],
12 | )
13 |
14 | licenses(["notice"])
15 |
16 | py_library(
17 | name = "pipeline_example_lib",
18 | srcs = ["pipeline_example.py"],
19 | srcs_version = "PY2AND3",
20 | deps = [
21 | # py/absl/flags dep,
22 | # py/tensorflow dep,
23 | # py/tensorflow:tensorflow_estimator dep,
24 | "//tensorflow_ranking",
25 | "//tensorflow_ranking/extension:pipeline",
26 | ],
27 | )
28 |
29 | py_binary(
30 | name = "pipeline_example_py_binary",
31 | srcs = ["pipeline_example.py"],
32 | main = "pipeline_example.py",
33 | python_version = "PY3",
34 | srcs_version = "PY2AND3",
35 | deps = [
36 | ":pipeline_example_lib",
37 | ],
38 | )
39 |
40 | py_test(
41 | name = "pipeline_example_test",
42 | size = "large",
43 | srcs = ["pipeline_example_test.py"],
44 | python_version = "PY3",
45 | srcs_version = "PY3",
46 | tags = [
47 | "no_pip",
48 | "notsan",
49 | ],
50 | deps = [
51 | ":pipeline_example_lib",
52 | # py/absl/flags dep,
53 | # py/absl/testing:flagsaver dep,
54 | # Placeholder: proto upb dep
55 | # py/tensorflow dep,
56 | # tensorflow_serving/apis:input_proto_py_pb2 dep,
57 | ],
58 | )
59 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/examples/pipeline_example_test.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Tests for pipeline_example_test.py."""
16 |
17 | import os
18 |
19 | from absl import flags
20 | from absl.testing import flagsaver
21 |
22 | import tensorflow as tf
23 |
24 | from google.protobuf import text_format
25 | from tensorflow_ranking.extension.examples import pipeline_example
26 | from tensorflow_serving.apis import input_pb2
27 |
28 | FLAGS = flags.FLAGS
29 |
30 | ELWC = text_format.Parse(
31 | """
32 | context {
33 | features {
34 | feature {
35 | key: "query_tokens"
36 | value { bytes_list { value: ["this", "is", "a", "relevant", "question"] } }
37 | }
38 | }
39 | }
40 | examples {
41 | features {
42 | feature {
43 | key: "document_tokens"
44 | value { bytes_list { value: ["this", "is", "a", "relevant", "answer"] } }
45 | }
46 | feature {
47 | key: "relevance"
48 | value { int64_list { value: 1 } }
49 | }
50 | feature {
51 | key: "doc_weight"
52 | value { float_list { value: 0.5 } }
53 | }
54 | }
55 | }
56 | examples {
57 | features {
58 | feature {
59 | key: "document_tokens"
60 | value { bytes_list { value: ["irrelevant", "data"] } }
61 | }
62 | feature {
63 | key: "relevance"
64 | value { int64_list { value: 0 } }
65 | }
66 | feature {
67 | key: "doc_weight"
68 | value { float_list { value: 2.0 } }
69 | }
70 | }
71 | }""", input_pb2.ExampleListWithContext())
72 |
73 |
74 | class PipelineExampleTest(tf.test.TestCase):
75 |
76 | def test_train_and_eval(self):
77 | data_dir = tf.compat.v1.test.get_temp_dir()
78 | data_file = os.path.join(data_dir, "elwc.tfrecord")
79 | if tf.io.gfile.exists(data_file):
80 | tf.io.gfile.remove(data_file)
81 |
82 | with tf.io.TFRecordWriter(data_file) as writer:
83 | for elwc in [ELWC] * 10:
84 | writer.write(elwc.SerializeToString())
85 |
86 | model_dir = os.path.join(data_dir, "model")
87 |
88 | with flagsaver.flagsaver(
89 | train_input_pattern=data_file,
90 | eval_input_pattern=data_file,
91 | model_dir=model_dir,
92 | num_train_steps=10,
93 | list_size=2):
94 | pipeline_example.train_and_eval()
95 |
96 | if tf.io.gfile.exists(model_dir):
97 | tf.io.gfile.rmtree(model_dir)
98 |
99 |
100 | if __name__ == "__main__":
101 | tf.test.main()
102 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/premade/BUILD:
--------------------------------------------------------------------------------
1 | """TFR-BERT."""
2 |
3 | # Placeholder: load py_library
4 | # Placeholder: load py_test
5 |
6 | package(
7 | default_visibility = [
8 | "//tensorflow_ranking:__subpackages__",
9 | ],
10 | )
11 |
12 | licenses(["notice"])
13 |
14 | py_library(
15 | name = "premade",
16 | srcs = ["__init__.py"],
17 | srcs_version = "PY3",
18 | deps = [
19 | ":tfrbert_task",
20 | ],
21 | )
22 |
23 | py_library(
24 | name = "tfrbert_task",
25 | srcs = ["tfrbert_task.py"],
26 | deps = [
27 | # py/absl/logging dep,
28 | # py/numpy dep,
29 | # py/tensorflow dep,
30 | # tensorflow_models/official/core:task_factory dep,
31 | # tensorflow_models/official/modeling/hyperparams:base_config dep,
32 | # tensorflow_models/official/nlp/configs:encoders dep,
33 | "//tensorflow_ranking/extension:task",
34 | "//tensorflow_ranking/python/keras:metrics",
35 | "//tensorflow_ranking/python/keras:model",
36 | ],
37 | )
38 |
39 | py_test(
40 | name = "tfrbert_task_test",
41 | srcs = ["tfrbert_task_test.py"],
42 | python_version = "PY3",
43 | deps = [
44 | ":tfrbert_task",
45 | # py/absl/flags dep,
46 | # py/absl/testing:parameterized dep,
47 | # Placeholder: proto upb dep
48 | # py/tensorflow dep,
49 | # tensorflow_models/official/nlp/configs:bert dep,
50 | # tensorflow_models/official/nlp/configs:encoders dep,
51 | # tensorflow_serving/apis:input_proto_py_pb2 dep,
52 | ],
53 | )
54 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/premade/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """TensorFlow Ranking Premade Orbit Task Module.
16 |
17 | Note: First - These APIs require These APS require the
18 | `tensorflow_models`package. You can install it with `pip install
19 | tf-models-official`. Second - Nothing under
20 | `tensorflow_ranking.extension.premade` is imported by default. To use
21 | these APIs import `premade` in your code:
22 | `import tensorflow_ranking.extension.premade`.
23 | """
24 |
25 | from tensorflow_ranking.extension.premade.tfrbert_task import * # pylint: disable=wildcard-import,line-too-long
26 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/testdata/bert_lite_config.json:
--------------------------------------------------------------------------------
1 | {
2 | "attention_probs_dropout_prob": 0.1,
3 | "hidden_act": "gelu",
4 | "hidden_dropout_prob": 0.1,
5 | "hidden_size": 2,
6 | "initializer_range": 0.02,
7 | "intermediate_size": 2,
8 | "max_position_embeddings": 4,
9 | "num_attention_heads": 1,
10 | "num_hidden_layers": 1,
11 | "type_vocab_size": 2,
12 | "vocab_size": 10
13 | }
14 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/testdata/bert_lite_tf2.ckpt.data-00000-of-00001:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/extension/testdata/bert_lite_tf2.ckpt.data-00000-of-00001
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/testdata/bert_lite_tf2.ckpt.index:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/extension/testdata/bert_lite_tf2.ckpt.index
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/testdata/bert_lite_vocab.txt:
--------------------------------------------------------------------------------
1 | [PAD]
2 | this
3 | is
4 | my
5 | simple
6 | test
7 | [UNK]
8 | [CLS]
9 | [SEP]
10 | [MASK]
11 |
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/testdata/tfrbert_elwc_test.tfrecord:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/extension/testdata/tfrbert_elwc_test.tfrecord
--------------------------------------------------------------------------------
/tensorflow_ranking/extension/testdata/tfrbert_elwc_train.tfrecord:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tensorflow/ranking/5b1d1338c1cb76507466d062e2d48bae57b17ac4/tensorflow_ranking/extension/testdata/tfrbert_elwc_train.tfrecord
--------------------------------------------------------------------------------
/tensorflow_ranking/python/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """TensorFlow Ranking library."""
16 |
17 | from __future__ import absolute_import
18 | from __future__ import division
19 | from __future__ import print_function
20 |
21 | from tensorflow_ranking.python import data
22 | from tensorflow_ranking.python import estimator
23 | from tensorflow_ranking.python import feature
24 | from tensorflow_ranking.python import head
25 | from tensorflow_ranking.python import keras
26 | from tensorflow_ranking.python import losses
27 | from tensorflow_ranking.python import metrics
28 | from tensorflow_ranking.python import model
29 | from tensorflow_ranking.python import utils
30 |
31 | from tensorflow.python.util.all_util import remove_undocumented # pylint: disable=g-bad-import-order
32 |
33 | # losses_impl and metrics_impl are added here, so that we can still depend on
34 | # them for tfr.keras.losses and metrics APIs. Note that these two are not
35 | # exposed via tfr.losses_impl, as there is no such import above.
36 | _allowed_symbols = [
37 | 'data',
38 | 'feature',
39 | 'estimator',
40 | 'head',
41 | 'keras',
42 | 'losses',
43 | 'metrics',
44 | 'model',
45 | 'utils',
46 | 'losses_impl',
47 | 'metrics_impl'
48 | ]
49 |
50 | remove_undocumented(__name__, _allowed_symbols)
51 |
--------------------------------------------------------------------------------
/tensorflow_ranking/python/keras/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """TensorFlow Ranking Keras Module."""
16 |
17 | from tensorflow_ranking.python.keras import canned
18 | from tensorflow_ranking.python.keras import estimator
19 | from tensorflow_ranking.python.keras import feature
20 | from tensorflow_ranking.python.keras import layers
21 | from tensorflow_ranking.python.keras import losses
22 | from tensorflow_ranking.python.keras import metrics
23 | from tensorflow_ranking.python.keras import model
24 | from tensorflow_ranking.python.keras import network
25 | from tensorflow_ranking.python.keras import pipeline
26 | from tensorflow_ranking.python.keras import saved_model
27 | from tensorflow_ranking.python.keras import strategy_utils
28 | from tensorflow_ranking.python.keras import utils
29 |
--------------------------------------------------------------------------------
/tensorflow_ranking/python/keras/canned/BUILD:
--------------------------------------------------------------------------------
1 | # TensorFlow Ranking Keras canned models.
2 |
3 | # Placeholder: load py_library
4 | # Placeholder: load py_test
5 |
6 | package(
7 | default_visibility = [
8 | "//tensorflow_ranking:__subpackages__",
9 | ],
10 | )
11 |
12 | licenses(["notice"])
13 |
14 | py_library(
15 | name = "canned",
16 | srcs = ["__init__.py"],
17 | srcs_version = "PY2AND3",
18 | deps = [
19 | ":dnn",
20 | ":gam",
21 | ],
22 | )
23 |
24 | py_library(
25 | name = "dnn",
26 | srcs = ["dnn.py"],
27 | srcs_version = "PY2AND3",
28 | deps = [
29 | # py/tensorflow dep,
30 | "//tensorflow_ranking/python:utils",
31 | "//tensorflow_ranking/python/keras:feature",
32 | "//tensorflow_ranking/python/keras:network",
33 | ],
34 | )
35 |
36 | py_test(
37 | name = "dnn_test",
38 | size = "small",
39 | srcs = ["dnn_test.py"],
40 | python_version = "PY3",
41 | srcs_version = "PY3",
42 | tags = [
43 | "no_pip",
44 | "notsan",
45 | ],
46 | deps = [
47 | ":dnn",
48 | # Placeholder: proto upb dep
49 | # py/tensorflow dep,
50 | "//tensorflow_ranking/python/keras:losses",
51 | "//tensorflow_ranking/python/keras:metrics",
52 | "//tensorflow_ranking/python/keras:model",
53 | ],
54 | )
55 |
56 | py_library(
57 | name = "gam",
58 | srcs = ["gam.py"],
59 | srcs_version = "PY2AND3",
60 | deps = [
61 | # py/tensorflow dep,
62 | "//tensorflow_ranking/python:utils",
63 | "//tensorflow_ranking/python/keras:feature",
64 | "//tensorflow_ranking/python/keras:model",
65 | ],
66 | )
67 |
68 | py_test(
69 | name = "gam_test",
70 | size = "small",
71 | srcs = ["gam_test.py"],
72 | python_version = "PY3",
73 | srcs_version = "PY3",
74 | tags = [
75 | "no_pip",
76 | "notsan",
77 | ],
78 | deps = [
79 | ":gam",
80 | # Placeholder: proto upb dep
81 | # py/tensorflow dep,
82 | "//tensorflow_ranking/python/keras:losses",
83 | "//tensorflow_ranking/python/keras:metrics",
84 | "//tensorflow_ranking/python/keras:network",
85 | ],
86 | )
87 |
--------------------------------------------------------------------------------
/tensorflow_ranking/python/keras/canned/__init__.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """TensorFlow Ranking Premade Keras Module."""
16 |
17 | from tensorflow_ranking.python.keras.canned.dnn import * # pylint: disable=wildcard-import
18 | from tensorflow_ranking.python.keras.canned.gam import * # pylint: disable=wildcard-import
19 |
--------------------------------------------------------------------------------
/tensorflow_ranking/python/keras/strategy_utils_test.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Tests for strategy_utils.py."""
16 |
17 | import os
18 |
19 | import tensorflow as tf
20 |
21 | from tensorflow_ranking.python.keras import strategy_utils
22 |
23 |
24 | class StrategyUtilsTest(tf.test.TestCase):
25 |
26 | def test_get_strategy(self):
27 | self.assertIsNone(strategy_utils.get_strategy(None))
28 |
29 | self.assertIsInstance(
30 | strategy_utils.get_strategy("MirroredStrategy"),
31 | tf.distribute.MirroredStrategy)
32 |
33 | self.assertIsInstance(
34 | strategy_utils.get_strategy("MultiWorkerMirroredStrategy"),
35 | tf.distribute.MultiWorkerMirroredStrategy)
36 |
37 | with self.assertRaises(ValueError):
38 | strategy_utils.get_strategy("NotSupportedStrategy")
39 |
40 | def test_get_output_filepath(self):
41 | none_strategy = strategy_utils.get_strategy(None)
42 | self.assertEqual(
43 | strategy_utils.get_output_filepath("/tmp/test", none_strategy),
44 | "/tmp/test")
45 |
46 | mirrored_strategy = strategy_utils.get_strategy("MirroredStrategy")
47 | self.assertEqual(
48 | strategy_utils.get_output_filepath("/tmp/test", mirrored_strategy),
49 | "/tmp/test")
50 |
51 | mwms_strategy = strategy_utils.get_strategy("MultiWorkerMirroredStrategy")
52 | filepath = "/tmp/test"
53 | if mwms_strategy.cluster_resolver.task_type == "worker":
54 | filepath = os.path.join(
55 | filepath, "workertemp_" + str(mwms_strategy.cluster_resolver.task_id))
56 | self.assertEqual(
57 | strategy_utils.get_output_filepath("/tmp/test", mwms_strategy),
58 | filepath)
59 |
60 |
61 | if __name__ == "__main__":
62 | tf.test.main()
63 |
--------------------------------------------------------------------------------
/tensorflow_ranking/python/keras/utils_test.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Tests for utils.py."""
16 |
17 | import tensorflow.compat.v2 as tf
18 |
19 | from tensorflow_ranking.python.keras import utils
20 |
21 |
22 | class UtilsTest(tf.test.TestCase):
23 |
24 | def test_functions_are_serializable(self):
25 | for fn in [
26 | utils.identity,
27 | utils.inverse,
28 | utils.pow_minus_1,
29 | utils.log2_inverse,
30 | utils.is_greater_equal_1,
31 | utils.symmetric_log1p,
32 | ]:
33 | self.assertIsNotNone(utils.serialize_keras_object(fn))
34 |
35 | def test_functions_are_callable(self):
36 | self.assertEqual(utils.identity(1.0), 1.0)
37 | self.assertEqual(utils.inverse(1.0), 1.0)
38 | self.assertEqual(utils.pow_minus_1(1.0), 1.0)
39 | self.assertEqual(utils.log2_inverse(1.0), 1.0)
40 | self.assertEqual(utils.is_greater_equal_1(1.0), True)
41 | self.assertAllClose(utils.symmetric_log1p(-1.0), -0.69314718056)
42 |
43 |
44 | if __name__ == '__main__':
45 | tf.enable_v2_behavior()
46 | tf.test.main()
47 |
--------------------------------------------------------------------------------
/tensorflow_ranking/python/version.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Define TensorFlow Ranking version information."""
16 |
17 | # We follow Semantic Versioning (https://semver.org/)
18 | _MAJOR_VERSION = '0'
19 | _MINOR_VERSION = '5'
20 | _PATCH_VERSION = '5'
21 |
22 | # When building releases, we can update this value on the release branch to
23 | # reflect the current release candidate ('rc0', 'rc1') or, finally, the official
24 | # stable release (indicated by `_VERSION_SUFFIX = ''`). Outside the context of a
25 | # release branch, the current version is by default assumed to be a
26 | # 'development' version, labeled 'dev'.
27 | _VERSION_SUFFIX = 'dev'
28 |
29 | # Example, '0.1.0.dev'
30 | __version__ = '.'.join([
31 | _MAJOR_VERSION,
32 | _MINOR_VERSION,
33 | _PATCH_VERSION,
34 | _VERSION_SUFFIX,
35 | ])
36 |
--------------------------------------------------------------------------------
/tensorflow_ranking/research/BUILD:
--------------------------------------------------------------------------------
1 | # Description:
2 | # TensorFlow Ranking research code for published papers.
3 |
4 | # Placeholder: load py_binary
5 | # Placeholder: load py_library
6 | # Placeholder: load py_test
7 |
8 | package(
9 | default_visibility = [
10 | "//tensorflow_ranking:__subpackages__",
11 | ],
12 | )
13 |
14 | licenses(["notice"])
15 |
16 | py_library(
17 | name = "dasalc_lib",
18 | srcs = ["dasalc.py"],
19 | srcs_version = "PY3",
20 | deps = [
21 | # py/absl/flags dep,
22 | # py/tensorflow dep,
23 | # py/tensorflow:tensorflow_estimator dep,
24 | "//tensorflow_ranking",
25 | "//tensorflow_ranking/extension:pipeline",
26 | ],
27 | )
28 |
29 | py_binary(
30 | name = "dasalc_py_binary",
31 | srcs = ["dasalc.py"],
32 | main = "dasalc.py",
33 | python_version = "PY3",
34 | srcs_version = "PY3",
35 | deps = [
36 | ":dasalc_lib",
37 | ],
38 | )
39 |
40 | py_test(
41 | name = "dasalc_test",
42 | size = "large",
43 | srcs = ["dasalc_test.py"],
44 | python_version = "PY3",
45 | srcs_version = "PY3",
46 | tags = [
47 | "no_pip",
48 | "notsan",
49 | ],
50 | deps = [
51 | ":dasalc_lib",
52 | # py/absl/flags dep,
53 | # py/absl/testing:flagsaver dep,
54 | # py/absl/testing:parameterized dep,
55 | # Placeholder: proto upb dep
56 | # py/tensorflow dep,
57 | # tensorflow_serving/apis:input_proto_py_pb2 dep,
58 | ],
59 | )
60 |
--------------------------------------------------------------------------------
/tensorflow_ranking/research/dasalc_test.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Tests for dasalc.py."""
16 |
17 | import os
18 |
19 | from absl import flags
20 | from absl.testing import flagsaver
21 | from absl.testing import parameterized
22 |
23 | import tensorflow as tf
24 |
25 | from google.protobuf import text_format
26 | from tensorflow_ranking.research import dasalc
27 | from tensorflow_serving.apis import input_pb2
28 |
29 | FLAGS = flags.FLAGS
30 |
31 | ELWC = text_format.Parse(
32 | """
33 | context {
34 | }
35 | examples {
36 | features {
37 | feature {
38 | key: "custom_features_1"
39 | value { float_list { value: 1.0 } }
40 | }
41 | feature {
42 | key: "custom_features_2"
43 | value { float_list { value: 1.5 } }
44 | }
45 | feature {
46 | key: "utility"
47 | value { int64_list { value: 1 } }
48 | }
49 | }
50 | }
51 | examples {
52 | features {
53 | feature {
54 | key: "custom_features_1"
55 | value { float_list { value: 1.0 } }
56 | }
57 | feature {
58 | key: "custom_features_3"
59 | value { float_list { value: 2.1 } }
60 | }
61 | feature {
62 | key: "utility"
63 | value { int64_list { value: 0 } }
64 | }
65 | }
66 | }""", input_pb2.ExampleListWithContext())
67 |
68 |
69 | def _write_tfrecord_files(path):
70 | elwc_list = [ELWC.SerializeToString()] * 10
71 | if tf.io.gfile.exists(path):
72 | tf.io.gfile.remove(path)
73 |
74 | with tf.io.TFRecordWriter(path) as writer:
75 | for elwc in elwc_list:
76 | writer.write(elwc)
77 |
78 |
79 | class TFRankingDASALCTest(tf.test.TestCase, parameterized.TestCase):
80 |
81 | def setUp(self):
82 | super(TFRankingDASALCTest, self).setUp()
83 | tf.compat.v1.reset_default_graph()
84 |
85 | # Prepares model directory, and train and eval data.
86 | self._base_model_dir = tf.compat.v1.test.get_temp_dir() + "/model/"
87 | tf.io.gfile.makedirs(self._base_model_dir)
88 | self._data_file = os.path.join(self._base_model_dir, "elwc.tfrecord")
89 | _write_tfrecord_files(self._data_file)
90 |
91 | def tearDown(self):
92 | super(TFRankingDASALCTest, self).tearDown()
93 | if self._base_model_dir:
94 | tf.io.gfile.rmtree(self._base_model_dir)
95 | self._base_model_dir = None
96 |
97 | def test_train_and_eval(self):
98 | self._model_dir = self._base_model_dir + "/" + "dasalc"
99 | with flagsaver.flagsaver(
100 | train_input_pattern=self._data_file,
101 | eval_input_pattern=self._data_file,
102 | model_dir=self._model_dir,
103 | batch_size=16,
104 | hidden_layer_dims=64,
105 | num_attention_layers=1,
106 | num_attention_heads=1,
107 | list_size=100,
108 | num_features=3,
109 | num_train_steps=10):
110 | dasalc.train_and_eval()
111 |
112 |
113 | if __name__ == "__main__":
114 | tf.test.main()
115 |
--------------------------------------------------------------------------------
/tensorflow_ranking/tools/pip_package/BUILD:
--------------------------------------------------------------------------------
1 | # Description:
2 | # Tools for building the TensorFlow Ranking pip package.
3 |
4 | licenses(["notice"])
5 |
6 | COMMON_PIP_DEPS = [
7 | "//tensorflow_ranking",
8 | ]
9 |
10 | sh_binary(
11 | name = "build_pip_package",
12 | srcs = ["build_pip_package.sh"],
13 | data = COMMON_PIP_DEPS,
14 | )
15 |
--------------------------------------------------------------------------------
/tensorflow_ranking/tools/pip_package/build_pip_package.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | # Copyright 2015 The TensorFlow Ranking Authors. All Rights Reserved.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | # ==============================================================================
16 | set -e
17 |
18 | function is_absolute {
19 | [[ "$1" = /* ]] || [[ "$1" =~ ^[a-zA-Z]:[/\\].* ]]
20 | }
21 |
22 | function real_path() {
23 | is_absolute "$1" && echo "$1" || echo "$PWD/${1#./}"
24 | }
25 |
26 | function build_wheel() {
27 | TMPDIR="$1"
28 | DEST="$2"
29 | PKG_NAME_FLAG="$3"
30 |
31 | mkdir -p "$TMPDIR"
32 | echo $(date) : "=== Preparing sources in dir: ${TMPDIR}"
33 |
34 | if [ ! -d bazel-bin/tensorflow_ranking ]; then
35 | echo "Could not find bazel-bin. Did you run from the root of the build tree?"
36 | exit 1
37 | fi
38 | cp -r "bazel-bin/tensorflow_ranking/tools/pip_package/build_pip_package.runfiles/org_tensorflow_ranking/tensorflow_ranking" "$TMPDIR"
39 | cp tensorflow_ranking/tools/pip_package/setup.py "$TMPDIR"
40 |
41 | # Make sure init files exist.
42 | touch "${TMPDIR}/tensorflow_ranking/__init__.py"
43 | touch "${TMPDIR}/tensorflow_ranking/extension/__init__.py"
44 | touch "${TMPDIR}/tensorflow_ranking/python/__init__.py"
45 |
46 | pushd ${TMPDIR} > /dev/null
47 | echo $(date) : "=== Building wheel"
48 | "${PYTHON_BIN_PATH:-python}" setup.py bdist_wheel --universal
49 | mkdir -p ${DEST}
50 | cp dist/* ${DEST}
51 | popd > /dev/null
52 | echo $(date) : "=== Output wheel file is in: ${DEST}"
53 | rm -rf "${TMPDIR}"
54 | }
55 |
56 | function main() {
57 | PKG_NAME_FLAG="tensorflow_ranking"
58 |
59 | DSTDIR="$(real_path $1)"
60 | SRCDIR="$(mktemp -d -t tmp.XXXXXXXXXX)"
61 | if [[ -z "$DSTDIR" ]]; then
62 | echo "No destination dir provided"
63 | exit 1
64 | fi
65 |
66 | build_wheel "$SRCDIR" "$DSTDIR" "$PKG_NAME_FLAG"
67 | }
68 |
69 | main "$@"
70 |
--------------------------------------------------------------------------------
/tensorflow_ranking/tools/pip_package/setup.py:
--------------------------------------------------------------------------------
1 | # Copyright 2024 The TensorFlow Ranking Authors.
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 |
15 | """Pip package setup file for TensorFlow Ranking.
16 |
17 | TensorFlow Ranking is a framework to define learning-to-rank models.
18 | """
19 |
20 | from __future__ import absolute_import
21 | from __future__ import division
22 | from __future__ import print_function
23 |
24 | import setuptools
25 |
26 | DOCLINES = __doc__.split('\n')
27 |
28 | # This version string is semver compatible, but incompatible with pip.
29 | # For pip, we will remove all '-' characters from this string, and use the
30 | # result for pip.
31 | _VERSION = '0.5.5'
32 |
33 | REQUIRED_PACKAGES = [
34 | 'absl-py >= 0.1.6',
35 | 'numpy >= 1.23.2',
36 | 'six >= 1.10.0',
37 | 'tensorflow-serving-api >= 2.0.0, < 3.0.0',
38 | 'tensorflow < 2.16.0',
39 | ]
40 |
41 | project_name = 'tensorflow_ranking'
42 |
43 | setuptools.setup(
44 | name='tensorflow_ranking',
45 | version=_VERSION.replace('-', ''),
46 | description=DOCLINES[0],
47 | long_description='\n'.join(DOCLINES[2:]),
48 | url='https://github.com/tensorflow/ranking',
49 | author='Google Inc.',
50 | author_email='packages@tensorflow.org',
51 | packages=setuptools.find_packages(),
52 | install_requires=REQUIRED_PACKAGES,
53 | # PyPI package information.
54 | classifiers=[
55 | 'Development Status :: 5 - Production/Stable',
56 | 'Intended Audience :: Developers',
57 | 'Intended Audience :: Education',
58 | 'Intended Audience :: Science/Research',
59 | 'License :: OSI Approved :: Apache Software License',
60 | 'Programming Language :: Python :: 2',
61 | 'Programming Language :: Python :: 2.7',
62 | 'Programming Language :: Python :: 3',
63 | 'Programming Language :: Python :: 3.4',
64 | 'Programming Language :: Python :: 3.5',
65 | 'Programming Language :: Python :: 3.6',
66 | 'Topic :: Scientific/Engineering',
67 | 'Topic :: Scientific/Engineering :: Mathematics',
68 | 'Topic :: Scientific/Engineering :: Artificial Intelligence',
69 | 'Topic :: Software Development',
70 | 'Topic :: Software Development :: Libraries',
71 | 'Topic :: Software Development :: Libraries :: Python Modules',
72 | ],
73 | license='Apache 2.0',
74 | keywords='tensorflow ranking learning-to-rank',
75 | )
76 |
--------------------------------------------------------------------------------