使用超参数 - 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)
注意

有关如何将输入数据和设备 arn 等信息传递到混合作业脚本的更多信息,请参阅此 github 页面

QAOA 在 Amazon Braket Hybrid Jobs 和 PennyLane Amazon Braket Hybrid Job s 教程提供了几个对学习如何使用超参数非常有用的指南,这些指南对学习如何使用超参数非常有用。