iT邦幫忙

2021 iThome 鐵人賽

DAY 21
0
AI & Data

一起學習 Azure Machine Learning 系列 第 21

[DAY21] 用 Azure Machine Learning SDK 建立運算資源

DAY21 用 Azure Machine Learning SDK 建立運算資源

資料集也建立完成了,今天我們就來建立運算資源(VM)吧!還記得我們之前討論過運算資源的四種型式:

  1. Compute instances
  2. Compute clusters
  3. Inference clusters AKS
  4. Attached compute
    如果忘記了的話可以回到這篇去閱讀。

第三項是部署的部份,等到我們講到用 SDK 部署的時候再來講。

建立運算資源

  1. 建立 cluster 運算資源,我們要用 azureml.core.compute.ComputeTarget 類別和 AmlCompute 類別。而建立 compute instance
    我們要用 ComputeInstance 類別。

程式碼參考如下:

from azureml.core import Workspace
from azureml.core.compute import ComputeTarget, AmlCompute

ws = Workspace.from_config()

compute_name = 'aml-sdk'

# 設定要建立 VM 的規格、包含節點數目,是否專用等等。如果不用 cluster,改用 ComputeInstance
compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_DS11_V2',
                                                       min_nodes=0, max_nodes=4,
                                                       vm_priority='dedicated')

# 建立 VM
aml_cluster = ComputeTarget.create(ws, compute_name, compute_config)
aml_cluster.wait_for_completion(show_output=True) # 加這行可以看到建立的進度
  1. 一般在實務上,會用 Try Catch 的型式包起來,如果該 VM 沒有存在,那就建立起來。程式碼改寫如下:
from azureml.core.compute import ComputeTarget, AmlCompute
from azureml.core.compute_target import ComputeTargetException

compute_name = "aml-sdk"

# 確認該運算資源是否存在
try:
    aml_cluster = ComputeTarget(workspace=ws, name=compute_name)
    print('查無此VM')
except ComputeTargetException:
    # 如果不存在就建立
    compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_DS11_V2',
                                                           max_nodes=4)
    aml_cluster = ComputeTarget.create(ws, compute_name, compute_config)

aml_cluster.wait_for_completion(show_output=True)
  1. 我們也可以進到圖形化介面裡去看,可以看到剛剛我們要建立的運算資源,真的建立起來了,如下圖。
    Build compute resource with azure machine learning sdk

  2. 如果要用 Attached compute,就要 import 對應運算資源的類別。例如說,你要用 Databricks,就要引入 DatabricksCompute 類別,要用 HDInsight,就要引入 HDInsightCompute 類別。建議當需要時,再去查詢文件。

  3. 除了範例之外,其實還有很多參數是可以做設定的哦!例如說你建立了 Compute instance,還可以再建立其虛擬網路。詳細的技術文件可以參考這裡做查詢。

今天我們完成了運算資源的建立,明天我們來建立環境吧!


上一篇
[DAY20] 用 Azure Machine Learning SDK 建立 Dataset
下一篇
DAY22 用 Azure Machine Learning SDK 建立環境
系列文
一起學習 Azure Machine Learning 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言