View source on GitHub
|
A metric sliced by integer, boolean, or string features.
tfm.uplift.metrics.sliced_metric.SlicedMetric(
metric: tf_keras.metrics.Metric,
slicing_spec: (dict[str, str] | dict[str, int]),
slicing_feature_dtype: (tf.DType | None) = None,
name: (str | None) = None
)
A metric wrapper that computes a metric for different slices of an arbitrary
feature. The slicing is specified via a slicing spec, which is a dictionary
from a slice name to the unique value to be sliced on. For each pair of
slice_name, slicing_value passed, the suffix /slice_name will be
appended to the name of the result of the corresponding slice.
An overall result is also computed without any slicing applied.
In order for this to work correctly, the given metric must support passing
sample_weight to its update_state() method. Additionally, the slicing
feature must also be passed to update_state() method of this class and
must be of a broadcastable shape to the metric inputs.
This wrapper creates a deep copy of the metric passed to it for each slice.
At every call to update_state(), the wrapper will call the update_state()
method of the metric for each slice with the sample_weights set to zero
where the slicing feature is not equal to the corresponding slicing value.
If the given metric returns a tensor, the result of this metric will be a dictionary mapping from the sliced metric's name to the result for that slice. If the given metric returns a dictionary of tensors, the result of this metric will be a flattened dictionary consisting of each of the sliced metrics' results for every slice.
Example usage:
sliced_metric = SlicedMetric(tf_keras.metrics.Accuracy('accuracy'),slicing_spec={"control": False, "treatment": True},)sliced_metric.update_state(y_true=tf.constant([[0], [1], [0], [1]]),y_pred=tf.constant([[1], [0], [1], [1]]),slicing_feature=tf.constant([[True], [False], [True], [False]]),)sliced_metric.result(){"accuracy": 0.25,"accuracy/control": 0.5,"accuracy/treatment": 0}
Raises | |
|---|---|
A ValueError if slicing_spec is empty, contains duplicate slicing
values, or has slicing values of different types.
|
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() -> dict[str, tf.Tensor]
Aggregates all the metrics' results into a flattened dictionary.
update_state
update_state(
*args,
sample_weight: (tf.Tensor | None) = None,
slicing_feature: tf.Tensor,
**kwargs
)
Updates the state of the metrics for each slice.
| Args | |
|---|---|
*args
|
A variable amount of tf.Tensor instances that will be passed to
the update_state method of each metric.
|
sample_weight
|
An optional tf.Tensor used to weight the sample. Its
dimensions must be broadcastable to the shape(s) of *args.
|
slicing_feature
|
A tf.Tensor consisting of the feature to be sliced on.
Its dimensions must be broadcastable to the shape(s) of *args.
|
**kwargs
|
Keyword arguments that will be passed to the update_state
method of each metric.
|
View source on GitHub