XGBoost 训练 (XGBoostJob)

使用 XGBoostJob 训练 XGBoost 模型

此页面描述了用于使用 XGBoost 训练机器学习模型的 XGBoostJob

XGBoostJob 是一个 Kubernetes 自定义资源,用于在 Kubernetes 上运行 XGBoost 训练 Job。Kubeflow 对 XGBoostJob 的实现在 training-operator 中。

注意:由于 Istio 自动 sidecar 注入XGBoostJob 默认无法在用户命名空间中工作。为了使其运行,需要在 PyTorchJob pod 或命名空间中添加注解 sidecar.istio.io/inject: "false" 来禁用它。有关如何将此注解添加到您的 yaml 文件的示例,请参阅XGBoostJob 文档

创建 XGBoost 训练 Job

您可以通过定义 XGBoostJob 配置文件来创建训练 Job。请参阅IRIS 示例 的清单。您可以根据您的要求修改配置文件。例如:在 Spec 中将 CleanPodPolicy 设置为 None,以在 Job 终止后保留 pod。

cat xgboostjob.yaml

部署 XGBoostJob 资源以开始训练

kubectl create -f xgboostjob.yaml

现在您应该能够看到已创建的 pod,其数量与指定的副本数匹配。

kubectl get pods -l job-name=xgboost-dist-iris-test-train

在 CPU 集群上训练需要 5-10 分钟。可以通过查看日志来了解训练进度。

PODNAME=$(kubectl get pods -l job-name=xgboost-dist-iris-test-train,replica-type=master,replica-index=0 -o name)
kubectl logs -f ${PODNAME}

监控 XGBoostJob

kubectl get -o yaml xgboostjobs xgboost-dist-iris-test-train

请参阅状态部分以监控 Job 状态。以下是 Job 成功完成时的示例输出。

apiVersion: kubeflow.org/v1
kind: XGBoostJob
metadata:
  creationTimestamp: "2021-09-06T18:34:06Z"
  generation: 1
  name: xgboost-dist-iris-test-train
  namespace: default
  resourceVersion: "5844304"
  selfLink: /apis/kubeflow.org/v1/namespaces/default/xgboostjobs/xgboost-dist-iris-test-train
  uid: a1ea6675-3cb5-482b-95dd-68b2c99b8adc
spec:
  runPolicy:
    cleanPodPolicy: None
  xgbReplicaSpecs:
    Master:
      replicas: 1
      restartPolicy: Never
      template:
        spec:
          containers:
            - args:
                - --job_type=Train
                - --xgboost_parameter=objective:multi:softprob,num_class:3
                - --n_estimators=10
                - --learning_rate=0.1
                - --model_path=/tmp/xgboost-model
                - --model_storage_type=local
              image: docker.io/merlintang/xgboost-dist-iris:1.1
              imagePullPolicy: Always
              name: xgboost
              ports:
                - containerPort: 9991
                  name: xgboostjob-port
                  protocol: TCP
    Worker:
      replicas: 2
      restartPolicy: ExitCode
      template:
        spec:
          containers:
            - args:
                - --job_type=Train
                - --xgboost_parameter="objective:multi:softprob,num_class:3"
                - --n_estimators=10
                - --learning_rate=0.1
              image: docker.io/merlintang/xgboost-dist-iris:1.1
              imagePullPolicy: Always
              name: xgboost
              ports:
                - containerPort: 9991
                  name: xgboostjob-port
                  protocol: TCP
status:
  completionTime: "2021-09-06T18:34:23Z"
  conditions:
    - lastTransitionTime: "2021-09-06T18:34:06Z"
      lastUpdateTime: "2021-09-06T18:34:06Z"
      message: xgboostJob xgboost-dist-iris-test-train is created.
      reason: XGBoostJobCreated
      status: "True"
      type: Created
    - lastTransitionTime: "2021-09-06T18:34:06Z"
      lastUpdateTime: "2021-09-06T18:34:06Z"
      message: XGBoostJob xgboost-dist-iris-test-train is running.
      reason: XGBoostJobRunning
      status: "False"
      type: Running
    - lastTransitionTime: "2021-09-06T18:34:23Z"
      lastUpdateTime: "2021-09-06T18:34:23Z"
      message: XGBoostJob xgboost-dist-iris-test-train is successfully completed.
      reason: XGBoostJobSucceeded
      status: "True"
      type: Succeeded
  replicaStatuses:
    Master:
      succeeded: 1
    Worker:
      succeeded: 2

反馈

此页面是否有帮助?


最后修改于 2025 年 2 月 15 日:trainer: 在 Training Operator v1 文档中添加弃用警告 (#3997) (8ad90c5)