tf-models-nightly 2.17.0.dev20240413__py2.py3-none-any.whl → 2.17.0.dev20240414__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- official/recommendation/uplift/metrics/poisson_metrics.py +123 -0
- official/recommendation/uplift/metrics/poisson_metrics_test.py +181 -0
- {tf_models_nightly-2.17.0.dev20240413.dist-info → tf_models_nightly-2.17.0.dev20240414.dist-info}/METADATA +1 -1
- {tf_models_nightly-2.17.0.dev20240413.dist-info → tf_models_nightly-2.17.0.dev20240414.dist-info}/RECORD +8 -6
- {tf_models_nightly-2.17.0.dev20240413.dist-info → tf_models_nightly-2.17.0.dev20240414.dist-info}/AUTHORS +0 -0
- {tf_models_nightly-2.17.0.dev20240413.dist-info → tf_models_nightly-2.17.0.dev20240414.dist-info}/LICENSE +0 -0
- {tf_models_nightly-2.17.0.dev20240413.dist-info → tf_models_nightly-2.17.0.dev20240414.dist-info}/WHEEL +0 -0
- {tf_models_nightly-2.17.0.dev20240413.dist-info → tf_models_nightly-2.17.0.dev20240414.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,123 @@
|
|
1
|
+
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
|
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
|
+
"""Poisson regression metrics."""
|
16
|
+
|
17
|
+
from __future__ import annotations
|
18
|
+
|
19
|
+
from typing import Any
|
20
|
+
|
21
|
+
import tensorflow as tf, tf_keras
|
22
|
+
|
23
|
+
from official.recommendation.uplift import types
|
24
|
+
from official.recommendation.uplift.metrics import loss_metric
|
25
|
+
|
26
|
+
|
27
|
+
@tf_keras.utils.register_keras_serializable(package="Uplift")
|
28
|
+
class LogLoss(loss_metric.LossMetric):
|
29
|
+
"""Computes the (weighted) poisson log loss sliced by treatment group.
|
30
|
+
|
31
|
+
Given labels `y` and the model's predictions `x`, the loss is computed as:
|
32
|
+
`loss = x - y * log(x) + [y * log(y) - y + 0.5 * log(2 * pi * y)]`
|
33
|
+
|
34
|
+
Note that from a numerical perspective it is preferred to compute the loss
|
35
|
+
from the model's logits as opposed to directly using its predictions, where
|
36
|
+
`logits = log(x) = log(prediction)`. In this case the loss is computed as:
|
37
|
+
`loss = exp(logits) - y * logits + [y * log(y) - y + 0.5 * log(2 * pi * y)]`
|
38
|
+
|
39
|
+
Example standalone usage:
|
40
|
+
|
41
|
+
>>> poisson_loss = poisson_metrics.LogLoss()
|
42
|
+
>>> y_true = tf.constant([[1.0], [0.0]])
|
43
|
+
>>> y_pred = types.TwoTowerTrainingOutputs(
|
44
|
+
true_logits=tf.constant([[1.0], [0.0]]),
|
45
|
+
is_treatment=tf.constant([[1], [0]]),
|
46
|
+
)
|
47
|
+
>>> poisson_loss(y_true=y_true, y_pred=y_pred)
|
48
|
+
{
|
49
|
+
"poisson_log_loss/treatment": 1.7182817 # exp(1) - 1 * 1
|
50
|
+
"poisson_log_loss/control": 1.0 # exp(0) - 0 * 0
|
51
|
+
"poisson_log_loss": 1.3591409 # (1.7182817 + 1.0) / 2
|
52
|
+
}
|
53
|
+
|
54
|
+
Example usage with the `model.compile()` API:
|
55
|
+
|
56
|
+
>>> model.compile(
|
57
|
+
... optimizer="sgd",
|
58
|
+
... loss=TrueLogitsLoss(tf.nn.log_poisson_loss),
|
59
|
+
... metrics=[poisson_metrics.LogLoss()]
|
60
|
+
... )
|
61
|
+
"""
|
62
|
+
|
63
|
+
def __init__(
|
64
|
+
self,
|
65
|
+
from_logits: bool = True,
|
66
|
+
compute_full_loss: bool = False,
|
67
|
+
slice_by_treatment: bool = True,
|
68
|
+
name: str = "poisson_log_loss",
|
69
|
+
dtype: tf.DType = tf.float32,
|
70
|
+
):
|
71
|
+
"""Initializes the instance.
|
72
|
+
|
73
|
+
Args:
|
74
|
+
from_logits: When `y_pred` is of type `tf.Tensor`, specifies whether
|
75
|
+
`y_pred` represents the model's logits or predictions. Otherwise, when
|
76
|
+
`y_pred` is of type `TwoTowerTrainingOutputs`, set this to `True` in
|
77
|
+
order to compute the loss using the true logits.
|
78
|
+
compute_full_loss: Specifies whether the full log loss will be computed.
|
79
|
+
If `True`, the expression `[y_true * log(y_true) - y_true + 0.5 * log(2
|
80
|
+
* pi * y_true)]` will be added to the loss, otherwise the loss will be
|
81
|
+
computed solely by the expression `[y_pred - y_true * log(y_pred)]`.
|
82
|
+
slice_by_treatment: Specifies whether the loss should be sliced by the
|
83
|
+
treatment indicator tensor. If `True`, the metric's result will return
|
84
|
+
the loss values sliced by the treatment group. Note that this can only
|
85
|
+
be set to `True` when `y_pred` is of type `TwoTowerTrainingOutputs`.
|
86
|
+
name: Optional name for the instance.
|
87
|
+
dtype: Optional data type for the instance.
|
88
|
+
"""
|
89
|
+
super().__init__(
|
90
|
+
loss_fn=tf.nn.log_poisson_loss,
|
91
|
+
from_logits=from_logits,
|
92
|
+
compute_full_loss=compute_full_loss,
|
93
|
+
slice_by_treatment=slice_by_treatment,
|
94
|
+
name=name,
|
95
|
+
dtype=dtype,
|
96
|
+
)
|
97
|
+
|
98
|
+
def update_state(
|
99
|
+
self,
|
100
|
+
y_true: tf.Tensor,
|
101
|
+
y_pred: types.TwoTowerTrainingOutputs | tf.Tensor,
|
102
|
+
sample_weight: tf.Tensor | None = None,
|
103
|
+
):
|
104
|
+
if not self._from_logits:
|
105
|
+
if isinstance(y_pred, types.TwoTowerTrainingOutputs):
|
106
|
+
raise ValueError(
|
107
|
+
"`from_logits` must be set to `True` when `y_pred` is of type"
|
108
|
+
" TwoTowerTrainingOutputs. Note that the true logits and true"
|
109
|
+
" predictions are assumed to be linked to each other through the"
|
110
|
+
" log link function: `true_logits = tf.math.log(true_predictions)."
|
111
|
+
)
|
112
|
+
y_pred = tf.math.log(y_pred)
|
113
|
+
|
114
|
+
super().update_state(y_true, y_pred, sample_weight)
|
115
|
+
|
116
|
+
def get_config(self) -> dict[str, Any]:
|
117
|
+
config = super().get_config()
|
118
|
+
del config["loss_fn"]
|
119
|
+
return config
|
120
|
+
|
121
|
+
@classmethod
|
122
|
+
def from_config(cls, config: dict[str, Any]) -> LogLoss:
|
123
|
+
return cls(**config)
|
@@ -0,0 +1,181 @@
|
|
1
|
+
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
|
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 poisson regression metrics."""
|
16
|
+
|
17
|
+
from absl.testing import parameterized
|
18
|
+
import tensorflow as tf, tf_keras
|
19
|
+
|
20
|
+
from official.recommendation.uplift import keras_test_case
|
21
|
+
from official.recommendation.uplift import types
|
22
|
+
from official.recommendation.uplift.metrics import poisson_metrics
|
23
|
+
|
24
|
+
|
25
|
+
def _get_two_tower_outputs(
|
26
|
+
true_logits: tf.Tensor, is_treatment: tf.Tensor
|
27
|
+
) -> types.TwoTowerTrainingOutputs:
|
28
|
+
# Only the true_logits and is_treatment tensors are needed for testing.
|
29
|
+
return types.TwoTowerTrainingOutputs(
|
30
|
+
shared_embedding=tf.ones_like(is_treatment),
|
31
|
+
control_predictions=tf.ones_like(is_treatment),
|
32
|
+
treatment_predictions=tf.ones_like(is_treatment),
|
33
|
+
uplift=tf.ones_like(is_treatment),
|
34
|
+
control_logits=tf.ones_like(is_treatment),
|
35
|
+
treatment_logits=tf.ones_like(is_treatment),
|
36
|
+
true_logits=true_logits,
|
37
|
+
true_predictions=tf.ones_like(is_treatment),
|
38
|
+
is_treatment=is_treatment,
|
39
|
+
)
|
40
|
+
|
41
|
+
|
42
|
+
class LogLossTest(keras_test_case.KerasTestCase, parameterized.TestCase):
|
43
|
+
|
44
|
+
@parameterized.named_parameters(
|
45
|
+
{
|
46
|
+
"testcase_name": "two_tower_outputs_not_sliced",
|
47
|
+
"from_logits": True,
|
48
|
+
"compute_full_loss": False,
|
49
|
+
"slice_by_treatment": False,
|
50
|
+
"y_true": tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
51
|
+
"y_pred": _get_two_tower_outputs(
|
52
|
+
true_logits=tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
53
|
+
is_treatment=tf.constant([[1], [0], [1], [0]]),
|
54
|
+
),
|
55
|
+
"expected_loss": tf.reduce_mean(
|
56
|
+
tf.nn.log_poisson_loss(
|
57
|
+
tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
58
|
+
tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
59
|
+
)
|
60
|
+
),
|
61
|
+
},
|
62
|
+
{
|
63
|
+
"testcase_name": "two_tower_outputs_sliced",
|
64
|
+
"from_logits": True,
|
65
|
+
"compute_full_loss": False,
|
66
|
+
"slice_by_treatment": True,
|
67
|
+
"y_true": tf.constant([[1], [0]], dtype=tf.float32),
|
68
|
+
"y_pred": _get_two_tower_outputs(
|
69
|
+
true_logits=tf.constant([[1], [0]], dtype=tf.float32),
|
70
|
+
is_treatment=tf.constant([[1], [0]]),
|
71
|
+
),
|
72
|
+
"expected_loss": {
|
73
|
+
"poisson_log_loss/treatment": (
|
74
|
+
tf.math.exp(1.0) - 1 # exp(1) - 1 * 1
|
75
|
+
),
|
76
|
+
"poisson_log_loss/control": 1.0, # exp(0) - 0 * 0
|
77
|
+
"poisson_log_loss": ((tf.math.exp(1.0) - 1) + 1) / 2,
|
78
|
+
},
|
79
|
+
},
|
80
|
+
{
|
81
|
+
"testcase_name": "tensor_outputs_from_logits",
|
82
|
+
"from_logits": True,
|
83
|
+
"compute_full_loss": False,
|
84
|
+
"slice_by_treatment": False,
|
85
|
+
"y_true": tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
86
|
+
"y_pred": tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
87
|
+
"expected_loss": tf.reduce_mean(
|
88
|
+
tf.nn.log_poisson_loss(
|
89
|
+
tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
90
|
+
tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
91
|
+
)
|
92
|
+
),
|
93
|
+
},
|
94
|
+
{
|
95
|
+
"testcase_name": "tensor_outputs_from_logits_full_loss",
|
96
|
+
"from_logits": True,
|
97
|
+
"compute_full_loss": True,
|
98
|
+
"slice_by_treatment": False,
|
99
|
+
"y_true": tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
100
|
+
"y_pred": tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
101
|
+
"expected_loss": tf.reduce_mean(
|
102
|
+
tf.nn.log_poisson_loss(
|
103
|
+
tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
104
|
+
tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
105
|
+
compute_full_loss=True,
|
106
|
+
)
|
107
|
+
),
|
108
|
+
},
|
109
|
+
{
|
110
|
+
"testcase_name": "tensor_outputs_from_predictions",
|
111
|
+
"from_logits": False,
|
112
|
+
"compute_full_loss": False,
|
113
|
+
"slice_by_treatment": False,
|
114
|
+
"y_true": tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
115
|
+
"y_pred": tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
116
|
+
"expected_loss": tf.reduce_mean(
|
117
|
+
tf.nn.log_poisson_loss(
|
118
|
+
tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
119
|
+
tf.math.log(
|
120
|
+
tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
|
121
|
+
),
|
122
|
+
)
|
123
|
+
),
|
124
|
+
},
|
125
|
+
{
|
126
|
+
"testcase_name": "tensor_outputs_from_predictions_full_loss",
|
127
|
+
"from_logits": False,
|
128
|
+
"compute_full_loss": True,
|
129
|
+
"slice_by_treatment": False,
|
130
|
+
"y_true": tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
131
|
+
"y_pred": tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
132
|
+
"expected_loss": tf.reduce_mean(
|
133
|
+
tf.nn.log_poisson_loss(
|
134
|
+
tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
135
|
+
tf.math.log(
|
136
|
+
tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
|
137
|
+
),
|
138
|
+
compute_full_loss=True,
|
139
|
+
)
|
140
|
+
),
|
141
|
+
},
|
142
|
+
)
|
143
|
+
def test_metric_computes_correct_loss(
|
144
|
+
self,
|
145
|
+
from_logits: bool,
|
146
|
+
compute_full_loss: bool,
|
147
|
+
slice_by_treatment: bool,
|
148
|
+
y_true: tf.Tensor,
|
149
|
+
y_pred: tf.Tensor,
|
150
|
+
expected_loss: tf.Tensor,
|
151
|
+
):
|
152
|
+
metric = poisson_metrics.LogLoss(
|
153
|
+
from_logits=from_logits,
|
154
|
+
compute_full_loss=compute_full_loss,
|
155
|
+
slice_by_treatment=slice_by_treatment,
|
156
|
+
)
|
157
|
+
metric.update_state(y_true, y_pred)
|
158
|
+
self.assertAllClose(expected_loss, metric.result())
|
159
|
+
|
160
|
+
@parameterized.product(
|
161
|
+
from_logits=(True, False),
|
162
|
+
compute_full_loss=(True, False),
|
163
|
+
)
|
164
|
+
def test_metric_is_configurable(
|
165
|
+
self, from_logits: bool, compute_full_loss: bool
|
166
|
+
):
|
167
|
+
metric = poisson_metrics.LogLoss(
|
168
|
+
from_logits=from_logits,
|
169
|
+
compute_full_loss=compute_full_loss,
|
170
|
+
slice_by_treatment=False,
|
171
|
+
)
|
172
|
+
self.assertLayerConfigurable(
|
173
|
+
layer=metric,
|
174
|
+
y_true=tf.constant([[0], [0], [2], [7]], dtype=tf.float32),
|
175
|
+
y_pred=tf.constant([[1], [2], [3], [4]], dtype=tf.float32),
|
176
|
+
serializable=True,
|
177
|
+
)
|
178
|
+
|
179
|
+
|
180
|
+
if __name__ == "__main__":
|
181
|
+
tf.test.main()
|
@@ -915,6 +915,8 @@ official/recommendation/uplift/metrics/label_variance_test.py,sha256=k0mdEU1WU53
|
|
915
915
|
official/recommendation/uplift/metrics/loss_metric.py,sha256=gYZdnTsuL_2q1FZuPip-DaWxt_Q-02YYaePyMBVNx7w,7344
|
916
916
|
official/recommendation/uplift/metrics/loss_metric_test.py,sha256=48rQG8bKFdy0xBFjoOLXKRUlYpCEyAzSmPOFoF7FX94,16021
|
917
917
|
official/recommendation/uplift/metrics/metric_configs.py,sha256=Z-r79orE4EycQ5TJ7xdI5LhjOHT3wzChYyDxcxGqLXk,1670
|
918
|
+
official/recommendation/uplift/metrics/poisson_metrics.py,sha256=LJnovpST0H9kFGu-ziDstWOVlAYARLo9oPLDTjzrdu4,4623
|
919
|
+
official/recommendation/uplift/metrics/poisson_metrics_test.py,sha256=Kd8CuQeEBlxRklA-7mGKHcUD0CyskE1S3cJqk6mEvv4,6756
|
918
920
|
official/recommendation/uplift/metrics/sliced_metric.py,sha256=uhvzudOWtMNKZ0avwGhX-37UELR9Cq9b4C0g8erBkXw,8688
|
919
921
|
official/recommendation/uplift/metrics/sliced_metric_test.py,sha256=bhVGyI1tOkFkVOtruJo3p6XopDFyG1JW5qdZm9-RqeU,12248
|
920
922
|
official/recommendation/uplift/metrics/treatment_fraction.py,sha256=WHrKfsN42xU7S-pK99xEVpVtd3zLD7UidLT1K8vgIn4,2757
|
@@ -1204,9 +1206,9 @@ tensorflow_models/tensorflow_models_test.py,sha256=nc6A9K53OGqF25xN5St8EiWvdVbda
|
|
1204
1206
|
tensorflow_models/nlp/__init__.py,sha256=4tA5Pf4qaFwT-fIFOpX7x7FHJpnyJT-5UgOeFYTyMlc,807
|
1205
1207
|
tensorflow_models/uplift/__init__.py,sha256=mqfa55gweOdpKoaQyid4A_4u7xw__FcQeSIF0k_pYmI,999
|
1206
1208
|
tensorflow_models/vision/__init__.py,sha256=zBorY_v5xva1uI-qxhZO3Qh-Dii-Suq6wEYh6hKHDfc,833
|
1207
|
-
tf_models_nightly-2.17.0.
|
1208
|
-
tf_models_nightly-2.17.0.
|
1209
|
-
tf_models_nightly-2.17.0.
|
1210
|
-
tf_models_nightly-2.17.0.
|
1211
|
-
tf_models_nightly-2.17.0.
|
1212
|
-
tf_models_nightly-2.17.0.
|
1209
|
+
tf_models_nightly-2.17.0.dev20240414.dist-info/AUTHORS,sha256=1dG3fXVu9jlo7bul8xuix5F5vOnczMk7_yWn4y70uw0,337
|
1210
|
+
tf_models_nightly-2.17.0.dev20240414.dist-info/LICENSE,sha256=WxeBS_DejPZQabxtfMOM_xn8qoZNJDQjrT7z2wG1I4U,11512
|
1211
|
+
tf_models_nightly-2.17.0.dev20240414.dist-info/METADATA,sha256=SMm31t0MLz8wyx_vZ3yIklTbV1qbniqgblIk9XWmtEY,1432
|
1212
|
+
tf_models_nightly-2.17.0.dev20240414.dist-info/WHEEL,sha256=kGT74LWyRUZrL4VgLh6_g12IeVl_9u9ZVhadrgXZUEY,110
|
1213
|
+
tf_models_nightly-2.17.0.dev20240414.dist-info/top_level.txt,sha256=gum2FfO5R4cvjl2-QtP-S1aNmsvIZaFFT6VFzU0f4-g,33
|
1214
|
+
tf_models_nightly-2.17.0.dev20240414.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|