실험 추적으로 학습 작업 실행

Vertex AI는 대규모 모델 학습을 운영할 수 있게 해주는 관리형 학습 서비스를 제공합니다. Python용 Vertex AI SDK를 사용한 실험 추적을 사용 설정하여 커스텀 학습 작업을 제출할 때 매개변수 및 성능 측정항목을 캡처할 수 있습니다.

이 기능은 다음과 같은 경우에 사용할 수 없습니다.

  • Google Cloud Console 또는 Google Cloud CLI를 통해 학습 작업을 제출합니다.
  • 학습 작업에 TPU 사용,
  • 학습 작업에 분산 학습을 사용합니다.

사전 빌드된 학습 컨테이너커스텀 컨테이너 지원이 모두 지원됩니다. 필수: google-cloud-aiplatform용 1.24.1보다 높은 Python용 Vertex AI SDK 버전이 설치되어 있습니다. TensorFlow로 학습하는 경우 충돌을 방지하기 위해 protobuf 버전이 4.0 미만으로 설치되어 있는지 확인합니다.

데이터를 Vertex AI 실험에 로깅할 수 있는 두 가지 옵션은 자동 로깅과 수동 로깅입니다.

지원되는 프레임워크인 Fastai, Gluon, Keras, LightGBM, Pytorch Lightning, Scikit-learn, Spark, Statsmodels, XGBoost 중 하나를 사용하는 경우에는 자동 로깅을 사용하는 것이 좋습니다. 프레임워크가 지원되지 않거나 실험 실행에 로깅하려는 커스텀 측정항목이 있는 경우 학습 스크립트를 직접 조정하여 매개변수, 측정항목, 아티팩트를 로깅할 수 있습니다.

데이터 자동 로깅

자동 로깅을 사용 설정하려면 enable_autolog=True를 설정합니다. from_local_script를 참조하세요. 실험 실행 생성 여부를 선택할 수 있습니다. 실험 이름을 지정하지 않으면 실험 이름이 자동으로 생성됩니다.

Python용 Vertex AI SDK는 사용자를 위해 ExperimentRun 리소스 만들기를 처리합니다.

Python

def create_custom_job_with_experiment_autologging_sample(
    project: str,
    location: str,
    staging_bucket: str,
    display_name: str,
    script_path: str,
    container_uri: str,
    service_account: str,
    experiment: str,
    experiment_run: Optional[str] = None,
) -> None:
    aiplatform.init(project=project, location=location, staging_bucket=staging_bucket, experiment=experiment)

    job = aiplatform.CustomJob.from_local_script(
        display_name=display_name,
        script_path=script_path,
        container_uri=container_uri,
        enable_autolog=True,
    )

    job.run(
        service_account=service_account,
        experiment=experiment,
        experiment_run=experiment_run,
    )

  • project: 프로젝트 ID 이러한 프로젝트 ID는 Google Cloud 콘솔 시작 페이지에서 찾을 수 있습니다.
  • location: 사용 가능한 위치 목록을 참조하세요.
  • staging_bucket: 버킷에 지정한 이름입니다(예: my_bucket).
  • display_name: CustomJob의 사용자 정의 이름입니다.
  • script_path: 학습 코드의 진입점인 스크립트의 경로입니다(로컬 파일 시스템의 작업 디렉터리 기준).
  • container_uri: 학습 컨테이너 이미지의 URI는 Vertex AI 사전 빌드된 학습 컨테이너 또는 커스텀 컨테이너일 수 있습니다.
  • service_account: 필요한 권한이 있는 서비스 계정 만들기를 참조하세요.
  • experiment: 실험 이름을 입력합니다. 실험에 텐서보드 인스턴스가 있어야 합니다. 섹션 탐색 메뉴에서 실험을 선택하면 Google Cloud 콘솔에서 실험 목록을 찾을 수 있습니다.
  • experiment_run: (선택사항) 실행 이름을 지정합니다. 지정하지 않으면 실행이 자동 생성됩니다.

