คอมโพเนนต์ไปป์ไลน์ TFX ของจูนเนอร์

ส่วนประกอบ Tuner จะปรับแต่งไฮเปอร์พารามิเตอร์สำหรับโมเดล

ส่วนประกอบจูนเนอร์และไลบรารี KerasTuner

คอมโพเนนต์ Tuner ใช้ Python KerasTuner API อย่างกว้างขวางเพื่อปรับแต่งไฮเปอร์พารามิเตอร์

ส่วนประกอบ

จูนเนอร์ใช้เวลา:

  • tf.ตัวอย่างที่ใช้สำหรับการฝึกอบรมและประเมินผล
  • ไฟล์โมดูลที่ผู้ใช้จัดเตรียมไว้ (หรือโมดูล fn) ที่กำหนดตรรกะการปรับแต่ง รวมถึงคำจำกัดความของโมเดล พื้นที่ค้นหาไฮเปอร์พารามิเตอร์ วัตถุประสงค์ ฯลฯ
  • คำจำกัดความ Protobuf ของ train args และ eval args
  • (ไม่บังคับ) คำจำกัดความ Protobuf ของการปรับแต่ง args
  • (ทางเลือก) กราฟการแปลงที่สร้างโดยส่วนประกอบการแปลงต้นทาง
  • (ไม่บังคับ) สคีมาข้อมูลที่สร้างโดยส่วนประกอบไปป์ไลน์ SchemaGen และอาจแก้ไขโดยนักพัฒนา

ด้วยข้อมูล โมเดล และวัตถุประสงค์ที่กำหนด Tuner จะปรับแต่งไฮเปอร์พารามิเตอร์และให้ผลลัพธ์ที่ดีที่สุด

คำแนะนำ

จำเป็นต้องใช้ฟังก์ชันโมดูลผู้ใช้ tuner_fn ที่มีลายเซ็นต่อไปนี้สำหรับ Tuner:

...
from keras_tuner.engine import base_tuner

TunerFnResult = NamedTuple('TunerFnResult', [('tuner', base_tuner.BaseTuner),
                                             ('fit_kwargs', Dict[Text, Any])])

def tuner_fn(fn_args: FnArgs) -> TunerFnResult:
  """Build the tuner using the KerasTuner API.
  Args:
    fn_args: Holds args as name/value pairs.
      - working_dir: working dir for tuning.
      - train_files: List of file paths containing training tf.Example data.
      - eval_files: List of file paths containing eval tf.Example data.
      - train_steps: number of train steps.
      - eval_steps: number of eval steps.
      - schema_path: optional schema of the input data.
      - transform_graph_path: optional transform graph produced by TFT.
  Returns:
    A namedtuple contains the following:
      - tuner: A BaseTuner that will be used for tuning.
      - fit_kwargs: Args to pass to tuner's run_trial function for fitting the
                    model , e.g., the training and validation dataset. Required
                    args depend on the above tuner's implementation.
  """
  ...

ในฟังก์ชันนี้ คุณกำหนดทั้งโมเดลและช่องว่างการค้นหาไฮเปอร์พารามิเตอร์ และเลือกวัตถุประสงค์และอัลกอริธึมสำหรับการปรับแต่ง ส่วนประกอบ Tuner จะใช้โค้ดโมดูลนี้เป็นอินพุต ปรับไฮเปอร์พารามิเตอร์ และส่งเสียงผลลัพธ์ที่ดีที่สุด

Trainer สามารถใช้ไฮเปอร์พารามิเตอร์เอาต์พุตของ Tuner เป็นอินพุตและนำไปใช้ในโค้ดโมดูลผู้ใช้ได้ คำจำกัดความไปป์ไลน์มีลักษณะดังนี้:

...
tuner = Tuner(
    module_file=module_file,  # Contains `tuner_fn`.
    examples=transform.outputs['transformed_examples'],
    transform_graph=transform.outputs['transform_graph'],
    train_args=trainer_pb2.TrainArgs(num_steps=20),
    eval_args=trainer_pb2.EvalArgs(num_steps=5))

trainer = Trainer(
    module_file=module_file,  # Contains `run_fn`.
    examples=transform.outputs['transformed_examples'],
    transform_graph=transform.outputs['transform_graph'],
    schema=schema_gen.outputs['schema'],
    # This will be passed to `run_fn`.
    hyperparameters=tuner.outputs['best_hyperparameters'],
    train_args=trainer_pb2.TrainArgs(num_steps=100),
    eval_args=trainer_pb2.EvalArgs(num_steps=5))
...

คุณอาจไม่ต้องการปรับแต่งไฮเปอร์พารามิเตอร์ทุกครั้งที่คุณฝึกโมเดลของคุณใหม่ เมื่อคุณใช้ Tuner เพื่อกำหนดชุดไฮเปอร์พารามิเตอร์ที่ดีแล้ว คุณสามารถลบ Tuner ออกจากไปป์ไลน์ของคุณ และใช้ ImporterNode เพื่อนำเข้าส่วน Tuner จากการฝึกครั้งก่อนเพื่อฟีดไปยัง Trainer

hparams_importer = Importer(
    # This can be Tuner's output file or manually edited file. The file contains
    # text format of hyperparameters (keras_tuner.HyperParameters.get_config())
    source_uri='path/to/best_hyperparameters.txt',
    artifact_type=HyperParameters,
).with_id('import_hparams')

