Số liệu sau xuất

Như tên cho thấy, đây là số liệu được thêm sau khi xuất, trước khi đánh giá.

TFMA được đóng gói với một số số liệu đánh giá được xác định trước, như example_count, auc, nhầm lẫn_matrix_at_thresholds, Precision_recall_at_k, mse, mae, v.v. (Danh sách đầy đủ ở đây .)

Nếu bạn không tìm thấy số liệu hiện có liên quan đến trường hợp sử dụng của mình hoặc muốn tùy chỉnh số liệu, bạn có thể xác định số liệu tùy chỉnh của riêng mình. Hãy đọc để biết thêm chi tiết!

Thêm số liệu tùy chỉnh trong TFMA

Xác định số liệu tùy chỉnh trong TFMA 1.x

Mở rộng lớp cơ sở trừu tượng

Để thêm số liệu tùy chỉnh, hãy tạo một lớp mới mở rộng lớp trừu tượng _PostExportMetric và xác định hàm tạo của nó cũng như triển khai các phương thức trừu tượng/chưa triển khai.

Xác định hàm tạo

Trong hàm tạo, lấy tất cả thông tin liên quan như label_key, dự đoán_key, example_weight_key,metric_tag, v.v. làm tham số cho chỉ số tùy chỉnh.

Thực hiện các phương pháp trừu tượng/chưa thực hiện
  • check_compatibility

    Triển khai phương pháp này để kiểm tra tính tương thích của số liệu với mô hình đang được đánh giá, tức là kiểm tra xem tất cả các tính năng bắt buộc, nhãn dự kiến ​​và khóa dự đoán có trong mô hình ở loại dữ liệu thích hợp hay không. Phải mất ba đối số:

    • tính năng_dict
    • dự đoán_dict
    • nhãn_dict

    Những từ điển này chứa các tham chiếu đến Tensors cho mô hình.

  • get_metric_ops

    Triển khai phương pháp này để cung cấp các hoạt động số liệu (các hoạt động giá trị và cập nhật) nhằm tính toán số liệu. Tương tự như phương thức check_compatibility, nó cũng có ba đối số:

    • tính năng_dict
    • dự đoán_dict
    • nhãn_dict

    Xác định logic tính toán số liệu của bạn bằng cách sử dụng các tham chiếu này đến Tensors cho mô hình.

  • populate_stats_and_poppopulate_plots_and_pop

    Triển khai số liệu này để chuyển đổi kết quả số liệu thô sang định dạng nguyên mẫu MetricValuePlotData . Điều này có ba đối số:

    • slice_key: Tên của slice slice thuộc về.
    • kết hợp_metrics: Từ điển chứa kết quả thô.
    • out_metrics: Từ điển đầu ra chứa số liệu ở định dạng nguyên mẫu mong muốn.
@_export('my_metric')
class _MyMetric(_PostExportMetric):
   def __init__(self,
                target_prediction_keys: Optional[List[Text]] = None,
                labels_key: Optional[Text] = None,
                metric_tag: Optional[Text] = None):
      self._target_prediction_keys = target_prediction_keys
      self._label_keys = label_keys
      self._metric_tag = metric_tag
      self._metric_key = 'my_metric_key'

   def check_compatibility(self, features_dict:types.TensorTypeMaybeDict,
                           predictions_dict: types.TensorTypeMaybeDict,
                           labels_dict: types.TensorTypeMaybeDict) -> None:
       # Add compatibility check needed for the metric here.

   def get_metric_ops(self, features_dict: types.TensorTypeMaybeDict,
                      predictions_dict: types.TensorTypeMaybeDict,
                      labels_dict: types.TensorTypeMaybeDict
                     ) -> Dict[bytes, Tuple[types.TensorType,
                     types.TensorType]]:
        # Metric computation logic here.
        # Define value and update ops.
        value_op = compute_metric_value(...)
        update_op = create_update_op(... )
        return {self._metric_key: (value_op, update_op)}

   def populate_stats_and_pop(
       self, slice_key: slicer.SliceKeyType, combined_metrics: Dict[Text, Any],
       output_metrics: Dict[Text, metrics_pb2.MetricValue]) -> None:
       # Parses the metric and converts it into required metric format.
       metric_result = combined_metrics[self._metric_key]
       output_metrics[self._metric_key].double_value.value = metric_result

Cách sử dụng

# Custom metric callback
custom_metric_callback = my_metric(
    labels_key='label',
    target_prediction_keys=['prediction'])

fairness_indicators_callback =
   post_export_metrics.fairness_indicators(
        thresholds=[0.1, 0.3, 0.5, 0.7, 0.9], labels_key=label)

add_metrics_callbacks = [custom_metric_callback,
   fairness_indicators_callback]

eval_shared_model = tfma.default_eval_shared_model(
    eval_saved_model_path=eval_saved_model_path,
    add_metrics_callbacks=add_metrics_callbacks)

eval_config = tfma.EvalConfig(...)

# Run evaluation
tfma.run_model_analysis(
    eval_config=eval_config, eval_shared_model=eval_shared_model)