存取作業中的資料
適用於:Azure CLI ml 延伸模組 v2 (目前)
Python SDK azure-ai-ml v2 (目前)
了解如何使用 Azure Machine Learning Python SDK v2 和 Azure Machine Learning CLI 延伸模組 v2,讀取和寫入作業的資料。
必要條件
Azure 訂用帳戶。 如果您沒有 Azure 訂用帳戶,請在開始前先建立免費帳戶。 試用免費或付費版本的 Azure Machine Learning。
Azure Machine Learning 工作區
支援的路徑
當您提供資料輸入/輸出給作業時,您必須指定 path
指向資料位置的參數。 下表顯示 Azure Machine Learning 支援的不同資料位置,以及 參數的 path
範例:
位置 | 範例 |
---|---|
本機電腦上的路徑 | ./home/username/data/my_data |
公用 HTTP 伺服器的路徑 | https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv |
Azure 儲存體上的路徑 | https://<account_name>.blob.core.windows.net/<container_name>/<path> abfss://<file_system>@<account_name>.dfs.core.windows.net/<path> |
資料存放區上的路徑 | azureml://datastores/<data_store_name>/paths/<path> |
資料資產的路徑 | azureml:<my_data>:<version> |
支援的模式
當您執行具有資料輸入/輸出的作業時,可以指定 模式 ,例如,資料是否應該掛接唯讀,或下載至計算目標。 下表顯示不同類型/模式/輸入/輸出組合的可能模式:
類型 | 輸入/輸出 | upload |
download |
ro_mount |
rw_mount |
direct |
eval_download |
eval_mount |
---|---|---|---|---|---|---|---|---|
uri_folder |
輸入 | ✓ | ✓ | ✓ | ||||
uri_file |
輸入 | ✓ | ✓ | ✓ | ||||
mltable |
輸入 | ✓ | ✓ | ✓ | ✓ | ✓ | ||
uri_folder |
輸出 | ✓ | ✓ | |||||
uri_file |
輸出 | ✓ | ✓ | |||||
mltable |
輸出 | ✓ | ✓ | ✓ |
注意
eval_download
和 eval_mount
對 mltable
而言是唯一的。 ro_mount
是 MLTable 的預設模式。 不過,在某些情況下,MLTable 可以產生不一定與儲存體中 MLTable 檔案共置的檔案。 或者, mltable
可以子集或隨機顯示位於儲存體資源中的資料。 只有當引擎實際評估 MLTable 檔案時,才會顯示該檢視。 這些模式提供檔案的檢視。
在作業中讀取資料
建立作業規格 YAML 檔案 (<file-name>.yml
)。 在 inputs
作業的 區段中,指定:
type
;不論資料是特定檔案 () 、資料夾位置 (uri_file
uri_folder
) 或mltable
。path
資料位置的 ;[支援的路徑] 區段中概述的任何路徑都會運作。
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
# Possible Paths for Data:
# Blob: https://<account_name>.blob.core.windows.net/<container_name>/<folder>/<file>
# Datastore: azureml://datastores/paths/<folder>/<file>
# Data Asset: azureml:<my_data>:<version>
command: |
ls ${{inputs.my_data}}
code: <folder where code is located>
inputs:
my_data:
type: <type> # uri_file, uri_folder, mltable
path: <path>
environment: azureml:AzureML-sklearn-1.0-ubuntu20.04-py38-cpu@latest
compute: azureml:cpu-cluster
接下來,在 CLI 中執行
az ml job create -f <file-name>.yml
讀取 V1 資料資產
本節說明如何在 V2 作業中讀取 V1 FileDataset
和資料 TabularDataset
實體。
讀取 FileDataset
建立作業規格 YAML 檔案 (<file-name>.yml
),其類型設為 mltable
及模式設為 eval_mount
:
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
command: |
ls ${{inputs.my_data}}
code: <folder where code is located>
inputs:
my_data:
type: mltable
mode: eval_mount
path: azureml:<filedataset_name>@latest
environment: azureml:<environment_name>@latest
compute: azureml:cpu-cluster
接下來,在 CLI 中執行
az ml job create -f <file-name>.yml
讀取 TabularDataset
建立作業規格 YAML 檔案 (<file-name>.yml
),其類型設為 mltable
及模式設為 direct
:
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
command: |
ls ${{inputs.my_data}}
code: <folder where code is located>
inputs:
my_data:
type: mltable
mode: direct
path: azureml:<tabulardataset_name>@latest
environment: azureml:<environment_name>@latest
compute: azureml:cpu-cluster
接下來,在 CLI 中執行
az ml job create -f <file-name>.yml
在作業中寫入資料
在您的作業中,您可以使用 輸出將資料寫入雲端式儲存體。 支援的模式區段顯示只有作業輸出可以寫入資料,因為模式可以是 rw_mount
或 upload
。
建立作業規格 YAML 檔案 (<file-name>.yml
) ,並 outputs
填入您要寫入資料的類型和路徑區段:
$schema: https://azuremlschemas.azureedge.net/latest/CommandJob.schema.json
# Possible Paths for Data:
# Blob: https://<account_name>.blob.core.windows.net/<container_name>/<folder>/<file>
# Datastore: azureml://datastores/paths/<folder>/<file>
# Data Asset: azureml:<my_data>:<version>
code: src
command: >-
python prep.py
--raw_data ${{inputs.raw_data}}
--prep_data ${{outputs.prep_data}}
inputs:
raw_data:
type: <type> # uri_file, uri_folder, mltable
path: <path>
outputs:
prep_data:
type: <type> # uri_file, uri_folder, mltable
path: <path>
environment: azureml:<environment_name>@latest
compute: azureml:cpu-cluster
接下來,使用 CLI 建立作業:
az ml job create --file <file-name>.yml
管線中的資料
如果您使用 Azure Machine Learning 管線,您可以使用 Azure Machine Learning CLI v2 擴充功能或 Python SDK v2,將資料讀取至管線元件,並在管線元件之間移動資料。
Azure Machine Learning CLI v2
此 YAML 檔案示範如何使用某個元件的輸出資料作為管線另一個元件的輸入,以及 Azure Machine Learning CLI v2 擴充功能:
$schema: https://azuremlschemas.azureedge.net/latest/pipelineJob.schema.json
type: pipeline
display_name: 3b_pipeline_with_data
description: Pipeline with 3 component jobs with data dependencies
settings:
default_compute: azureml:cpu-cluster
outputs:
final_pipeline_output:
mode: rw_mount
jobs:
component_a:
type: command
component: ./componentA.yml
inputs:
component_a_input:
type: uri_folder
path: ./data
outputs:
component_a_output:
mode: rw_mount
component_b:
type: command
component: ./componentB.yml
inputs:
component_b_input: ${{parent.jobs.component_a.outputs.component_a_output}}
outputs:
component_b_output:
mode: rw_mount
component_c:
type: command
component: ./componentC.yml
inputs:
component_c_input: ${{parent.jobs.component_b.outputs.component_b_output}}
outputs:
component_c_output: ${{parent.outputs.final_pipeline_output}}
# mode: upload
Python SDK v2
此範例會定義包含三個節點的管線,並在每個節點之間移動資料。
prepare_data_node
會將來自 Fashion MNIST 資料集的影像和標籤載入 至mnist_train.csv
和mnist_test.csv
。train_node
使用定型資料,使用mnist_train.csv
Keras 來定型 CNN 模型。score_node
使用mnist_test.csv
測試資料為模型評分。
# define a pipeline containing 3 nodes: Prepare data node, train node, and score node
@pipeline(
default_compute=cpu_compute_target,
)
def image_classification_keras_minist_convnet(pipeline_input_data):
"""E2E image classification pipeline with keras using python sdk."""
prepare_data_node = prepare_data_component(input_data=pipeline_input_data)
train_node = keras_train_component(
input_data=prepare_data_node.outputs.training_data
)
train_node.compute = gpu_compute_target
score_node = keras_score_component(
input_data=prepare_data_node.outputs.test_data,
input_model=train_node.outputs.output_model,
)
# create a pipeline
pipeline_job = image_classification_keras_minist_convnet(pipeline_input_data=fashion_ds)