View source on GitHub
|
Computes the pseudo R-squared metric for poisson regression.
tfm.uplift.metrics.poisson_metrics.PseudoRSquared(
from_logits: bool = True,
slice_by_treatment: bool = True,
name: str = 'pseudo_r_squared',
dtype: tf.DType = tf.float32
)
The pseudo R-squared is computed from log likelihoods of three models:
1) LLbaseline: log likelihood of a mean baseline predictor. 2) LLfit: log likelihood of the fitted model. 3) LLmax: maximum achievable log likelihood, which occurs when the predictions equal to the labels.
The equation that computes the pseudo R-squared is:
>>> R_squared = (LLfit - LLbaseline) / (LLmax - LLbaseline)
Args | |
|---|---|
from_logits
|
When y_pred is of type tf.Tensor, specifies whether
y_pred represents the model's logits or predictions. Otherwise, when
y_pred is of type TwoTowerTrainingOutputs, set this to True in
order to compute the loss using the true logits.
|
slice_by_treatment
|
Specifies whether the loss should be sliced by the
treatment indicator tensor. If True, the metric's result will return
the loss values sliced by the treatment group. Note that this can only
be set to True when y_pred is of type TwoTowerTrainingOutputs.
|
name
|
Optional name for the instance. |
dtype
|
Optional data type for the instance. |
Methods
call
call(
inputs, *args, **kwargs
)
This is where the layer's logic lives.
The call() method may not create state (except in its first
invocation, wrapping the creation of variables or other resources in
tf.init_scope()). It is recommended to create state, including
tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
| Args | |
|---|---|
inputs
|
Input tensor, or dict/list/tuple of input tensors.
The first positional inputs argument is subject to special rules:
|
*args
|
Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above. |
**kwargs
|
Additional keyword arguments. May contain tensors, although
this is not recommended, for the reasons above.
The following optional keyword arguments are reserved:
training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.mask: Boolean input mask. If the layer's call() method takes a
mask argument, its default value will be set to the mask
generated for inputs by the previous layer (if input did come
from a layer that generated a corresponding mask, i.e. if it came
from a TF-Keras layer with masking support).
|
| Returns | |
|---|---|
| A tensor or list/tuple of tensors. |
merge_state
merge_state(
metrics
)
Merges the state from one or more metrics.
This method can be used by distributed systems to merge the state computed by different metric instances. Typically the state will be stored in the form of the metric's weights. For example, a tf.keras.metrics.Mean metric contains a list of two weight values: a total and a count. If there were two instances of a tf.keras.metrics.Accuracy that each independently aggregated partial state for an overall accuracy calculation, these two metric's states could be combined as follows:
m1 = tf.keras.metrics.Accuracy()_ = m1.update_state([[1], [2]], [[0], [2]])
m2 = tf.keras.metrics.Accuracy()_ = m2.update_state([[3], [4]], [[3], [4]])
m2.merge_state([m1])m2.result().numpy()0.75
| Args | |
|---|---|
metrics
|
an iterable of metrics. The metrics must have compatible state. |
| Raises | |
|---|---|
ValueError
|
If the provided iterable does not contain metrics matching the metric's required specifications. |
reset_state
reset_state()
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
result
result() -> (tf.Tensor | dict[str, tf.Tensor])
Computes and returns the scalar metric value tensor or a dict of scalars.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
| Returns | |
|---|---|
| A scalar tensor, or a dictionary of scalar tensors. |
update_state
update_state(
y_true: tf.Tensor,
y_pred: (types.TwoTowerTrainingOutputs | tf.Tensor),
sample_weight: (tf.Tensor | None) = None
)
Accumulates statistics for the metric.
| Args | |
|---|---|
*args
|
|
**kwargs
|
A mini-batch of inputs to the Metric. |
View source on GitHub