การฝึกอบรมแบบกระจาย

การฝึกอบรมแบบกระจาย เป็นรูปแบบหนึ่งของการฝึกอบรมที่มีการกระจายความต้องการทรัพยากรคอมพิวเตอร์ (เช่น CPU, RAM) ไปยังคอมพิวเตอร์หลายเครื่อง การฝึกอบรมแบบกระจายช่วยให้ฝึกอบรมได้เร็วขึ้นและบนชุดข้อมูลขนาดใหญ่ (ตัวอย่างสูงสุดไม่กี่พันล้านตัวอย่าง)

การฝึกอบรมแบบกระจายยังมีประโยชน์สำหรับ การเพิ่มประสิทธิภาพไฮเปอร์พารามิเตอร์แบบอัตโนมัติ โดยที่แบบจำลองหลายแบบได้รับการฝึกแบบคู่ขนานกัน

ในเอกสารนี้คุณจะได้เรียนรู้วิธีการ:

  • ฝึกฝนโมเดล TF-DF โดยใช้การฝึกอบรมแบบกระจาย
  • ปรับไฮเปอร์พารามิเตอร์ของโมเดล TF-DF โดยใช้การฝึกอบรมแบบกระจาย

ข้อจำกัด

ณ ขณะนี้ รองรับการฝึกอบรมแบบกระจายสำหรับ:

  • การฝึกอบรมโมเดล Gradient Boosted Trees ด้วย tfdf.keras.DistributedGradientBoostedTreesModel โมเดลต้นไม้ที่มีการไล่ระดับสีแบบกระจายจะเทียบเท่ากับแบบจำลองที่ไม่กระจาย
  • ค้นหาไฮเปอร์พารามิเตอร์สำหรับประเภทโมเดล TF-DF ใดๆ

วิธีเปิดใช้งานการฝึกอบรมแบบกระจาย

ส่วนนี้แสดงรายการขั้นตอนในการเปิดใช้งานการฝึกอบรมแบบกระจาย สำหรับตัวอย่างทั้งหมด โปรดดูหัวข้อถัดไป

ขอบเขตพารามิเตอร์ ServerStrategy

โมเดลและชุดข้อมูลถูกกำหนดไว้ในขอบเขต ParameterServerStrategy

strategy = tf.distribute.experimental.ParameterServerStrategy(...)
with strategy.scope():
  model = tfdf.keras.DistributedGradientBoostedTreesModel()
  distributed_train_dataset = strategy.distribute_datasets_from_function(dataset_fn)
model.fit(distributed_train_dataset)

รูปแบบชุดข้อมูล

เช่นเดียวกับการฝึกอบรมแบบไม่กระจาย คุณสามารถจัดเตรียมชุดข้อมูลเป็นได้

  1. ชุดข้อมูลแบบกระจายเทนเซอร์โฟลว์แบบจำกัด หรือ
  2. เส้นทางไปยังไฟล์ชุดข้อมูลโดยใช้ รูปแบบชุดข้อมูลที่เข้ากันได้รูปแบบใดรูป แบบหนึ่ง

การใช้ไฟล์ที่แบ่งส่วนนั้นง่ายกว่าการใช้ชุดข้อมูลแบบกระจายเทนเซอร์โฟลว์แบบจำกัด (1 บรรทัดเทียบกับโค้ด ~ 20 บรรทัด) อย่างไรก็ตาม เฉพาะแนวทางชุดข้อมูล tensorflow เท่านั้นที่รองรับการประมวลผลล่วงหน้าของ TensorFlow หากไปป์ไลน์ของคุณไม่มีการประมวลผลล่วงหน้า แนะนำให้ใช้ตัวเลือกชุดข้อมูลที่แบ่งส่วน

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

ตั้งค่าผู้ปฏิบัติงาน

กระบวนการหลัก คือโปรแกรมที่ใช้โค้ด Python ซึ่งกำหนดโมเดล TensorFlow กระบวนการนี้ไม่ได้ใช้การคำนวณจำนวนมาก การคำนวณการฝึกอบรมที่มีประสิทธิภาพกระทำโดย ผู้ปฏิบัติงาน ผู้ปฏิบัติงานคือกระบวนการที่ใช้เซิร์ฟเวอร์พารามิเตอร์ TensorFlow

หัวหน้าควรได้รับการกำหนดค่าด้วยที่อยู่ IP ของผู้ปฏิบัติงาน ซึ่งสามารถทำได้โดยใช้ตัวแปรสภาพแวดล้อม TF_CONFIG หรือโดยการสร้าง ClusterResolver ดู การฝึกอบรมเซิร์ฟเวอร์พารามิเตอร์ด้วย ParameterServerStrategy สำหรับรายละเอียดเพิ่มเติม