수동으로 데이터 로깅

수동 로그 데이터 옵션을 사용하여 학습 스크립트를 통합합니다.

학습 스크립트를 변경하는 방법은 다음과 같습니다.

import os
import pickle
import pandas as pd
from sklearn.linear_model import LinearRegression
# To use manual logging APIs, import aiplatform
from google.cloud import aiplatform

# Create Dataset
data = {'A': [1.1,2.2,4.1,5.2],
        'B': [200, 212.12, 22, 123],
        'Y': [1,0,1,0]}
df = pd.DataFrame(data)
X = df[['A', 'B']]
Y = df['Y']

# Train model
model = LinearRegression().fit(X, Y)

# Save the model to gcs
model_dir = os.getenv('AIP_MODEL_DIR')
model_gcs = model_dir.replace('gs://', '/gcs/')
model_name = 'model.pkl'
os.mkdir(model_gcs)
f = open(os.path.join(model_gcs, model_name), 'wb')
pickle.dump(model, f)

f = open(os.path.join(model_gcs, model_name), 'wb')
    pickle.dump(model, f)

# Call aiplatform's logging APIs to save data to Vertex AI Experiments.
params = model.get_params()
aiplatform.log_params(params)
metrics = {"training_accuracy": model.score(X,Y)}
aiplatform.log_metrics(metrics)

실험 실행 생성 여부를 선택할 수 있습니다. 실험 이름을 지정하지 않으면 실험 이름이 자동으로 생성됩니다.

자세한 내용은 실험 실행에 수동으로 데이터 로깅을 참조하세요.

Python

def create_custom_job_with_experiment_sample(
    project: str,
    location: str,
    staging_bucket: str,
    display_name: str,
    script_path: str,
    container_uri: str,
    service_account: str,
    experiment: str,
    experiment_run: Optional[str] = None,
) -> None:
    aiplatform.init(
        project=project,
        location=location,
        staging_bucket=staging_bucket,
        experiment=experiment
    )

    job = aiplatform.CustomJob.from_local_script(
        display_name=display_name,
        script_path=script_path,
        container_uri=container_uri,
    )

    job.run(
        service_account=service_account,
        experiment=experiment,
        experiment_run=experiment_run,
    )

  • project: 프로젝트 ID 이러한 프로젝트 ID는 Google Cloud 콘솔 시작 페이지에서 찾을 수 있습니다.
  • location: 사용 가능한 위치 목록을 참조하세요.
  • staging_bucket: 버킷에 지정한 이름입니다(예: my_bucket).
  • display_name: CustomJob의 사용자 정의 이름입니다.
  • script_path: 학습 코드의 진입점인 스크립트의 경로입니다(로컬 파일 시스템의 작업 디렉터리 기준).
  • container_uri: 학습 컨테이너 이미지의 URI는 Vertex AI 사전 빌드된 학습 컨테이너 또는 커스텀 컨테이너일 수 있습니다. 커스텀 컨테이너를 사용하는 경우 google-cloud-aiplatform>=1.24.0이 설치되어 있는지 확인합니다.
  • service_account: 필요한 권한이 있는 서비스 계정 만들기를 참조하세요.
  • experiment: 실험 이름을 입력합니다. 섹션 탐색 메뉴에서 실험을 선택하면 Google Cloud 콘솔에서 실험 목록을 찾을 수 있습니다.
  • experiment_run: 실행 이름을 지정합니다. 지정하지 않으면 실행이 자동 생성됩니다.

자동 로깅된 매개변수 및 측정항목 보기

Python용 Vertex AI SDK를 사용하여 실행을 비교하고 실행 데이터를 가져옵니다. Google Cloud 콘솔에서는 이러한 실행을 간편하게 비교하는 방법을 제공합니다.

다음 단계

관련 노트북 샘플