追蹤類別

Python 適用的 Vertex AI SDK 包含可協助進行視覺化、測量和追蹤的類別。這些類別可分為三種類型:

  • 使用中繼資料追蹤機器學習 (ML) 工作流程中資源的類別
  • 用於 Vertex AI Experiments 的類別
  • 用於 Vertex AI TensorBoard 的類別

下列主題概述了與追蹤及監控 Vertex AI SDK for Python 中機器學習工作流程相關的類別。

中繼資料類別

您可以使用 Vertex AI SDK for Python 建立 Vertex ML Metadata,追蹤及分析機器學習工作流程中的中繼資料。詳情請參閱「Vertex ML Metadata 簡介」。

Artifact

Artifact 類別代表 Gemini Enterprise Agent Platform 中構件的中繼資料。構件是機器學習工作流程產生的離散實體或資料片段。構件的例子包括資料集模型輸入檔案。詳情請參閱「追蹤執行作業和構件」。

建立 Artifact 資源時,您需要指定其結構定義。每種構件都有專屬結構定義。舉例來說,system.Dataset 結構定義代表資料集,而 system.Metrics 結構定義代表評估指標。詳情請參閱「如何使用系統結構定義」。

以下程式碼範例說明如何建立代表模型的 Artifact 資源:

model_artifact = aiplatform.Artifact.create(
        schema_title="system.Model",
        display_name=PREPROCESSED_DATASET_NAME,
        uri=PREPROCESSED_DATASET_URI,

Execution

Execution 類別代表 Gemini Enterprise Agent Platform 執行作業中的中繼資料。執行作業是機器學習工作流程中的一個步驟。 執行作業的例子包括資料處理、訓練和模型評估。執行作業可取用資料集等構件,並產生模型等構件。

使用 aiplatform.start_execution 建立 Execution 資源。建立 Execution 資源後,請使用相同的 aiplatform.start_execution 方法,並將 resume 參數設為 True,即可繼續執行。

以下程式碼範例說明如何建立 Execution 資源:

with aiplatform.start_execution(schema_title='system.ContainerExecution',
                                display_name='trainer') as execution:
    execution.assign_input_artifacts([my_artifact])
    model = aiplatform.Artifact.create(uri='gs://my-uri', schema_title='system.Model')
    execution.assign_output_artifacts([model])

Vertex AI Experiments 類別

您可以使用 Python 適用的 Vertex AI SDK 建立及執行 Vertex AI Experiments。使用 Vertex AI Experiments 追蹤記錄的指標和參數,協助您分析及最佳化機器學習工作流程。詳情請參閱「Vertex AI Experiments 簡介」。

如要進一步瞭解如何使用 ExperimentExperimentRun 類別,請嘗試下列其中一個教學課程:

Experiment

Experiment 類別代表 Gemini Enterprise Agent Platform 中的實驗。使用實驗分析其實驗執行管道執行,並採用不同設定,例如多個輸入構件和超參數。

建立 Experiment 資源的方式有兩種:

  1. 建立 Experiment 的偏好方式是呼叫 aiplatform.init 時,將實驗名稱指定為參數:

    # In a real world scenario it's likely you would specify more parameters
    # when you call aiplatform.init. This sample shows only how to use the
    # parameter used to create an Experiment.
    
    # Specify a name for the experiment
    EXPERIMENT_NAME = "your-experiment-name"
    
    # Create the experiment
    aiplatform.init(experiment=EXPERIMENT_NAME)
    
  2. 您也可以呼叫 aiplatform.Experiment.create 來建立 Experimentaiplatform.Experiment.create 會建立 Experiment 資源,但不會將其設為全域環境。因此,您無法使用 aiplatform.start_run 執行實驗。以下程式碼範例說明如何使用 aiplatform.Experiment.create 建立實驗,然後執行實驗:

    # Specify a name for the experiment
    EXPERIMENT_NAME = "your-experiment-name"
    EXPERIMENT_RUN_NAME = "your-run"
    
    # Create the experiment
    experiment = aiplatform.Experiment.create(experiment_name=EXPERIMENT_NAME)
    experiment_run = aiplatform.ExperimentRun.create(EXPERIMENT_RUN_NAME, experiment=EXPERIMENT_NAME)
    

ExperimentRun

ExperimentRun 類別代表實驗的執行作業。

下列程式碼範例說明如何建立及啟動實驗執行作業,然後使用該作業取得實驗相關資訊。如要刪除實驗執行作業,請取得 ExperimentRun 執行個體的參照,並呼叫其 delete 方法。

# Specify your project name, location, experiment name, and run name
PROJECT_NAME = "my-project"
LOCATION = "us-central1"
EXPERIMENT_NAME = "experiment-1"
RUN_NAME = "run-1"

# Create the experiment to run
aiplatform.init(experiment=EXPERIMENT_NAME,
                project=PROJECT_NAME,
                location=LOCATION)

# Create and run an ExperimentRun resource. Next, you can use it to get
# information about your experiment. For example, you can log parameters and
# metrics with specified key-value pairs.
with aiplatform.start_run(RUN_NAME):
     aiplatform.log_params({'learning_rate': 0.1, 'dropout_rate': 0.2})
     aiplatform.log_metrics({'accuracy': 0.9, 'recall': 0.8})

# Get a reference to the ExperimentRun resource, get the parameters logged to 
# the run, get the summary metrics logged to the run, then delete it.
with aiplatform.start_run(RUN_NAME, resume=True) as run:
     run.get_params()
     run.get_metrics()
     run.delete()

Vertex AI TensorBoard 類別

Python 適用的 Vertex AI SDK 包含多個類別,可搭配使用開放原始碼的代管版 Vertex AI TensorBoard。Vertex AI TensorBoard 是一種工具,可用於在機器學習工作流程中監控測量結果和視覺化內容。詳情請參閱「開始使用 Vertex AI TensorBoard」。

如要進一步瞭解如何使用 Vertex AI SDK for Python 搭配 Vertex AI TensorBoard,請試用下列任一筆記本教學課程:

Tensorboard

Tensorboard 類別代表代管資源,用於儲存 Vertex AI TensorBoard 實驗。您必須先建立Tensorboard執行個體,才能查看實驗結果。您可以在 Google Cloud 專案中建立多個Tensorboard例項。

以下程式碼範例說明如何建立 Tensorboard 執行個體:

# Specify your project name, location, and the name of your Tensorboard
PROJECT_NAME = "my-project"
LOCATION = "us-central1"
TENSORBOARD_NAME = "my-tensorboard"

aiplatform.init(project=PROJECT_NAME, location=LOCATION)

tensorboard = aiplatform.Tensorboard.create(
    display_name=TENSORBOARD_NAME,
    project=PROJECT_NAME,
    location=LOCATION,
)

TensorboardExperiment

TensorboardExperiment 代表一組 TensorboardRun 物件。TensorboardRun 執行個體代表 Tensorboard 中訓練工作執行的結果。

TensorboardRun

TensorboardRun 類別的執行個體會對應至 Tensorboard 中執行的訓練工作,並具有一組指定的超參數、模型定義、資料集等。

TensorboardTimeSeries

TensorboardTimeSeries 類別代表訓練執行作業中產生的序列。

後續步驟