使用超參數 - Amazon Braket

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

使用超參數

您可以在建立混合任務時定義演算法所需的超參數,例如學習速率或步進大小。超參數值通常用於控制演算法的各個層面,並且通常可以進行調校以最佳化演算法的效能。若要在 Braket 混合任務中使用超參數,您需要將其名稱和值明確指定為字典。請注意,這些值必須是字串資料類型。您可以在搜尋最佳值集時指定要測試的超參數值。使用超參數的第一步是將超參數設定為字典並將其定義為字典,如下列程式碼所示:

# Defining the number of qubits used n_qubits = 8 # Defining the number of layers used n_layers = 10 # Defining the number of iterations used for your optimization algorithm n_iterations = 10 hyperparams = { "n_qubits": n_qubits, "n_layers": n_layers, "n_iterations": n_iterations }

然後,您將傳遞上述程式碼片段中定義的超參數,以便在您選擇的演算法中使用,如下所示:

import time from braket.aws import AwsQuantumJob # Name your job so that it can be later identified job_name = f"qcbm-gaussian-training-{n_qubits}-{n_layers}-" + str(int(time.time())) job = AwsQuantumJob.create( # Run this hybrid job on the SV1 simulator device="arn:aws:braket:::device/quantum-simulator/amazon/sv1", # The directory or single file containing the code to run. source_module="qcbm", # The main script or function the job will run. entry_point="qcbm.qcbm_job:main", # Set the job_name job_name=job_name, # Set the hyperparameters hyperparameters=hyperparams, # Define the file that contains the input data input_data="data.npy", # Or input_data=s3_path # wait_until_complete=False, )
注意

若要進一步了解輸入資料,請參閱輸入區段。

然後,超參數會使用下列程式碼載入混合任務指令碼:

import json import os # Load the Hybrid Job hyperparameters hp_file = os.environ["AMZN_BRAKET_HP_FILE"] with open(hp_file, "r") as f: hyperparams = json.load(f)
注意

如需如何將輸入資料和裝置等資訊傳遞至混合式任務指令碼的詳細資訊,請參閱此 github 頁面

Amazon Braket Hybrid Jobs 教學課程中,QAOA 會針對如何使用超參數,以及 PennyLane 和 Quantum Machine Learning Amazon Braket 提供幾個非常實用的指南。