자습서 6: 기능 저장소를 사용하여 네트워크 격리
Azure Machine Learning 관리되는 네트워크 격리를 사용하면 기능을 검색하고, 만들고, 운용할 수 있습니다. 기능은 다양한 기능을 실험하는 프로토타입 단계부터 시작하여 기계 학습 수명 주기에서 결합 조직 역할을 합니다. 해당 수명 주기는 모델을 배포하고 추론 단계에서 기능 데이터를 찾는 운영화 단계까지 계속됩니다. 기능 저장소에 대한 자세한 내용은 기능 저장소 개념 설명서를 참조하세요.
이 자습서에서는 프라이빗 엔드포인트를 통해 보안 수신을 구성하고 관리 가상 네트워크를 통해 송신을 보호하는 방법을 설명합니다.
자습서 1부에서는 사용자 지정 변환을 통해 기능 집합 사양을 만드는 방법을 보여주고 해당 기능 집합을 사용해 학습 데이터를 생성하는 방법을 보여주었습니다. 2부에서는 구체화 사용 설정과 백필 수행 방법을 보여주었습니다. 또한 2부에서는 모델 성능을 향상하는 수단으로 기능을 실험하는 방법을 보여주었습니다. 3부에서는 기능 저장소가 실험 및 학습 흐름에서 민첩성을 어떻게 향상시키는지 보여주었습니다. 또한 3부에서는 일괄 처리 추론을 실행하는 방법을 설명했습니다. 자습서 4에서는 온라인/실시간 유추 사용 사례에 기능 저장소를 사용하는 방법을 설명했습니다. 자습서 5에서는 사용자 지정 데이터 원본을 사용하여 기능 집합을 개발하는 방법을 설명했습니다. 자습서 6에서는 다음 작업을 수행하는 방법을 보여줍니다.
- 관리되는 네트워크 격리에서 네트워크 격리에 필요한 리소스 설정
- 새 기능 저장소 리소스 만들기
- 네트워크 격리 시나리오를 지원하도록 기능 저장소 설정
- 네트워크 격리 시나리오를 지원하도록 프로젝트 작업 영역(현재 작업 영역)을 업데이트
필수 조건
참고 항목
이 자습서에서는 서버리스 Spark Compute와 함께 Azure Machine Learning Notebook을 사용합니다.
서버리스 Spark 작업용 관리형 가상 네트워크에서 사용하도록 설정된 Azure Machine Learning 작업 영역입니다.
프로젝트 작업 영역을 구성하려면 다음을 수행합니다.
이름이
network.yml
인 YAML 파일을 만듭니다.managed_network: isolation_mode: allow_internet_outbound
다음 명령을 실행하여 작업 영역을 업데이트하고 서버리스 Spark 작업용 관리형 가상 네트워크를 프로비전합니다.
az ml workspace update --file network.yml --resource-group my_resource_group --name my_workspace_name az ml workspace provision-network --resource-group my_resource_group --name my_workspace_name --include-spark
자세한 내용은 서버리스 Spark 작업 구성을 참조하세요.
사용자 계정에는 기능 저장소를 만드는 리소스 그룹에 할당된
Owner
역할 또는Contributor
역할이 있어야 합니다. 사용자 계정에도User Access Administrator
역할이 필요합니다.
Important
Azure Machine Learning 작업 영역에서는 isolation_mode
를 allow_internet_outbound
로 설정합니다. 지원되는 유일한 네트워크 격리 모드입니다. 이 자습서에서는 프라이빗 엔드포인트를 통해 원본, 구체화 저장소, 관찰 데이터에 안전하게 연결하는 방법을 보여 줍니다.
설정
이 자습서에서는 Python 기능 저장소 Core SDK(azureml-featurestore
)를 사용합니다. Python SDK는 기능 집합 개발과 테스트에만 사용됩니다. CLI는 기능 저장소, 기능 집합, 기능 저장소 엔터티에 대한 CRUD(만들기, 읽기, 업데이트, 삭제) 작업에 사용됩니다. 이는 CLI/YAML이 기본 설정되는 CI/CD(연속 통합 및 지속적인 업데이트) 또는 GitOps 시나리오에 유용합니다.
이 자습서에서는 이러한 리소스를 명시적으로 설치할 필요가 없습니다. 여기에 표시된 설정 지침에서 conda.yaml
파일이 이를 다루기 때문입니다.
개발을 위해 Notebook 환경을 준비하려면 다음을 수행합니다.
다음 명령을 사용하여 azureml-examples 리포지토리를 로컬 GitHub 리소스에 복제합니다.
git clone --depth 1 https://github.com/Azure/azureml-examples
azureml-examples 리포지토리에서 zip 파일을 다운로드할 수도 있습니다. 이 페이지에서 먼저
code
드롭다운을 선택한 다음,Download ZIP
을 선택합니다. 이어서, 로컬 디바이스의 폴더에 콘텐츠 압축을 풉니다.프로젝트 작업 영역에 기능 저장소 샘플 디렉터리 업로드
- Azure Machine Learning 작업 영역에서 Azure Machine Learning 스튜디오 UI를 엽니다.
- 왼쪽 탐색 패널에서 Notebook을 선택합니다.
- 디렉터리 목록에서 사용자 이름을 선택합니다.
- 말줄임표(...)를 선택한 다음 폴더 업로드를 선택합니다.
- 복제된 디렉터리 경로
azureml-examples/sdk/python/featurestore-sample
에서 기능 저장소 샘플 폴더를 선택합니다.
자습서 실행
옵션 1: 새 Notebook을 만들고 이 문서의 안내를 단계별로 실행합니다.
옵션 2: 기존 Notebook
featurestore_sample/notebooks/sdk_and_cli/network_isolation/Network-isolation-feature-store.ipynb
을 엽니다. 이 문서를 열어 둔 채 자세한 설명과 설명서 링크를 참조할 수 있습니다.- 위쪽 탐색의 컴퓨팅 드롭다운에서 서버리스 Spark Compute를 선택합니다. 이 작업은 1~2분 정도 걸릴 수 있습니다. 상단 상태 표시줄이 구성 세션을 표시할 때까지 기다립니다.
- 상단 상태 표시줄에서 세션 구성을 선택합니다.
- Python 패키지를 선택합니다.
- Conda 파일 업로드를 선택합니다.
- 로컬 디바이스에 있는
azureml-examples/sdk/python/featurestore-sample/project/env/conda.yml
파일을 선택합니다. - (선택 사항) 서버리스 Spark 클러스터 시작 시간을 줄이기 위해 세션 시간 제한(분 단위 유휴 시간)을 늘릴 수 있습니다.
이 코드 셀은 Spark 세션을 시작합니다. 모든 종속성을 설치하고 Spark 세션을 시작하는 데 약 10분이 소요됩니다.
# Run this cell to start the spark session (any code block will start the session ). This can take around 10 mins. print("start spark session")
샘플의 루트 디렉터리 설정
import os # Please update your alias below (or any custom directory you have uploaded the samples to). # You can find the name from the directory structure in the left navigation. root_dir = "./Users/<your_user_alias>/featurestore_sample" if os.path.isdir(root_dir): print("The folder exists.") else: print("The folder does not exist. Please create or fix the path")
Azure Machine Learning CLI 설정:
Azure Machine Learning CLI 확장 설치
# install azure ml cli extension !az extension add --name ml
인증
# authenticate !az login
기본 구독 설정
# Set default subscription import os subscription_id = os.environ["AZUREML_ARM_SUBSCRIPTION"] !az account set -s $subscription_id
참고 항목
기능 저장소 작업 영역은 프로젝트 간 기능 재사용을 지원합니다. 현재 사용 중인 작업 영역인 프로젝트 작업 영역은 특정 기능 저장소의 기능을 활용하여 모델을 학습시키고 추론합니다. 다수의 프로젝트 작업 영역이 동일한 기능 저장소 작업 영역을 공유하고 재사용할 수 있습니다.
필요 리소스 프로비전
ADLS(Azure Data Lake Storage) Gen2 스토리지 계정이나 컨테이너를 새로 만들거나 기능 저장소에 기존 스토리지 계정 또는 컨테이너 리소스를 다시 사용할 수 있습니다. 실제 상황에서는 서로 다른 스토리지 계정들이 ADLS Gen2 컨테이너를 호스트할 수 있습니다. 특정 요구 사항에 따라 두 옵션 모두 작동합니다.
이 자습서에서는 동일한 ADLS Gen2 스토리지 계정에 별도 스토리지 컨테이너를 세 개 만듭니다.
- 원본 데이터
- 오프라인 저장소
- 관찰 데이터
원본 데이터, 오프라인 저장소 및 관찰 데이터에 대한 ADLS Gen2 스토리지 계정을 만듭니다.
다음 코드 샘플에 Azure Data Lake Storage Gen2 스토리지 계정의 이름을 제공합니다. 제공된 기본 설정을 사용하여 다음 코드 셀을 실행할 수 있습니다. 선택적으로 기본 설정을 재정의할 수 있습니다.
## Default Setting # We use the subscription, resource group, region of this active project workspace, # We hard-coded default resource names for creating new resources ## Overwrite # You can replace them if you want to create the resources in a different subsciprtion/resourceGroup, or use existing resources # At the minimum, provide an ADLS Gen2 storage account name for `storage_account_name` storage_subscription_id = os.environ["AZUREML_ARM_SUBSCRIPTION"] storage_resource_group_name = os.environ["AZUREML_ARM_RESOURCEGROUP"] storage_account_name = "<STORAGE_ACCOUNT_NAME>" storage_location = "eastus" storage_file_system_name_offline_store = "offline-store" storage_file_system_name_source_data = "source-data" storage_file_system_name_observation_data = "observation-data"
이 코드 셀은 위의 코드 셀에 정의된 ADLS Gen2 스토리지 계정을 만듭니다.
# Create new storage account !az storage account create --name $storage_account_name --enable-hierarchical-namespace true --resource-group $storage_resource_group_name --location $storage_location --subscription $storage_subscription_id
이 코드 셀은 오프라인 저장소를 위한 새 스토리지 컨테이너를 만듭니다.
# Create a new storage container for offline store !az storage fs create --name $storage_file_system_name_offline_store --account-name $storage_account_name --subscription $storage_subscription_id
이 코드 셀은 원본 데이터용 스토리지 컨테이너를 새로 만듭니다.
# Create a new storage container for source data !az storage fs create --name $storage_file_system_name_source_data --account-name $storage_account_name --subscription $storage_subscription_id
이 코드 셀은 관찰 데이터용 스토리지 컨테이너를 새로 만듭니다.
# Create a new storage container for observation data !az storage fs create --name $storage_file_system_name_observation_data --account-name $storage_account_name --subscription $storage_subscription_id
이 자습서 시리즈에 필요한 샘플 데이터를 새로 만든 스토리지 컨테이너에 복사합니다.
스토리지 컨테이너에 데이터를 쓰려면 Contributor 역할과 Storage Blob Data Contributor 역할이 이 단계에 따라 Azure Portal에 만들어진 ADLS Gen2 스토리지 계정의 사용자 ID에 할당되었는지 확인합니다.
Important
Contributor 역할과 Storage Blob Data Contributor 역할을 사용자 ID에 할당했다면 다음 단계 전까지 권한이 전파되도록 몇 분 정도 기다립니다. 액세스 제어에 대한 자세한 내용은 Azure Storage 계정용 RBAC(역할 기반 액세스 제어)를 참조하세요.
다음 코드 셀은 이 자습서에서 사용된 트랜잭션 기능 집합의 샘플 원본 데이터를 공용 스토리지 계정에서 새로 만든 스토리지 계정으로 복사합니다.
# Copy sample source data for transactions feature set used in this tutorial series from the public storage account to the newly created storage account transactions_source_data_path = "wasbs://data@azuremlexampledata.blob.core.windows.net/feature-store-prp/datasources/transactions-source/*.parquet" transactions_src_df = spark.read.parquet(transactions_source_data_path) transactions_src_df.write.parquet( f"abfss://{storage_file_system_name_source_data}@{storage_account_name}.dfs.core.windows.net/transactions-source/" )
이 자습서에서 사용된 계정 기능 집합의 샘플 원본 데이터를 새로 만든 스토리지 계정으로 복사합니다.
# Copy sample source data for account feature set used in this tutorial series from the public storage account to the newly created storage account accounts_data_path = "wasbs://data@azuremlexampledata.blob.core.windows.net/feature-store-prp/datasources/accounts-precalculated/*.parquet" accounts_data_df = spark.read.parquet(accounts_data_path) accounts_data_df.write.parquet( f"abfss://{storage_file_system_name_source_data}@{storage_account_name}.dfs.core.windows.net/accounts-precalculated/" )
학습에 사용된 샘플 관찰 데이터를 공용 스토리지 계정에서 새로 만든 스토리지 계정으로 복사합니다.
# Copy sample observation data used for training from the public storage account to the newly created storage account observation_data_train_path = "wasbs://data@azuremlexampledata.blob.core.windows.net/feature-store-prp/observation_data/train/*.parquet" observation_data_train_df = spark.read.parquet(observation_data_train_path) observation_data_train_df.write.parquet( f"abfss://{storage_file_system_name_observation_data}@{storage_account_name}.dfs.core.windows.net/train/" )
배치 추론에 사용된 샘플 관찰 데이터를 공용 스토리지 계정에서 새로 만든 스토리지 계정으로 복사합니다.
# Copy sample observation data used for batch inference from a public storage account to the newly created storage account observation_data_inference_path = "wasbs://data@azuremlexampledata.blob.core.windows.net/feature-store-prp/observation_data/batch_inference/*.parquet" observation_data_inference_df = spark.read.parquet(observation_data_inference_path) observation_data_inference_df.write.parquet( f"abfss://{storage_file_system_name_observation_data}@{storage_account_name}.dfs.core.windows.net/batch_inference/" )
새로 만든 스토리지 계정에서 공용 네트워크 액세스를 사용하지 않도록 설정합니다.
이 코드 셀은 이전에 만든 ADLS Gen2 스토리지 계정에 공용 네트워크 액세스를 사용하지 않도록 설정합니다.
# Disable the public network access for the above created ADLS Gen2 storage account !az storage account update --name $storage_account_name --resource-group $storage_resource_group_name --subscription $storage_subscription_id --public-network-access disabled
오프라인 저장소, 원본 데이터, 관찰 데이터 컨테이너의 ARM ID를 설정합니다.
# set the container arm id offline_store_gen2_container_arm_id = "/subscriptions/{sub_id}/resourceGroups/{rg}/providers/Microsoft.Storage/storageAccounts/{account}/blobServices/default/containers/{container}".format( sub_id=storage_subscription_id, rg=storage_resource_group_name, account=storage_account_name, container=storage_file_system_name_offline_store, ) print(offline_store_gen2_container_arm_id) source_data_gen2_container_arm_id = "/subscriptions/{sub_id}/resourceGroups/{rg}/providers/Microsoft.Storage/storageAccounts/{account}/blobServices/default/containers/{container}".format( sub_id=storage_subscription_id, rg=storage_resource_group_name, account=storage_account_name, container=storage_file_system_name_source_data, ) print(source_data_gen2_container_arm_id) observation_data_gen2_container_arm_id = "/subscriptions/{sub_id}/resourceGroups/{rg}/providers/Microsoft.Storage/storageAccounts/{account}/blobServices/default/containers/{container}".format( sub_id=storage_subscription_id, rg=storage_resource_group_name, account=storage_account_name, container=storage_file_system_name_observation_data, ) print(observation_data_gen2_container_arm_id)
구체화를 사용하도록 설정된 기능 저장소 만들기
기능 저장소 매개 변수 설정
다음 코드 셀 샘플에 표시된 대로 기능 저장소 이름, 위치, 구독 ID, 그룹 이름, ARM ID 값을 설정합니다.
# We use the subscription, resource group, region of this active project workspace.
# Optionally, you can replace them to create the resources in a different subsciprtion/resourceGroup, or use existing resources
import os
# At the minimum, define a name for the feature store
featurestore_name = "<FEATURESTORE_NAME>"
# It is recommended to create featurestore in the same location as the storage
featurestore_location = storage_location
featurestore_subscription_id = os.environ["AZUREML_ARM_SUBSCRIPTION"]
featurestore_resource_group_name = os.environ["AZUREML_ARM_RESOURCEGROUP"]
feature_store_arm_id = "/subscriptions/{sub_id}/resourceGroups/{rg}/providers/Microsoft.MachineLearningServices/workspaces/{ws_name}".format(
sub_id=featurestore_subscription_id,
rg=featurestore_resource_group_name,
ws_name=featurestore_name,
)
이 코드 셀은 구체화를 사용하도록 설정된 기능 저장소에 대한 YAML 사양 파일을 생성합니다.
# The below code creates a feature store with enabled materialization
import yaml
config = {
"$schema": "http://azureml/sdk-2-0/FeatureStore.json",
"name": featurestore_name,
"location": featurestore_location,
"compute_runtime": {"spark_runtime_version": "3.3"},
"offline_store": {
"type": "azure_data_lake_gen2",
"target": offline_store_gen2_container_arm_id,
},
}
feature_store_yaml = root_dir + "/featurestore/featurestore_with_offline_setting.yaml"
with open(feature_store_yaml, "w") as outfile:
yaml.dump(config, outfile, default_flow_style=False)
기능 저장소를 만들기
이 코드 셀은 이전 단계에서 생성한 YAML 사양 파일을 사용하여 구체화를 사용하도록 설정된 기능 저장소를 만듭니다.
!az ml feature-store create --file $feature_store_yaml --subscription $featurestore_subscription_id --resource-group $featurestore_resource_group_name
Azure Machine Learning 기능 저장소 코어 SDK 클라이언트 초기화
이 셀에서 초기화된 SDK 클라이언트는 다음과 같이 기능의 개발과 사용을 용이하게 합니다.
# feature store client
from azureml.featurestore import FeatureStoreClient
from azure.ai.ml.identity import AzureMLOnBehalfOfCredential
featurestore = FeatureStoreClient(
credential=AzureMLOnBehalfOfCredential(),
subscription_id=featurestore_subscription_id,
resource_group_name=featurestore_resource_group_name,
name=featurestore_name,
)
기능 저장소의 사용자 ID에 역할 할당
다음 설명을 따라 내 사용자 ID에서 사용할 Microsoft Entra Object ID를 가져옵니다. 이어서 다음 명령에 Microsoft Entra Object ID를 사용해 만든 기능 저장소의 내 사용자 ID에 AzureML 데이터 과학자 역할을 할당합니다.
your_aad_objectid = "<YOUR_AAD_OBJECT_ID>"
!az role assignment create --role "AzureML Data Scientist" --assignee-object-id $your_aad_objectid --assignee-principal-type User --scope $feature_store_arm_id
기능 저장소의 기본 스토리지 계정과 키 자격 증명 모음을 가져오고 해당 리소스에 대한 공용 네트워크 액세스를 사용하지 않도록 설정합니다.
다음 코드 셀은 다음 단계를 위한 기능 저장소 개체를 반환합니다.
fs = featurestore.feature_stores.get()
이 코드 셀은 기능 저장소의 기본 스토리지 계정과 키 자격 증명 모음의 이름을 반환합니다.
# Copy the properties storage_account and key_vault from the response returned in feature store show command respectively
default_fs_storage_account_name = fs.storage_account.rsplit("/", 1)[-1]
default_key_vault_name = fs.key_vault.rsplit("/", 1)[-1]
이 코드 셀은 기능 저장소의 기본 스토리지 계정에 대한 공용 네트워크 액세스를 사용하지 않도록 설정합니다.
# Disable the public network access for the above created default ADLS Gen2 storage account for the feature store
!az storage account update --name $default_fs_storage_account_name --resource-group $featurestore_resource_group_name --subscription $featurestore_subscription_id --public-network-access disabled
다음 셀은 기능 저장소의 기본 키 자격 증명 모음 이름을 인쇄합니다.
print(default_key_vault_name)
이전에 만든 기본 기능 저장소 키 자격 증명 모음에 대한 공용 네트워크 액세스 사용 안 함
- Azure Portal에서 만든 이전 셀의 기본 키 자격 증명 모음을 엽니다.
- 네트워킹 탭을 선택합니다.
- 공용 액세스 해제를 설정한 다음 페이지 왼쪽 아래에 이전 셀의 적용을 선택합니다.
기능 저장소 작업 영역에 관리형 가상 네트워크 사용
필요한 아웃바운드 규칙으로 기능 저장소 업데이트
다음 코드 셀은 기능 저장소에 정의된 아웃바운드 규칙 관련 YAML 사양 파일을 만듭니다.
# The below code creates a configuration for managed virtual network for the feature store
import yaml
config = {
"public_network_access": "disabled",
"managed_network": {
"isolation_mode": "allow_internet_outbound",
"outbound_rules": [
# You need to add multiple rules here if you have separate storage account for source, observation data and offline store.
{
"name": "sourcerulefs",
"destination": {
"spark_enabled": "true",
"subresource_target": "dfs",
"service_resource_id": f"/subscriptions/{storage_subscription_id}/resourcegroups/{storage_resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
},
"type": "private_endpoint",
},
# This rule is added currently because serverless Spark doesn't automatically create a private endpoint to default key vault.
{
"name": "defaultkeyvault",
"destination": {
"spark_enabled": "true",
"subresource_target": "vault",
"service_resource_id": f"/subscriptions/{featurestore_subscription_id}/resourcegroups/{featurestore_resource_group_name}/providers/Microsoft.Keyvault/vaults/{default_key_vault_name}",
},
"type": "private_endpoint",
},
],
},
}
feature_store_managed_vnet_yaml = (
root_dir + "/featurestore/feature_store_managed_vnet_config.yaml"
)
with open(feature_store_managed_vnet_yaml, "w") as outfile:
yaml.dump(config, outfile, default_flow_style=False)
이 코드 셀은 생성된 YAML 사양 파일을 사용하여 기능 저장소를 업데이트합니다.
!az ml feature-store update --file $feature_store_managed_vnet_yaml --name $featurestore_name --resource-group $featurestore_resource_group_name
정의된 아웃바운드 규칙에 대한 프라이빗 엔드포인트 만들기
provision-network
명령은 구체화 작업이 원본, 오프라인 저장소, 관찰 데이터, 기본 스토리지 계정, 기능 저장소의 기본 키 자격 증명 모음으로 실행되는 관리형 가상 네트워크에서 프라이빗 엔드포인트를 만듭니다. 이 명령을 완료하는 데 20분 정도가 걸릴 수 있습니다.
#### Provision network to create necessary private endpoints (it may take approximately 20 minutes)
!az ml feature-store provision-network --name $featurestore_name --resource-group $featurestore_resource_group_name --include-spark
이 코드 셀은 아웃바운드 규칙으로 정의된 프라이빗 엔드포인트가 만들어졌는지 확인합니다.
### Check that managed virtual network is correctly enabled
### After provisioning the network, all the outbound rules should become active
### For this tutorial, you will see 6 outbound rules
!az ml feature-store show --name $featurestore_name --resource-group $featurestore_resource_group_name
프로젝트 작업 영역에 대한 관리형 가상 네트워크 업데이트
다음으로, 프로젝트 작업 영역에 관리형 가상 네트워크를 업데이트합니다. 먼저, 프로젝트 작업 영역의 구독 ID, 리소스 그룹, 작업 영역 이름을 가져옵니다.
# lookup the subscription id, resource group and workspace name of the current workspace
project_ws_sub_id = os.environ["AZUREML_ARM_SUBSCRIPTION"]
project_ws_rg = os.environ["AZUREML_ARM_RESOURCEGROUP"]
project_ws_name = os.environ["AZUREML_ARM_WORKSPACE_NAME"]
필요한 아웃바운드 규칙을 통해 프로젝트 작업 영역 업데이트
프로젝트 작업 영역에는 다음 리소스에 대한 액세스 권한이 필요합니다.
- 원본 데이터
- 오프라인 저장소
- 관찰 데이터
- 기능 저장소
- 기능 저장소의 기본 스토리지 계정
이 코드 셀은 필요한 아웃바운드 규칙을 통해 생성된 YAML 사양 파일을 사용하여 프로젝트 작업 영역을 업데이트합니다.
# The below code creates a configuration for managed virtual network for the project workspace
import yaml
config = {
"managed_network": {
"isolation_mode": "allow_internet_outbound",
"outbound_rules": [
# Incase you have separate storage accounts for source, observation data and offline store, you need to add multiple rules here. No action needed otherwise.
{
"name": "projectsourcerule",
"destination": {
"spark_enabled": "true",
"subresource_target": "dfs",
"service_resource_id": f"/subscriptions/{storage_subscription_id}/resourcegroups/{storage_resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}",
},
"type": "private_endpoint",
},
# Rule to create private endpoint to default storage of feature store
{
"name": "defaultfsstoragerule",
"destination": {
"spark_enabled": "true",
"subresource_target": "blob",
"service_resource_id": f"/subscriptions/{featurestore_subscription_id}/resourcegroups/{featurestore_resource_group_name}/providers/Microsoft.Storage/storageAccounts/{default_fs_storage_account_name}",
},
"type": "private_endpoint",
},
# Rule to create private endpoint to default key vault of feature store
{
"name": "defaultfskeyvaultrule",
"destination": {
"spark_enabled": "true",
"subresource_target": "vault",
"service_resource_id": f"/subscriptions/{featurestore_subscription_id}/resourcegroups/{featurestore_resource_group_name}/providers/Microsoft.Keyvault/vaults/{default_key_vault_name}",
},
"type": "private_endpoint",
},
# Rule to create private endpoint to feature store
{
"name": "featurestorerule",
"destination": {
"spark_enabled": "true",
"subresource_target": "amlworkspace",
"service_resource_id": f"/subscriptions/{featurestore_subscription_id}/resourcegroups/{featurestore_resource_group_name}/providers/Microsoft.MachineLearningServices/workspaces/{featurestore_name}",
},
"type": "private_endpoint",
},
],
}
}
project_ws_managed_vnet_yaml = (
root_dir + "/featurestore/project_ws_managed_vnet_config.yaml"
)
with open(project_ws_managed_vnet_yaml, "w") as outfile:
yaml.dump(config, outfile, default_flow_style=False)
이 코드 셀은 아웃바운드 규칙을 통해 생성된 YAML 사양 파일을 사용하여 프로젝트 작업 영역을 업데이트합니다.
#### Update project workspace to create private endpoints for the defined outbound rules (it may take approximately 15 minutes)
!az ml workspace update --file $project_ws_managed_vnet_yaml --name $project_ws_name --resource-group $project_ws_rg
이 코드 셀은 아웃바운드 규칙으로 정의된 프라이빗 엔드포인트가 만들어졌는지 확인합니다.
!az ml workspace show --name $project_ws_name --resource-group $project_ws_rg
Azure Portal의 아웃바운드 규칙을 확인할 수도 있습니다. 프로젝트 작업 영역의 왼쪽 탐색 패널에서 네트워킹으로 이동한 다음 작업 영역 관리형 아웃바운드 액세스 탭을 엽니다.
트랜잭션 롤링 집계 기능 집합 프로토타입 및 개발
트랜잭션 원본 데이터 탐색
참고 항목
공개적으로 액세스할 수 있는 Blob 컨테이너가 이 자습서에 사용된 샘플 데이터를 호스트합니다. wasbs
드라이버를 통해 Spark에서만 읽을 수 있습니다. 고유 원본 데이터를 사용하여 기능 집합을 만들 때는 ADLS Gen2 계정에 호스트하고 데이터 경로에서 abfss
드라이버를 사용하세요.
# remove the "." in the root directory path as we need to generate absolute path to read from Spark
transactions_source_data_path = f"abfss://{storage_file_system_name_source_data}@{storage_account_name}.dfs.core.windows.net/transactions-source/*.parquet"
transactions_src_df = spark.read.parquet(transactions_source_data_path)
display(transactions_src_df.head(5))
# Note: display(training_df.head(5)) displays the timestamp column in a different format. You can can call transactions_src_df.show() to see correctly formatted value
트랜잭션 기능 집합을 로컬로 개발
기능 집합 사양은 로컬에서 개발하고 테스트할 수 있는 기능 집합의 독립적인 정의입니다.
다음 롤링 창 집계 기능을 만듭니다.
- 3일 단위 트랜잭션 수
- 3일 단위 트랜잭션 합계
- 3일 단위 트랜잭션 평균
- 7일 단위 트랜잭션 수
- 7일 단위 트랜잭션 합계
- 7일 단위 트랜잭션 평균
기능 변환 코드 파일 featurestore/featuresets/transactions/spec/transformation_code/transaction_transform.py
를 검사합니다. 이 Spark 변환기는 기능에 대해 정의된 롤링 집계를 수행합니다.
기능 집합 및 변환을 자세히 이해하려면 기능 저장소 개념을 참조하세요.
from azureml.featurestore import create_feature_set_spec, FeatureSetSpec
from azureml.featurestore.contracts import (
DateTimeOffset,
FeatureSource,
TransformationCode,
Column,
ColumnType,
SourceType,
TimestampColumn,
)
transactions_featureset_code_path = (
root_dir + "/featurestore/featuresets/transactions/transformation_code"
)
transactions_featureset_spec = create_feature_set_spec(
source=FeatureSource(
type=SourceType.parquet,
path=f"abfss://{storage_file_system_name_source_data}@{storage_account_name}.dfs.core.windows.net/transactions-source/*.parquet",
timestamp_column=TimestampColumn(name="timestamp"),
source_delay=DateTimeOffset(days=0, hours=0, minutes=20),
),
transformation_code=TransformationCode(
path=transactions_featureset_code_path,
transformer_class="transaction_transform.TransactionFeatureTransformer",
),
index_columns=[Column(name="accountID", type=ColumnType.string)],
source_lookback=DateTimeOffset(days=7, hours=0, minutes=0),
temporal_join_lookback=DateTimeOffset(days=1, hours=0, minutes=0),
infer_schema=True,
)
# Generate a spark dataframe from the feature set specification
transactions_fset_df = transactions_featureset_spec.to_spark_dataframe()
# display few records
display(transactions_fset_df.head(5))
기능 집합 사양 내보내기
기능 저장소에 기능 집합 사양을 등록하려면 해당 사양을 특정 형식으로 저장해야 합니다.
생성된 트랜잭션 기능 집합 사양을 검사하려면 파일 트리에서 이 파일을 열어 다음 사양을 확인합니다.
featurestore/featuresets/accounts/spec/FeaturesetSpec.yaml
사양에는 다음 요소가 포함됩니다.
source
: 스토리지 리소스에 대한 참조 - 지금의 경우에는 Blob Storage 리소스의 parquet 파일features
: 기능 및 해당 기능의 데이터 형식 목록 변환 코드를 입력하는 경우index_columns
: 기능 집합의 값에 액세스하는 데 필요한 조인 키
기능 집합 사양을 YAML 파일로 유지하여 얻어지는 또 다른 이점으로, 사양을 버전 단위로 제어할 수 있습니다. 기능 집합 사양에 대한 자세한 내용은 최상위 기능 저장소 엔터티 설명서와 기능 집합 사양 YAML 참조를 참조합니다.
import os
# create a new folder to dump the feature set specification
transactions_featureset_spec_folder = (
root_dir + "/featurestore/featuresets/transactions/spec"
)
# check if the folder exists, create one if not
if not os.path.exists(transactions_featureset_spec_folder):
os.makedirs(transactions_featureset_spec_folder)
transactions_featureset_spec.dump(transactions_featureset_spec_folder, overwrite=True)
기능 저장소 엔터티 등록
엔터티는 동일한 논리적 엔터티를 사용하는 기능 집합 전체에서 동일한 조인 키 정의의 사용을 강제하는 데 도움이 됩니다. 계정 엔터티, 고객 엔터티 등을 엔터티 예시로 볼 수 있습니다. 일반적으로, 엔터티는 한 번 만들어지면 기능 집합 전체에서 재사용됩니다. 자세한 내용은 최상위 기능 저장소 엔터티 설명서를 참조합니다.
이 코드 셀은 기능 저장소의 계정 엔터티를 만듭니다.
account_entity_path = root_dir + "/featurestore/entities/account.yaml"
!az ml feature-store-entity create --file $account_entity_path --resource-group $featurestore_resource_group_name --workspace-name $featurestore_name
기능 저장소에 트랜잭션 기능 집합을 등록하고 구체화 작업을 제출합니다.
기능 집합 자산을 공유하고 다시 사용하려면 먼저 해당 자산을 기능 저장소에 등록해야 합니다. 기능 집합 자산을 등록하면 버전 관리 및 구체화를 포함한 관리 기능이 제공됩니다. 이 자습서 시리즈에서는 이러한 항목을 다룹니다.
기능 집합 자산은 이전에 만든 기능 집합 사양과 버전 및 구체화 설정 등의 기타 속성을 모두 참조합니다.
기능 집합 만들기
다음 코드 셀은 미리 정의된 YAML 사양 파일을 사용하여 기능 집합을 만듭니다.
transactions_featureset_path = (
root_dir
+ "/featurestore/featuresets/transactions/featureset_asset_offline_enabled.yaml"
)
!az ml feature-set create --file $transactions_featureset_path --resource-group $featurestore_resource_group_name --workspace-name $featurestore_name
이 코드 셀은 새로 만든 기능 집합을 미리 봅니다.
# Preview the newly created feature set
!az ml feature-set show --resource-group $featurestore_resource_group_name --workspace-name $featurestore_name -n transactions -v 1
백필 구체화 작업 제출
다음 코드 셀은 기능 구체화 창의 시작 및 종료 시간 값을 정의하고 백필 구체화 작업을 제출합니다.
feature_window_start_time = "2023-02-01T00:00.000Z"
feature_window_end_time = "2023-03-01T00:00.000Z"
!az ml feature-set backfill --name transactions --version 1 --by-data-status "['None']" --workspace-name $featurestore_name --resource-group $featurestore_resource_group_name --feature-window-start-time $feature_window_start_time --feature-window-end-time $feature_window_end_time
이 코드 셀은 <JOB_ID_FROM_PREVIOUS_COMMAND>
을(를) 제공하여 백필 구체화 작업 상태를 확인합니다.
### Check the job status
!az ml job show --name <JOB_ID_FROM_PREVIOUS_COMMAND> -g $featurestore_resource_group_name -w $featurestore_name
이 코드 셀은 현재 기능 집합의 구체화 작업을 모두 목록으로 보여줍니다.
### List all the materialization jobs for the current feature set
!az ml feature-set list-materialization-operation --name transactions --version 1 -g $featurestore_resource_group_name -w $featurestore_name
Azure Cache for Redis를 온라인 저장소로 연결
Azure Cache for Redis 만들기
다음 코드 셀에서 새로 만들거나 재사용할 Azure Cache for Redis 이름을 정의합니다. 선택적으로 기타 기본 설정을 재정의할 수 있습니다.
redis_subscription_id = os.environ["AZUREML_ARM_SUBSCRIPTION"]
redis_resource_group_name = os.environ["AZUREML_ARM_RESOURCEGROUP"]
redis_name = "my-redis"
redis_location = storage_location
Redis 캐시 계층(기본이나 표준 또는 프리미엄)을 선택할 수 있습니다. 선택한 캐시 계층에 사용할 수 있는 SKU 제품군을 선택해야 합니다. 다양한 계층을 선택하는 것이 캐시 성능에 미치는 영향에 대한 자세한 내용은 이 설명서 리소스를 참조하세요. Azure Cache for Redis의 다양한 SKU 계층 및 제품군에 대한 자세한 가격 책정 관련 내용은 이 설명서 리소스를 참조하세요.
다음 코드 셀을 실행하여 프리미엄 계층, SKU 제품군 P
및 캐시 용량 2로 Azure Cache for Redis를 만듭니다. Redis 인스턴스를 프로비전하는 데 약 5-10분이 걸릴 수 있습니다.
# Create new redis cache
from azure.mgmt.redis import RedisManagementClient
from azure.mgmt.redis.models import RedisCreateParameters, Sku, SkuFamily, SkuName
management_client = RedisManagementClient(
AzureMLOnBehalfOfCredential(), redis_subscription_id
)
# It usually takes about 5 - 10 min to finish the provision of the Redis instance.
# If the following begin_create() call still hangs for longer than that,
# please check the status of the Redis instance on the Azure portal and cancel the cell if the provision has completed.
# This sample uses a PREMIUM tier Redis SKU from family P, which may cost more than a STANDARD tier SKU from family C.
# Please choose the SKU tier and family according to your performance and pricing requirements.
redis_arm_id = (
management_client.redis.begin_create(
resource_group_name=redis_resource_group_name,
name=redis_name,
parameters=RedisCreateParameters(
location=redis_location,
sku=Sku(name=SkuName.PREMIUM, family=SkuFamily.P, capacity=2),
public_network_access="Disabled", # can only disable PNA to redis cache during creation
),
)
.result()
.id
)
print(redis_arm_id)
온라인 스토어를 사용하여 기능 저장소 업데이트
Azure Cache for Redis를 온라인 구체화 저장소로 사용하려면 기능 저장소에 연결해야 합니다. 다음 코드 셀은 기능 저장소에 정의된 온라인 저장소 아웃바운드 규칙을 통해 YAML 사양 파일을 만듭니다.
# The following code cell creates a YAML specification file for outbound rules that are defined for the feature store.
## rule 1: PE to online store (redis cache): this is optional if online store is not used
import yaml
config = {
"public_network_access": "disabled",
"managed_network": {
"isolation_mode": "allow_internet_outbound",
"outbound_rules": [
{
"name": "sourceruleredis",
"destination": {
"spark_enabled": "true",
"subresource_target": "redisCache",
"service_resource_id": f"/subscriptions/{storage_subscription_id}/resourcegroups/{storage_resource_group_name}/providers/Microsoft.Cache/Redis/{redis_name}",
},
"type": "private_endpoint",
},
],
},
"online_store": {"target": f"{redis_arm_id}", "type": "redis"},
}
feature_store_managed_vnet_yaml = (
root_dir + "/featurestore/feature_store_managed_vnet_config.yaml"
)
with open(feature_store_managed_vnet_yaml, "w") as outfile:
yaml.dump(config, outfile, default_flow_style=False)
다음 코드 셀은 온라인 저장소용 아웃바운드 규칙을 통해 생성된 YAML 사양 파일을 사용하여 기능 저장소를 업데이트합니다.
!az ml feature-store update --file $feature_store_managed_vnet_yaml --name $featurestore_name --resource-group $featurestore_resource_group_name
프로젝트 작업 영역 아웃바운드 규칙 업데이트
프로젝트 작업 영역에는 온라인 저장소에 대한 액세스 권한이 필요합니다. 다음 코드 셀은 프로젝트 작업 영역과 관련해 필요한 아웃바운드 규칙을 통해 YAML 사양 파일을 만듭니다.
import yaml
config = {
"managed_network": {
"isolation_mode": "allow_internet_outbound",
"outbound_rules": [
{
"name": "onlineruleredis",
"destination": {
"spark_enabled": "true",
"subresource_target": "redisCache",
"service_resource_id": f"/subscriptions/{storage_subscription_id}/resourcegroups/{storage_resource_group_name}/providers/Microsoft.Cache/Redis/{redis_name}",
},
"type": "private_endpoint",
},
],
}
}
project_ws_managed_vnet_yaml = (
root_dir + "/featurestore/project_ws_managed_vnet_config.yaml"
)
with open(project_ws_managed_vnet_yaml, "w") as outfile:
yaml.dump(config, outfile, default_flow_style=False)
다음 코드 셀을 실행하면 온라인 저장소용 아웃바운드 규칙을 통해 생성된 YAML 사양 파일을 사용하여 프로젝트 작업 영역을 업데이트합니다.
#### Update project workspace to create private endpoints for the defined outbound rules (it may take approximately 15 minutes)
!az ml workspace update --file $project_ws_managed_vnet_yaml --name $project_ws_name --resource-group $project_ws_rg
온라인 저장소로 설정된 트랜잭션 기능 구체화
다음 코드 셀을 사용하면 transactions
기능 집합에 대한 온라인 구체화가 가능합니다.
# Update featureset to enable online materialization
transactions_featureset_path = (
root_dir
+ "/featurestore/featuresets/transactions/featureset_asset_online_enabled.yaml"
)
!az ml feature-set update --file $transactions_featureset_path --resource-group $featurestore_resource_group_name --workspace-name $featurestore_name
다음 코드 셀은 기능 구체화 창의 시작 및 종료 시간을 정의하고 백필 구체화 작업을 제출합니다.
feature_window_start_time = "2024-01-24T00:00.000Z"
feature_window_end_time = "2024-01-25T00:00.000Z"
!az ml feature-set backfill --name transactions --version 1 --by-data-status "['None']" --feature-window-start-time $feature_window_start_time --feature-window-end-time $feature_window_end_time --feature-store-name $featurestore_name --resource-group $featurestore_resource_group_name
등록된 기능을 사용하여 학습 데이터 생성
관측 데이터 로드
먼저 관찰 데이터를 탐색합니다. 관찰 데이터에는 일반적으로 학습 및 유추에 사용되는 코어 데이터가 포함됩니다. 그런 다음 해당 데이터를 기능 데이터와 조인하여 전체 학습 데이터 리소스를 만듭니다. 관찰 데이터는 이벤트 시간 동안 캡처된 데이터입니다. 지금과 같은 경우, 트랜잭션 ID, 계정 ID, 트랜잭션 총액을 포함한 코어 트랜잭션 데이터가 있습니다. 여기서는 관찰 데이터가 학습에 사용되므로 대상 변수도 추가됩니다(is_fraud
).
observation_data_path = f"abfss://{storage_file_system_name_observation_data}@{storage_account_name}.dfs.core.windows.net/train/*.parquet"
observation_data_df = spark.read.parquet(observation_data_path)
obs_data_timestamp_column = "timestamp"
display(observation_data_df)
# Note: the timestamp column is displayed in a different format. Optionally, you can can call training_df.show() to see correctly formatted value
등록된 기능 집합을 가져오고 해당 기능을 목록으로 표시
다음으로, 기능 집합의 이름과 버전을 제공하여 해당 집합을 가져와 그 기능 집합의 기능을 나열합니다. 또한, 일부 샘플 기능 값을 인쇄합니다.
# look up the featureset by providing name and version
transactions_featureset = featurestore.feature_sets.get("transactions", "1")
# list its features
transactions_featureset.features
# print sample values
display(transactions_featureset.to_spark_dataframe().head(5))
기능 선택 및 학습 데이터 생성
학습 데이터 관련 기능을 선택하고 기능 저장소 SDK를 사용하여 학습 데이터를 생성합니다.
from azureml.featurestore import get_offline_features
# you can select features in pythonic way
features = [
transactions_featureset.get_feature("transaction_amount_7d_sum"),
transactions_featureset.get_feature("transaction_amount_7d_avg"),
]
# you can also specify features in string form: featurestore:featureset:version:feature
more_features = [
"transactions:1:transaction_3d_count",
"transactions:1:transaction_amount_3d_avg",
]
more_features = featurestore.resolve_feature_uri(more_features)
features.extend(more_features)
# generate training dataframe by using feature data and observation data
training_df = get_offline_features(
features=features,
observation_data=observation_data_df,
timestamp_column=obs_data_timestamp_column,
)
# Ignore the message that says feature set is not materialized (materialization is optional). We will enable materialization in the next part of the tutorial.
display(training_df)
# Note: the timestamp column is displayed in a different format. Optionally, you can can call training_df.show() to see correctly formatted value
특정 시점 조인이 해당 기능을 학습 데이터에 추가했습니다.
선택 사항으로서의 다음 단계
이제 보안 기능 저장소를 제대로 만들어 구체화 실행을 제출했으므로 자습서 시리즈를 통해 기능 저장소에 대한 이해를 구축할 수 있습니다.
이 자습서에는 이 시리즈의 자습서 1부와 2부의 단계가 혼재되어 있습니다. 네트워크 격리를 위해 다른 자습서 Notebook에 사용한 필수 공용 스토리지 컨테이너를 이 자습서 Notebook에서 만든 컨테이너로 바꿔야 합니다.
이것으로 자습서를 마칩니다. 학습 데이터는 기능 저장소의 기능을 사용합니다. 이를 나중에 사용할 수 있도록 스토리지에 저장하거나 여기에 모델 학습을 직접 실행할 수 있습니다.