tfm.uplift.metrics.treatment_sliced_metric.TreatmentSlicedMetric

Computes (weighted) sliced metric values based on treatment group.

Inherits From: SlicedMetric

Two copies are made of the given metric to compute the same metric for the control and treatment groups. The metric name is used for the output results, with a "/control" and "/treatment" suffix for the control and treatment groups. If the given metric outputs a dictionary of tensors the result will be a flattened dictionary with all the sliced metric results.

Example usage:

sliced_metric = TreatmentSlicedMetric(
    metric=tf_keras.metrics.Mean(name="example/mean")
)
sliced_metric.update_state(
    values=tf.constant([[0], [1], [5]]),
    is_treatment=tf.constant([[1], [0], [1]]),
)
sliced_metric.result()
{
    "example/mean": 2.0,
    "example/mean/control": 1.0,
    "example/mean/treatment": 2.5
}

metric A keras.metrics.Metric instance with implemented get_config and from_config methods. Its update_state method is expected have a update_state(self, values, sample_weight=None) signature. The control and treatment metrics will have the same name as the given metric, with "/control" and "/treatment" suffixes.

Methods

call

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:

  • inputs must be explicitly passed. A layer cannot have zero arguments, and inputs cannot be provided via the default value of a keyword argument.
  • NumPy array or Python scalar values in inputs get cast as tensors.
  • TF-Keras mask metadata is only collected from inputs.
  • Layers are built (build(input_shape) method) using shape info from inputs only.
  • input_spec compatibility is only checked against inputs.
  • Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
  • The SavedModel input specification is generated using inputs only.
  • Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
*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

    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

    View source

    Resets all of the metric state variables.

    This function is called between epochs/steps, when a metric is evaluated during training.

    result

    View source

    Aggregates all the metrics' results into a flattened dictionary.

    update_state

    View source

    Computes aggregate, control and treatment metric updates.

    Args
    values A tf.Tensor instance of shape (D0, ..., DN) passed to the update_state method of the treatment, control, and overall metrics.
    is_treatment a tf.Tensor of shape (D0,) or (D0, 1) castable to boolean indicating if the example belongs to the treatment group (True) or control group (False).
    sample_weight optional tf.Tensor for the sample weight. If given it will also be sliced by the is_treatment tensor.
    **kwargs Keyword arguments that will be passed to the update_state method of each metric.