TFMA Types¶
tensorflow_model_analysis.types
¶
Types.
Attributes¶
AddMetricsCallbackType
module-attribute
¶
AddMetricsCallbackType = Callable[
[
TensorTypeMaybeDict,
TensorTypeMaybeDict,
TensorTypeMaybeDict,
],
Dict[str, Tuple[TensorType, TensorType]],
]
ConcreteStructuredMetricValue
module-attribute
¶
ConcreteStructuredMetricValue = TypeVar(
"ConcreteStructuredMetricValue",
bound="StructuredMetricValue",
)
DictOfFetchedTensorValues
module-attribute
¶
DictOfFetchedTensorValues = Dict[
FPLKeyType, Dict[str, TensorValue]
]
DictOfTensorTypeMaybeDict
module-attribute
¶
DictOfTensorTypeMaybeDict = Dict[str, TensorTypeMaybeDict]
DictOfTensorValueMaybeDict
module-attribute
¶
DictOfTensorValueMaybeDict = Dict[str, TensorValueMaybeDict]
FeaturesPredictionsLabels
module-attribute
¶
FeaturesPredictionsLabels = NamedTuple(
"FeaturesPredictionsLabels",
[
("input_ref", int),
("features", DictOfFetchedTensorValues),
("predictions", DictOfFetchedTensorValues),
("labels", DictOfFetchedTensorValues),
],
)
MaterializedColumn
module-attribute
¶
MaterializedColumn = NamedTuple(
"MaterializedColumn",
[
("name", str),
(
"value",
Union[
List[bytes],
List[int],
List[float],
bytes,
int,
float,
],
),
],
)
MaybeMultipleEvalSharedModels
module-attribute
¶
MaybeMultipleEvalSharedModels = Union[
EvalSharedModel,
List[EvalSharedModel],
Dict[str, EvalSharedModel],
]
MetricValueType
module-attribute
¶
MetricValueType = Union[
PrimitiveMetricValueType, ndarray, StructuredMetricValue
]
TensorTypeMaybeMultiLevelDict
module-attribute
¶
TensorTypeMaybeMultiLevelDict = Union[
TensorTypeMaybeDict, DictOfTensorTypeMaybeDict
]
TensorValue
module-attribute
¶
TensorValue = Union[
ndarray,
SparseTensorValue,
RaggedTensorValue,
SparseTensorValue,
]
TensorValueMaybeDict
module-attribute
¶
TensorValueMaybeDict = Union[TensorValue, DictOfTensorValue]
TensorValueMaybeMultiLevelDict
module-attribute
¶
TensorValueMaybeMultiLevelDict = Union[
TensorValueMaybeDict, DictOfTensorValueMaybeDict
]
TypeSpecMaybeMultiLevelDict
module-attribute
¶
TypeSpecMaybeMultiLevelDict = Union[
TypeSpecMaybeDict, DictOfTypeSpecMaybeDict
]
Classes¶
EvalSharedModel
¶
Bases: NamedTuple('EvalSharedModel', [('model_path', str), ('add_metrics_callbacks', List[Callable]), ('include_default_metrics', bool), ('example_weight_key', Union[str, Dict[str, str]]), ('additional_fetches', List[str]), ('model_loader', ModelLoader), ('model_name', str), ('model_type', str), ('rubber_stamp', bool), ('is_baseline', bool), ('resource_hints', Optional[Dict[str, Any]]), ('backend_config', Optional[Any])])
Shared model used during extraction and evaluation.
ATTRIBUTE | DESCRIPTION |
---|---|
model_path |
Path to EvalSavedModel (containing the saved_model.pb file).
|
add_metrics_callbacks |
Optional list of callbacks for adding additional metrics to the graph. The names of the metrics added by the callbacks should not conflict with existing metrics. See below for more details about what each callback should do. The callbacks are only used during evaluation.
|
include_default_metrics |
True to include the default metrics that are part of the saved model graph during evaluation.
|
example_weight_key |
Example weight key (single-output model) or dict of example weight keys (multi-output model) keyed by output_name.
|
additional_fetches |
Prefixes of additional tensors stored in signature_def.inputs that should be fetched at prediction time. The "features" and "labels" tensors are handled automatically and should not be included in this list.
|
model_loader |
Model loader.
|
model_name |
Model name (should align with ModelSpecs.name).
|
model_type |
Model type (tfma.TF_KERAS, tfma.TF_LITE, tfma.TF_ESTIMATOR, ..).
|
rubber_stamp |
True if this model is being rubber stamped. When a model is rubber stamped diff thresholds will be ignored if an associated baseline model is not passed.
|
is_baseline |
The model is the baseline for comparison or not.
|
resource_hints |
The beam resource hints to apply to the PTransform which runs inference for this model.
|
backend_config |
The backend config for running model inference.
|
More details on add_metrics_callbacks:
Each add_metrics_callback should have the following prototype: def add_metrics_callback(features_dict, predictions_dict, labels_dict):
Note that features_dict, predictions_dict and labels_dict are not necessarily dictionaries - they might also be Tensors, depending on what the model's eval_input_receiver_fn returns.
It should create and return a metric_ops dictionary, such that metric_ops['metric_name'] = (value_op, update_op), just as in the Trainer.
Short example:
def add_metrics_callback(features_dict, predictions_dict, labels): metrics_ops = {} metric_ops['mean_label'] = tf.metrics.mean(labels) metric_ops['mean_probability'] = tf.metrics.mean(tf.slice( predictions_dict['probabilities'], [0, 1], [2, 1])) return metric_ops
ModelLoader
¶
Model loader is responsible for loading shared model types.
ATTRIBUTE | DESCRIPTION |
---|---|
construct_fn |
A callable which creates the model instance. The callable should take no args as input (typically a closure is used to capture necessary parameters).
|
tags |
Optional model tags (e.g. 'serve' for serving or 'eval' for EvalSavedModel).
|
Source code in tensorflow_model_analysis/api/types.py
Attributes¶
Functions¶
load
¶
Returns loaded model.
PARAMETER | DESCRIPTION |
---|---|
model_load_time_callback
|
Optional callback to track load time. |
Source code in tensorflow_model_analysis/api/types.py
RaggedTensorValue
¶
Bases: NamedTuple('RaggedTensorValue', [('values', ndarray), ('nested_row_splits', List[ndarray])])
RaggedTensorValue encapsulates a batch of ragged tensor values.
ATTRIBUTE | DESCRIPTION |
---|---|
values |
A np.ndarray of values.
|
nested_row_splits |
A list of np.ndarray values representing the row splits (one per dimension including the batch dimension).
|
SparseTensorValue
¶
Bases: NamedTuple('SparseTensorValue', [('values', ndarray), ('indices', ndarray), ('dense_shape', ndarray)])
SparseTensorValue encapsulates a batch of sparse tensor values.
ATTRIBUTE | DESCRIPTION |
---|---|
values |
A np.ndarray of values.
|
indices |
A np.ndarray of indices.
|
dense_shape |
A np.ndarray representing the dense shape.
|
StructuredMetricValue
¶
Bases: ABC
The base class for all structured metrics used within TFMA.
This class allows custom metrics to control how proto serialization happens, and how to handle basic algebraic operations used in computing confidence intervals and model diffs. By implementing the _apply_binary_op methods, subclasses can then be treated like primitive numeric types.
ValueWithTDistribution
¶
Bases: NamedTuple('ValueWithTDistribution', [('sample_mean', MetricValueType), ('sample_standard_deviation', MetricValueType), ('sample_degrees_of_freedom', int), ('unsampled_value', MetricValueType)])
Represents the t-distribution value.
It includes sample_mean, sample_standard_deviation, sample_degrees_of_freedom. And also unsampled_value is also stored here to record the value calculated without bootstrapping. The sample_standard_deviation is calculated as: \sqrt{ \frac{1}{N-1} \sum_{i=1}^{N}{(x_i - \bar{x})^2} }
VarLenTensorValue
¶
Bases: NamedTuple('VarLenTensorValue', [('values', ndarray), ('indices', ndarray), ('dense_shape', ndarray)])
VarLenTensorValue encapsulates a batch of varlen dense tensor values.
ATTRIBUTE | DESCRIPTION |
---|---|
values |
A np.ndarray of values.
|
indices |
A np.ndarray of indices.
|
dense_shape |
A np.ndarray representing the dense shape of the entire tensor. Note that each row (i.e. set of values sharing the same value for the first / batch dimension) is considered to have its own shape based on the presence of values.
|
Classes¶
DenseRowIterator
¶
An Iterator over rows of a VarLenTensorValue as dense np.arrays.
Because the VarLenTensorValue was created from a set of variable length (dense) arrays, we can invert this process to turn a VarLenTensorValue back into the original dense arrays.
Source code in tensorflow_model_analysis/api/types.py
Functions¶
dense_rows
¶
from_dense_rows
classmethod
¶
from_dense_rows(
dense_rows: Iterable[ndarray],
) -> VarLenTensorValue
Converts a collection of variable length dense arrays into a tensor.
PARAMETER | DESCRIPTION |
---|---|
dense_rows
|
A sequence of possibly variable length 1D arrays.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
VarLenTensorValue
|
A new VarLenTensorValue containing the sparse representation of the |
VarLenTensorValue
|
vertically stacked dense rows. The dense_shape attribute on the result |
VarLenTensorValue
|
will be (num_rows, max_row_len). |