การตรวจสอบความถูกต้องของแบบจำลองการวิเคราะห์โมเดล Tensorflow

ภาพรวม

TFMA รองรับการตรวจสอบความถูกต้องของโมเดลโดยการตั้งค่าเกณฑ์ค่าและเปลี่ยนเกณฑ์ตาม ตัวชี้วัดที่สนับสนุน

การกำหนดค่า

ค่าเกณฑ์ทั่วไป

เกณฑ์ค่ามีประโยชน์ในเกตโมเดลผู้สมัคร โดยการตรวจสอบว่าหน่วยวัดที่เกี่ยวข้องมีขนาดใหญ่กว่าขอบเขตล่าง และ/หรือเล็กกว่าขอบเขตบน ผู้ใช้สามารถตั้งค่าค่า lower_bound และ upper_bound ค่าใดค่าหนึ่งหรือทั้งสองค่าก็ได้ lower_bound มีค่าเริ่มต้นเป็นค่าอนันต์เชิงลบหากไม่ได้ตั้งค่า และค่า upper_bound มีค่าเริ่มต้นเป็นค่าอนันต์หากไม่ได้ตั้งค่า

import tensorflow_model_analysis as tfma

lower_bound = tfma.GenericValueThreshold(lower_bound={'value':0})
upper_bound = tfma.GenericValueThreshold(upper_bound={'value':1})
lower_upper_bound = tfma.GenericValueThreshold(lower_bound={'value':0},
                                               upper_bound={'value':1))

GenericChangeThreshold

เกณฑ์การเปลี่ยนแปลงมีประโยชน์ในเกตโมเดลผู้สมัคร โดยการตรวจสอบว่าหน่วยเมตริกที่เกี่ยวข้องมีขนาดใหญ่กว่า/เล็กกว่าโมเดลพื้นฐานหรือไม่ การเปลี่ยนแปลงสามารถวัดได้สองวิธี: การเปลี่ยนแปลงสัมบูรณ์และการเปลี่ยนแปลงสัมพัทธ์ การเปลี่ยนแปลงแบบสมบูรณ์ได้รับการคำนวณเป็นค่าความแตกต่างระหว่างหน่วยเมตริกของตัวเลือกและแบบจำลองพื้นฐาน กล่าวคือ v_c - v_b โดยที่ v_c หมายถึงค่าเมตริกที่เป็นตัวเลือก และ v_b หมายถึงค่าพื้นฐาน ค่าสัมพัทธ์คือความแตกต่างสัมพัทธ์ระหว่างเมตริกของผู้สมัครและค่าพื้นฐาน กล่าวคือ v_c/v_b เกณฑ์สัมบูรณ์และเกณฑ์สัมพัทธ์สามารถอยู่ร่วมกับโมเดลเกทได้ด้วยเกณฑ์ทั้งสอง นอกจากการตั้งค่าเกณฑ์แล้ว ผู้ใช้ยังต้องกำหนดค่า MetricDirection ด้วย สำหรับหน่วยวัดที่มีค่าสูงกว่าในเกณฑ์ดี (เช่น AUC) ให้กำหนดทิศทางเป็น HIGHER_IS_BETTER สำหรับหน่วยวัดที่มีค่าต่ำกว่าในเกณฑ์ดี (เช่น ขาดทุน) ให้กำหนดทิศทางเป็น LOWER_IS_BETTER เกณฑ์การเปลี่ยนแปลงจำเป็นต้องมีการประเมินแบบจำลองพื้นฐานพร้อมกับแบบจำลองผู้สมัคร ดูตัวอย่าง คู่มือการเริ่มต้นใช้งาน

import tensorflow_model_analysis as tfma

absolute_higher_is_better = tfma.GenericChangeThreshold(absolute={'value':1},
                                                        direction=tfma.MetricDirection.HIGHER_IS_BETTER)
absolute_lower_is_better = tfma.GenericChangeThreshold(absolute={'value':1},
                                                       direction=tfma.MetricDirection.LOWER_IS_BETTER)
relative_higher_is_better = tfma.GenericChangeThreshold(relative={'value':1},
                                                        direction=tfma.MetricDirection.HIGHER_IS_BETTER)
relative_lower_is_better = tfma.GenericChangeThreshold(relative={'value':1},
                                                       direction=tfma.MetricDirection.LOWER_IS_BETTER)
absolute_and_relative = tfma.GenericChangeThreshold(relative={'value':1},
                                                    absolute={'value':0.2},
                                                    direction=tfma.MetricDirection.LOWER_IS_BETTER)

รวบรวมสิ่งต่าง ๆ เข้าด้วยกัน

