ตัวชี้วัดหลังการส่งออก

ตามชื่อที่แนะนำ นี่คือเมตริกที่เพิ่มหลังการส่งออก ก่อนการประเมิน

TFMA มาพร้อมกับตัววัดการประเมินที่กำหนดไว้ล่วงหน้าหลายตัว เช่น example_count, auc, comparison_matrix_at_thresholds, precision_recall_at_k, mse, mae และอื่นๆ อีกมากมาย (รายการทั้งหมด อยู่ที่นี่ )

หากคุณไม่พบเมตริกที่มีอยู่ที่เกี่ยวข้องกับกรณีการใช้งานของคุณ หรือต้องการปรับแต่งเมตริก คุณสามารถกำหนดเมตริกที่คุณกำหนดเองได้ อ่านรายละเอียดต่อ!

การเพิ่มเมตริกที่กำหนดเองใน TFMA

การกำหนดเมตริกที่กำหนดเองใน TFMA 1.x

ขยายคลาสฐานนามธรรม

หากต้องการเพิ่มเมตริกที่กำหนดเอง ให้สร้างคลาสใหม่ที่ขยายคลาสนามธรรม _PostExportMetric และกำหนดตัวสร้างและนำเมธอดนามธรรม/ที่ไม่ได้นำไปใช้ไปใช้

กำหนดตัวสร้าง

ใน Constructor ให้ใช้ข้อมูลที่เกี่ยวข้องทั้งหมดเป็นพารามิเตอร์ เช่น label_key, Predict_key, example_weight_key, metric_tag ฯลฯ ที่จำเป็นสำหรับเมตริกที่กำหนดเอง

ประยุกต์ใช้วิธีนามธรรม / ที่ไม่ได้นำไปใช้
  • ตรวจสอบ_ความเข้ากันได้

    ใช้วิธีนี้เพื่อตรวจสอบความเข้ากันได้ของหน่วยเมตริกกับโมเดลที่กำลังประเมิน เช่น ตรวจสอบว่าคุณลักษณะที่จำเป็นทั้งหมด ป้ายกำกับที่คาดหวัง และคีย์การทำนายปรากฏอยู่ในโมเดลในประเภทข้อมูลที่เหมาะสมหรือไม่ ต้องใช้ข้อโต้แย้งสามข้อ:

    • features_dict
    • การคาดการณ์_dict
    • label_dict

    พจนานุกรมเหล่านี้มีการอ้างอิงถึงเทนเซอร์สำหรับโมเดลนี้

  • get_metric_ops

    ใช้วิธีนี้เพื่อจัดเตรียม ops เมตริก (มูลค่าและ ops อัปเดต) เพื่อคำนวณเมตริก เช่นเดียวกับเมธอด check_compatibility มันยังรับอาร์กิวเมนต์สามตัวด้วย:

    • features_dict
    • การคาดการณ์_dict
    • label_dict

    กำหนดตรรกะการคำนวณเมตริกของคุณโดยใช้การอ้างอิงเทนเซอร์เหล่านี้สำหรับโมเดล

  • populate_stats_and_pop และ populate_plots_and_pop

    ใช้ตัววัดนี้เพื่อแปลงผลลัพธ์ตัววัดดิบเป็นรูปแบบ MetricValue และ PlotData proto สิ่งนี้ใช้เวลาสามข้อโต้แย้ง:

    • Slice_key: ชื่อของเมตริก Slice เป็นของ
    • integrated_metrics: พจนานุกรมที่มีผลลัพธ์ดิบ
    • output_metrics: พจนานุกรมเอาต์พุตที่มีเมตริกในรูปแบบโปรโตที่ต้องการ
@_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

การใช้งาน

# 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)