ParameterServerStrategy ของ TensorFlow กำหนดผู้ปฏิบัติงานสองประเภท: "ผู้ปฏิบัติงาน" และ "เซิร์ฟเวอร์พารามิเตอร์" TensorFlow กำหนดให้ผู้ปฏิบัติงานแต่ละประเภทอย่างน้อยหนึ่งคนสร้างอินสแตนซ์ อย่างไรก็ตาม TF-DF ใช้เฉพาะ "คนงาน" เท่านั้น ดังนั้น จำเป็นต้องสร้างอินสแตนซ์ "เซิร์ฟเวอร์พารามิเตอร์" หนึ่งรายการ แต่ TF-DF จะไม่ใช้งาน ตัวอย่างเช่น การกำหนดค่าการฝึกอบรม TF-DF อาจมีลักษณะดังนี้:

  • 1 หัวหน้า
  • คนงาน 50 คน
  • 1 เซิร์ฟเวอร์พารามิเตอร์

ผู้ปฏิบัติงานจำเป็นต้องเข้าถึงการดำเนินการฝึกอบรมแบบกำหนดเองของ TensorFlow Decision Forests มีสองตัวเลือกในการเปิดใช้งานการเข้าถึง:

  1. ใช้เซิร์ฟเวอร์พารามิเตอร์ TF-DF C++ ที่กำหนดค่าไว้ล่วงหน้า //third_party/tensorflow_decision_forests/tensorflow/distribute:tensorflow_std_server
  2. สร้างเซิร์ฟเวอร์พารามิเตอร์โดยการเรียก tf.distribute.Server() ในกรณีนี้ ควรนำเข้า TF-DF import tensorflow_decision_forests

ตัวอย่าง

ส่วนนี้จะแสดงตัวอย่างทั้งหมดของการกำหนดค่าการฝึกอบรมแบบกระจาย สำหรับตัวอย่างเพิ่มเติม ตรวจสอบ การทดสอบหน่วย TF-DF

ตัวอย่าง: การฝึกอบรมแบบกระจายบนเส้นทางชุดข้อมูล

แบ่งชุดข้อมูลของคุณออกเป็นชุดของไฟล์ที่แบ่งส่วนโดยใช้ รูปแบบชุดข้อมูลที่เข้ากันได้ ขอแนะนำให้ตั้งชื่อไฟล์ดังต่อไปนี้: /path/to/dataset/train-<5 digit index>-of-<total files> ตัวอย่างเช่น

/path/to/dataset/train-00000-of-00100
/path/to/dataset/train-00001-of-00005
/path/to/dataset/train-00002-of-00005
...

เพื่อประสิทธิภาพสูงสุด จำนวนไฟล์ควรมีอย่างน้อย 10 เท่าของจำนวนคนทำงาน ตัวอย่างเช่น หากคุณกำลังฝึกอบรมกับพนักงาน 100 คน ตรวจสอบให้แน่ใจว่าชุดข้อมูลแบ่งออกเป็นไฟล์อย่างน้อย 1,000 ไฟล์

ไฟล์นั้นสามารถอ้างอิงได้ด้วยนิพจน์การแบ่งส่วน เช่น:

  • /path/to/dataset/train@1000
  • /path/to/dataset/train@*

การฝึกอบรมแบบกระจายทำได้ดังนี้ ในตัวอย่างนี้ ชุดข้อมูลจะถูกจัดเก็บเป็น TFRecord ของ TensorFlow Examples (กำหนดโดยคีย์ tfrecord+tfe )

import tensorflow_decision_forests as tfdf
import tensorflow as tf

strategy = tf.distribute.experimental.ParameterServerStrategy(...)

with strategy.scope():
  model = tfdf.keras.DistributedGradientBoostedTreesModel()

model.fit_on_dataset_path(
    train_path="/path/to/dataset/train@1000",
    label_key="label_key",
    dataset_format="tfrecord+tfe")

print("Trained model")
model.summary()

ตัวอย่าง: การฝึกอบรมแบบกระจายบนชุดข้อมูลแบบกระจาย TensorFlow ที่มีจำกัด

