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

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

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

คอมโพเนนต์ 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 Platform (GCP)

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

  • AI Platform Vizier (ผ่านการใช้งาน CloudTuner)
  • AI Platform Training (ในฐานะผู้จัดการฝูงสำหรับการปรับแต่งแบบกระจาย)

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 = 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 Vizier การค้นหาแบบขนานมากเกินไปจะส่งผลเสียต่อประสิทธิภาพของการค้นหา

ตัวอย่าง E2E

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

กวดวิชา KerasTuner

บทช่วยสอน CloudTuner

ข้อเสนอ

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