Python variables in julia custom loss function #444
-
Hi, I am trying to create a custom loss function based on lower and upper bounds of the dataset. I have these bounds in a python variable, but I am not sure how to use this variable in the julia loss function or whether it would be even possible. My current implementation (with some omissions) is as follows:
The parameters I want to use in the custom loss function are lower_bounds and upper_bounds. Any help would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Hi @AndrewHutani, This is definitely possible. However To get your Python lower and upper bounds into the loss, I would do something with string interpolation and just put them into the string like: full_objective=f"""
function eval_loss(tree, dataset, options)
lower_bounds = [{", ".join(map(str, np.min(y, axis=0)))}]
upper_bounds = [{", ".join(map(str, np.max(y, axis=0)))}]
prediction, flag = eval_tree_array(tree, dataset.X, options)
if !flag
return eltype(dataset.X)(Inf)
end
return sum((prediction .- dataset.y) .^ 2) / dataset.n
end
""" (But modifying the final line with the Cheers! |
Beta Was this translation helpful? Give feedback.
Hi @AndrewHutani,
This is definitely possible. However
loss
expects elementwise loss only (e.g.,loss="my_loss(prediction, target) = (prediction - target)^2"
for a single scalarprediction
and single scalartarget
). Instead you should usefull_objective
. The example of this is here: https://astroautomata.com/PySR/api/#the-objective. Do you want to try with that and see if you can get it to work?To get your Python lower and upper bounds into the loss, I would do something with string interpolation and just put them into the string like: