Update scripts/NNDy.py

first extended commenting
This commit is contained in:
castaneda 2025-03-21 14:43:18 +01:00
parent c9bba34ba6
commit 48d73f6811

View File

@ -13,38 +13,52 @@ import NNDy_Interface
if __name__ == '__main__': if __name__ == '__main__':
#indicate name of the sequence to be optimized, to be found in {routine_name}.py #indicate name of the sequence to be optimized as {routine_name}.py
routine_name = 'TestSetup' #no need to explicit the whole path since the sequence will be looked for in the 'shared_drive' of labscript
#indicate name of the DA file to be run by lyse, {cost_model}.py routine_name = ''
cost_model = 'TestDA' #indicate complete path to the DA file that contains the cost function definition, e.g. ${THIS_FOLDER}$/{cost_model}.py
cost_model = ''
#see /DA/TestDA.py for instructions on how to write DA file
#HALTING CONDITIONS # $$$ HALTING CONDITIONS $$$
#these 3 halting conditions can be specified in any combination or subset of minimum one of them
#mute the ones that you don't use both here and in the passing to the interface below
#indicate maximum number of runs #indicate maximum number of runs
#max_num_runs = 10 max_num_runs =
#or one can also indicate max_num_runs_without_better_params #indicate max_num_runs_without_better_params
max_num_runs_without_better_params = 50 max_num_runs_without_better_params =
#indicate target cost #indicate target cost
#target_cost = 0 target_cost =
#FIXED GLOBAL VARIABLES # $$$ FIXED GLOBAL VARIABLES $$$
#indicate the values of the global variables that won't be optimized #it's possible to set from here the values of some or all global variables (already defined in runmanager - see runmanager documentation for more details) that will NOT be optimized
globalPar = {'T_wlm': 60, #globalPar must be a dictionary
'buffer_time': 10} #setting them here will override the values set in runmanager (remember that runmanager, blacs and lyse must be open and correctly setup before you can run this script -
# not necessary if they're already set on runmanager #it's good practice to execute the sequence from runmanager and check the returns of the DA file before running NNDy)
#if no value is to be set from here globalPar must be an empty dictionary
globalPar = {}
#INPUT PARAMETERS # $$$ INPUT PARAMETERS $$$
#indicate the initial values of the global variables to be optimized, in the following called input parameters #indicate variables to be optimized, in the following we will call them "input parameters"
#as a dictionary #input parameters are to be chosen among the global variables of the sequence
inputPar = [1e6, 2.5, 2] #indicate in the following the names, in a list
num_params = len(inputPar) inputPar_names = []
inputPar_names = ['delta_freq', 'carrier_amp', 'wait_AWG' ]
#indicate range of input parameters as two dictionaries of length num_params num_params = len(inputPar_names)
min_boundary = [-20e6, 0.01, 0] #also listed and in the same order indicate the initial values of the parameters
#such values should be checked before, so that they don't map into a sparse region of the parameter space, i.e. they should produce a good signal already
# even better if they're "human optimized" values
inputPar = []
max_boundary = [20e6, 4.5, 10]
#indicate range of input parameters as two lists of length num_params
#it's recommended to set here the hardware limit for each parameter, and eventually imposing a trust region - see below - to limit the search of the optimizer
min_boundary = []
max_boundary = []
hyperpar = { hyperpar = {
@ -54,30 +68,35 @@ if __name__ == '__main__':
#your job here is almost done...
#the interface is created recalling the object defined in the NNDy_Interface file
interface = NNDy_Interface.NNDy_Interface(routine_name, cost_model, hyperpar) interface = NNDy_Interface.NNDy_Interface(routine_name, cost_model, hyperpar)
#then the controller is fed with the settings defined above
controller = mlc.create_controller(interface, controller = mlc.create_controller(interface,
controller_type = 'neural_net', controller_type = 'neural_net',
#HALTING CONDITIONS
#max_num_runs = max_num_runs, #HALTING CONDITIONS, select as needed by commenting
max_num_runs = max_num_runs,
max_num_runs_without_better_params = max_num_runs_without_better_params, max_num_runs_without_better_params = max_num_runs_without_better_params,
#target_cost = target_cost, target_cost = target_cost,
#INPUT PARAMETERS #INPUT PARAMETERS
num_params = num_params, num_params = num_params,
#mloop handles the variables as python arrays not as dictionaries so when passing the parameters they're converted into named lists
min_boundary = min_boundary, max_boundary = max_boundary, min_boundary = min_boundary, max_boundary = max_boundary,
first_params = inputPar, first_params = inputPar,
param_names = inputPar_names, param_names = inputPar_names,
#other settings #other settings
#%of allowed variation from current best parameters found # %of allowed variation (from 0 to 1) - wrt each parameter range - from current best parameters found, limits the exploration around the current global minimum of the cost function
trust_region = 0.5, trust_region = ,
#output parameters over which cost is computed are noisy quantities #output parameters over which cost is computed are noisy quantities
cost_has_noise = True, cost_has_noise = True,
#if False, waits for the experiment to be performed every time so that every new optimization iteration trains on an enlarged training set #if False, waits for the experiment to be performed every time so that every new optimization iteration trains on an enlarged training set
no_delay = False) no_delay = False)
#for other possible settings for the optimizer see documentation https://m-loop.readthedocs.io/en/latest/tutorials.html #for other possible settings for the optimizer see documentation https://m-loop.readthedocs.io/en/latest/tutorials.html
#To run M-LOOP and find the optimal parameters just use the controller method optimize #To run M-LOOP and find the optimal parameters just use the controller method optimize
controller.optimize() controller.optimize()