trainer = Trainer(
    ...
    # An alternative is directly use the tuned hyperparameters in Trainer's user
    # module code and set hyperparameters to None here.
    hyperparameters = hparams_importer.outputs['result'])

การปรับแต่งบนแพลตฟอร์ม Google Cloud (GCP)

เมื่อทำงานบน Google Cloud Platform (GCP) คอมโพเนนต์ Tuner จะใช้ประโยชน์จากบริการ 2 รายการได้ดังนี้

AI Platform Vizier เป็นแบ็กเอนด์ของการปรับแต่งไฮเปอร์พารามิเตอร์

AI Platform Vizier เป็นบริการที่ได้รับการจัดการซึ่งดำเนินการเพิ่มประสิทธิภาพกล่องดำ โดยใช้เทคโนโลยี Google Vizier

CloudTuner เป็นการใช้งาน KerasTuner ซึ่งพูดคุยกับบริการ AI Platform Vizier ในฐานะแบ็กเอนด์การศึกษา เนื่องจาก CloudTuner เป็นคลาสย่อยของ keras_tuner.Tuner จึงสามารถใช้เป็นการแทนที่แบบดรอปอินในโมดูล tuner_fn และดำเนินการเป็นส่วนหนึ่งของคอมโพเนนต์ TFX Tuner

ด้านล่างนี้คือข้อมูลโค้ดที่แสดงวิธีใช้ CloudTuner โปรดสังเกตว่าการกำหนดค่า CloudTuner จำเป็นต้องมีรายการที่เฉพาะเจาะจงสำหรับ GCP เช่น project_id และ region

...
from tensorflow_cloud import CloudTuner

...
def tuner_fn(fn_args: FnArgs) -> TunerFnResult:
  """An implementation of tuner_fn that instantiates CloudTuner."""

  ...
  tuner = CloudTuner(
      _build_model,
      hyperparameters=...,
      ...
      project_id=...,       # GCP Project ID
      region=...,           # GCP Region where Vizier service is run.
  )

  ...
  return TuneFnResult(
      tuner=tuner,
      fit_kwargs={...}
  )

การปรับแต่งแบบขนานบน Cloud AI Platform Training ฝูงพนักงานแบบกระจาย

กรอบงาน KerasTuner ซึ่งเป็นการใช้งานพื้นฐานขององค์ประกอบ Tuner มีความสามารถในการค้นหาไฮเปอร์พารามิเตอร์แบบขนาน แม้ว่าส่วนประกอบ Tuner สต็อกจะไม่สามารถดำเนินการค้นหาได้มากกว่าหนึ่งคนพร้อมกัน แต่ด้วยการใช้ ส่วนประกอบ Tuner ส่วนขยาย Google Cloud AI Platform ก็มอบความสามารถในการเรียกใช้การปรับแต่งแบบขนาน โดยใช้งาน AI Platform Training Job เป็นฝูงผู้ปฏิบัติงานแบบกระจาย ผู้จัดการ. TuneArgs คือการกำหนดค่าที่กำหนดให้กับส่วนประกอบนี้ นี่คือการแทนที่ส่วนประกอบ Tuner สต็อกแบบดรอปอิน

tuner = google_cloud_ai_platform.Tuner(
    ...   # Same kwargs as the above stock Tuner component.
    tune_args=proto.TuneArgs(num_parallel_trials=3),  # 3-worker parallel
    custom_config={
        # Configures Cloud AI Platform-specific configs . For for details, see
        # https://cloud.google.com/ai-platform/training/docs/reference/rest/v1/projects.jobs#traininginput.
        TUNING_ARGS_KEY:
            {
                'project': ...,
                'region': ...,
                # Configuration of machines for each master/worker in the flock.
                'masterConfig': ...,
                'workerConfig': ...,
                ...
            }
    })
...

ลักษณะการทำงานและเอาต์พุตของส่วนประกอบ Tuner ส่วนขยายจะเหมือนกับส่วนประกอบ Tuner หุ้น ยกเว้นว่าการค้นหาไฮเปอร์พารามิเตอร์หลายรายการจะดำเนินการพร้อมกันบนเครื่องของผู้ปฏิบัติงานที่แตกต่างกัน และด้วยเหตุนี้ num_trials จึงจะเสร็จสิ้นเร็วขึ้น วิธีนี้จะมีประสิทธิภาพโดยเฉพาะอย่างยิ่งเมื่ออัลกอริธึมการค้นหาสามารถขนานกันได้อย่างน่าเขินอาย เช่น RandomSearch อย่างไรก็ตาม หากอัลกอริธึมการค้นหาใช้ข้อมูลจากผลลัพธ์ของการทดลองก่อนหน้า เช่น อัลกอริธึม Google Vizier ที่ใช้ใน AI Platform Vizier การค้นหาแบบคู่ขนานมากเกินไปจะส่งผลเสียต่อประสิทธิภาพของการค้นหา

ตัวอย่าง E2E

E2E CloudTuner บนตัวอย่าง GCP

บทช่วยสอน KerasTuner

บทช่วยสอน CloudTuner

ข้อเสนอ

มีรายละเอียดเพิ่มเติมใน การอ้างอิง Tuner API