I’m using the macro @hyperopt
from package hyperopt.jl to optimize some hyperparameters for a machine learning model. However, I’m pretty constrained by the syntax of @hyperopt
, which is designed to be used as follows:
hoRes = @hyperopt for i=numOfSamples, sampler=someSampler,
hPar1 = valRange1,
hPar2 = valRange2,
...
hParN = valRangeN
modelFunc([hPar1, ..., hParN])
end
I want to wrap the above code in a function that can take a non-fixed number (N
) of hyperparameters as a Vector
argument as I’m testing many different variants of my model. However, due to the macro formalism, I cannot find a straightforward way to achieve this without explicitly writing out the N
functions, each corresponding to optimizing a specific number (1 <= I <= N
) of hyperparameters.
I believe there is a way to write a generated function (or a macro), genFunc
, that directly modifies the code for the for
loop when I specify the number of hyperparameters as input. It probably should look like something as below:
function wrapperOpt(::Val{N}, pars, valRanges, numOfSamples, someSampler) where {N}
# N == length(pars) == length(valPranges)
f = genFunc(Val(N), modelFunc, numOfSamples, someSampler)
f(pars, valRanges)
end
where genFunc
modifies the code involving @hyperopt
and the required for
loop.
However, there seems not to be an example of how to generate a code of for loop in the official documentation (especially with multiple condition expressions). And I don’t have much experience with metaprogramming either. If someone could help me out, I would much appreciate it! Thank you!