

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 執行管道
<a name="pipelines-step-decorator-run-pipeline"></a>

下頁描述如何使用 Amazon SageMaker Pipelines 執行管道，無論是搭配 SageMaker AI 資源還是在本機。

使用 `pipeline.start()` 函式啟動新的管道執行，就像傳統 SageMaker AI 管道執行一樣。如需 `start()` 函式的相關資訊，請參閱 [sagemaker.workflow.pipeline.Pipeline.start](https://sagemaker.readthedocs.io/en/stable/workflows/pipelines/sagemaker.workflow.pipelines.html#sagemaker.workflow.pipeline.Pipeline.start)。

**注意**  
使用 `@step` 裝飾項目定義的步驟會做為訓練任務執行。因此，請注意下列限制：  
您帳戶中的執行個體限制和訓練任務限制。相應地更新您的限制，以避免發生任何限流或資源限制問題。
與管道中每個訓練步驟執行相關聯的金錢成本。如需詳細資訊，請參閱 [Amazon SageMaker 定價](https://aws.amazon.com/sagemaker/pricing/)。

## 從本機執行的管道擷取結果
<a name="pipelines-step-decorator-run-pipeline-retrieve"></a>

若要檢視管道執行任何步驟的結果，請使用 [execution.result()](https://sagemaker.readthedocs.io/en/stable/workflows/pipelines/sagemaker.workflow.pipelines.html#sagemaker.workflow.pipeline._PipelineExecution.result           )，如下列程式碼片段所示：

```
execution = pipeline.start()
execution.result(step_name="train")
```

**注意**  
Pipelines 不支援本機模式下的 `execution.result()`。

您一次只能擷取一個步驟的結果。如果步驟名稱是由 SageMaker AI 產生，您可以呼叫 `list_steps` 來擷取步驟名稱，如下所示：

```
execution.list_step()
```

## 在本機執行管道
<a name="pipelines-step-decorator-run-pipeline-local"></a>

您可以像執行傳統管道步驟一樣，使用 `@step` 裝飾步驟在本機執行管道。如需本機模式管道執行的詳細資訊，請參閱[使用本機模式執行管道](pipelines-local-mode.md)。若要使用本機模式，請將 `LocalPipelineSession` 而非 `SageMakerSession` 提供給管道定義，如下列範例所示：

```
from sagemaker.workflow.function_step import step
from sagemaker.workflow.pipeline import Pipeline
from sagemaker.workflow.pipeline_context import LocalPipelineSession

@step
def train():
    training_data = s3.download(....)
    ...
    return trained_model
    
step_train_result = train()

local_pipeline_session = LocalPipelineSession()

local_pipeline = Pipeline(
    name="{{<pipeline-name>}}",
    steps=[step_train_result],
    sagemaker_session=local_pipeline_session # needed for local mode
)

local_pipeline.create(role_arn="role_arn")

# pipeline runs locally
execution = local_pipeline.start()
```