TF-DF คาดว่าชุดข้อมูล TensorFlow ที่แบ่งส่วนผู้ปฏิบัติงานมีจำกัดแบบกระจาย:

  • แบบกระจาย : ชุดข้อมูลที่ไม่กระจายถูกรวมไว้ใน strategy.distribute_datasets_from_function
  • finite : ชุดข้อมูลควรอ่านแต่ละตัวอย่างเพียงครั้งเดียว ชุดข้อมูล ไม่ ควรมีคำสั่ง repeat
  • worker-sharded : ผู้ปฏิบัติงานแต่ละคนควรอ่านส่วนที่แยกจากกันของชุดข้อมูล

นี่คือตัวอย่าง:

import tensorflow_decision_forests as tfdf
import tensorflow as tf


def dataset_fn(context, paths):
  """Create a worker-sharded finite dataset from paths.

  Like for non-distributed training, each example should be visited exactly
  once (and by only one worker) during the training. In addition, for optimal
  training speed, the reading of the examples should be distributed among the
  workers (instead of being read by a single worker, or read and discarded
  multiple times).

  In other words, don't add a "repeat" statement and make sure to shard the
  dataset at the file level and not at the example level.
  """

  # List the dataset files
  ds_path = tf.data.Dataset.from_tensor_slices(paths)

  # Make sure the dataset is used with distributed training.
  assert context is not None


  # Split the among the workers.
  #
  # Note: The "shard" is applied on the file path. The shard should not be
  # applied on the examples directly.
  # Note: You cannot use 'context.num_input_pipelines' with ParameterServerV2.
  current_worker = tfdf.keras.get_worker_idx_and_num_workers(context)
  ds_path = ds_path.shard(
      num_shards=current_worker.num_workers,
      index=current_worker.worker_idx)

  def read_csv_file(path):
    """Reads a single csv file."""

    numerical = tf.constant([0.0], dtype=tf.float32)
    categorical_string = tf.constant(["NA"], dtype=tf.string)
    csv_columns = [
        numerical,  # feature 1
        categorical_string,  # feature 2
        numerical,  # feature 3
        # ... define the features here.
    ]
    return tf.data.experimental.CsvDataset(path, csv_columns, header=True)

  ds_columns = ds_path.interleave(read_csv_file)

  # We assume a binary classification label with the following possible values.
  label_values = ["<=50K", ">50K"]

  # Convert the text labels into integers:
  # "<=50K" => 0
  # ">50K" => 1
  init_label_table = tf.lookup.KeyValueTensorInitializer(
      keys=tf.constant(label_values),
      values=tf.constant(range(label_values), dtype=tf.int64))
  label_table = tf.lookup.StaticVocabularyTable(
      init_label_table, num_oov_buckets=1)

  def extract_label(*columns):
    return columns[0:-1], label_table.lookup(columns[-1])

  ds_dataset = ds_columns.map(extract_label)

  # The batch size has no impact on the quality of the model. However, a larger
  # batch size generally is faster.
  ds_dataset = ds_dataset.batch(500)
  return ds_dataset


strategy = tf.distribute.experimental.ParameterServerStrategy(...)
with strategy.scope():
  model = tfdf.keras.DistributedGradientBoostedTreesModel()

  train_dataset = strategy.distribute_datasets_from_function(
      lambda context: dataset_fn(context, [...list of csv files...])
  )

model.fit(train_dataset)

print("Trained model")
model.summary()

ตัวอย่าง: การปรับไฮเปอร์พารามิเตอร์แบบกระจายบนเส้นทางชุดข้อมูล

การปรับไฮเปอร์พารามิเตอร์แบบกระจายบนเส้นทางชุดข้อมูลจะคล้ายกับการฝึกแบบกระจาย ข้อแตกต่างเพียงอย่างเดียวคือตัวเลือกนี้เข้ากันได้กับโมเดลที่ไม่กระจาย ตัวอย่างเช่น คุณสามารถกระจายการปรับไฮเปอร์พารามิเตอร์ของโมเดลต้นไม้ที่มีการไล่ระดับสี (ไม่กระจาย) ได้

with strategy.scope():
  tuner = tfdf.tuner.RandomSearch(num_trials=30, use_predefined_hps=True)
  model = tfdf.keras.GradientBoostedTreesModel(tuner=tuner)

training_history = model.fit_on_dataset_path(
  train_path=train_path,
  label_key=label,
  dataset_format="csv",
  valid_path=test_path)

logging.info("Trained model:")
model.summary()

ตัวอย่าง: การทดสอบหน่วย

หากต้องการทดสอบหน่วยการฝึกอบรมแบบกระจาย คุณสามารถสร้างกระบวนการของผู้ปฏิบัติงานจำลองได้ ดูวิธีการ _create_in_process_tf_ps_cluster ใน การทดสอบหน่วย TF-DF สำหรับข้อมูลเพิ่มเติม