-
Notifications
You must be signed in to change notification settings - Fork 474
/
utils.py
110 lines (81 loc) · 3.28 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Copyright 2021 The TensorFlow Ranking Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""Utils for tfr.keras."""
import tensorflow.compat.v2 as tf
# The following functions are used to transform labels and ranks for losses and
# metrics computation. User customized functions can be defined similarly by
# following the same annotations.
@tf.keras.utils.register_keras_serializable(package="tensorflow_ranking")
def identity(label):
"""Identity function that returns the input label.
Args:
label: A `Tensor` or anything that can be converted to a tensor using
`tf.convert_to_tensor`.
Returns:
The input label.
"""
return label
@tf.keras.utils.register_keras_serializable(package="tensorflow_ranking")
def inverse(rank):
"""Computes the inverse of input rank.
Args:
rank: A `Tensor` or anything that can be converted to a tensor using
`tf.convert_to_tensor`.
Returns:
A `Tensor` that has each input element transformed as `x` to `1/x`.
"""
return tf.math.divide_no_nan(1., rank)
@tf.keras.utils.register_keras_serializable(package="tensorflow_ranking")
def pow_minus_1(label):
"""Computes `2**x - 1` element-wise for each label.
Can be used to define `gain_fn` for `tfr.keras.metrics.NDCGMetric`.
Args:
label: A `Tensor` or anything that can be converted to a tensor using
`tf.convert_to_tensor`.
Returns:
A `Tensor` that has each input element transformed as `x` to `2**x - 1`.
"""
return tf.math.pow(2., label) - 1.
@tf.keras.utils.register_keras_serializable(package="tensorflow_ranking")
def log2_inverse(rank):
"""Computes `1./log2(1+x)` element-wise for each label.
Can be used to define `rank_discount_fn` for `tfr.keras.metrics.NDCGMetric`.
Args:
rank: A `Tensor` or anything that can be converted to a tensor using
`tf.convert_to_tensor`.
Returns:
A `Tensor` that has each input element transformed as `x` to `1./log2(1+x)`.
"""
return tf.math.divide_no_nan(tf.math.log(2.), tf.math.log1p(rank))
@tf.keras.utils.register_keras_serializable(package="tensorflow_ranking")
def is_greater_equal_1(label):
"""Computes whether label is greater or equal to 1.
Args:
label: A `Tensor` or anything that can be converted to a tensor using
`tf.convert_to_tensor`.
Returns:
A `Tensor` that has each input element transformed as `x` to `I(x > 1)`.
"""
return tf.greater_equal(label, 1.0)
@tf.keras.utils.register_keras_serializable(package="tensorflow_ranking")
def symmetric_log1p(t):
"""Computes `sign(x) * log(1 + sign(x))`.
Args:
t: A `Tensor` or anything that can be converted to a tensor using
`tf.convert_to_tensor`.
Returns:
A `Tensor` that has each input element transformed as `x` to `I(x > 1)`.
"""
return tf.math.log1p(t * tf.sign(t)) * tf.sign(t)