ตัวอย่างต่อไปนี้จะรวมค่าและเกณฑ์การเปลี่ยนแปลง:

import tensorflow_model_analysis as tfma

lower_bound = tfma.GenericValueThreshold(lower_bound={'value':0.7})
relative_higher_is_better =
    tfma.GenericChangeThreshold(relative={'value':1.01},
                                direction=tfma.MetricDirection.HIGHER_IS_BETTER)
auc_threshold = tfma.MetricThreshold(value_threshold=lower_bound,
                                     change_threshold=relative_higher_is_better)

การเขียนการกำหนดค่าในรูปแบบโปรโตอาจอ่านได้ง่ายกว่า:

from google.protobuf import text_format

auc_threshold = text_format.Parse("""
  value_threshold { lower_bound { value: 0.6 } }
  change_threshold { relative { value: 1.01 } }
""", tfma.MetricThreshold())

สามารถตั้งค่า MetricThreshold ให้กับทั้งตัววัดเวลาการฝึกอบรมของโมเดล (แบบจำลองที่บันทึกไว้ EvalSavedModel หรือ Keras) และตัววัดหลังการฝึกอบรม (กำหนดในการกำหนดค่า TFMA) สำหรับตัววัดเวลาการฝึกอบรม เกณฑ์จะระบุไว้ใน tfma.MetricsSpec:

metrics_spec = tfma.MetricSpec(thresholds={'auc': auc_threshold})

สำหรับตัววัดหลังการฝึกอบรม จะมีการกำหนดเกณฑ์โดยตรงใน tfma.MetricConfig:

metric_config = tfma.MetricConfig(class_name='TotalWeightedExample',
                                  threshold=lower_bound)

นี่คือตัวอย่างพร้อมกับการตั้งค่าอื่นๆ ใน EvalConfig:

# Run in a Jupyter Notebook.
from google.protobuf import text_format

eval_config = text_format.Parse("""
  model_specs {
    # This assumes a serving model with a "serving_default" signature.
    label_key: "label"
    example_weight_key: "weight"
  }
  metrics_spec {
    # Training Time metric thresholds
    thresholds {
      key: "auc"
      value: {
        value_threshold {
          lower_bound { value: 0.7 }
        }
        change_threshold {
          direction: HIGHER_IS_BETTER
          absolute { value: -1e-10 }
        }
      }
    }
    # Post Training metrics and their thesholds.
    metrics {
      # This assumes a binary classification model.
      class_name: "AUC"
      threshold {
        value_threshold {
          lower_bound { value: 0 }
        }
      }
    }
  }
  slicing_specs {}
  slicing_specs {
    feature_keys: ["age"]
  }
""", tfma.EvalConfig())

eval_shared_models = [
  tfma.default_eval_shared_model(
      model_name=tfma.CANDIDATE_KEY,
      eval_saved_model_path='/path/to/saved/candiate/model',
      eval_config=eval_config),
  tfma.default_eval_shared_model(
      model_name=tfma.BASELINE_KEY,
      eval_saved_model_path='/path/to/saved/baseline/model',
      eval_config=eval_config),
]

eval_result = tfma.run_model_analysis(
    eval_shared_models,
    eval_config=eval_config,
    # This assumes your data is a TFRecords file containing records in the
    # tf.train.Example format.
    data_location="/path/to/file/containing/tfrecords",
    output_path="/path/for/output")

tfma.view.render_slicing_metrics(eval_result)
tfma.load_validation_result(output_path)

เอาท์พุต

นอกเหนือจากเอาต์พุตไฟล์เมทริกโดยผู้ประเมินแล้ว เมื่อใช้การตรวจสอบความถูกต้อง ไฟล์ "การตรวจสอบความถูกต้อง" เพิ่มเติมก็จะถูกส่งออกด้วย รูปแบบเพย์โหลดคือ ValidationResult ผลลัพธ์จะมีการตั้งค่า "validation_ok" เป็น True เมื่อไม่มีข้อผิดพลาด เมื่อมีความล้มเหลว ข้อมูลเกี่ยวกับเมทริกที่เกี่ยวข้อง เกณฑ์ และค่าเมทริกที่ถูกสังเกต ต่อไปนี้คือตัวอย่างที่ "weighted_examle_count" ไม่ผ่านเกณฑ์ค่า (1.5 ไม่เล็กกว่า 1.0 จึงถือว่าล้มเหลว):

  validation_ok: False
  metric_validations_per_slice {
    failures {
      metric_key {
        name: "weighted_example_count"
        model_name: "candidate"
      }
      metric_threshold {
        value_threshold {
          upper_bound { value: 1.0 }
        }
      }
      metric_value {
        double_value { value: 1.5 }
      }
    }
  }