Complexity Constraints for POW operator #721
Unanswered
ElizaMay505
asked this question in
Q&A
Replies: 1 comment
-
You could do this with a custom loss function (see https://astroautomata.com/PySR/api/ loss_function = """
function my_loss(tree, dataset::Dataset{T,L}, options)::L where {T,L}
prediction, flag = eval_tree_array(tree, dataset.X, options)
if !flag
return L(Inf) # NaN or Inf detected => infinite penalty!
end
pow_index = 1 # CHANGE ME (1 is if ^ appears first in binary_operators, 2 is second, ...)
non_constant_exponent = any(tree) do node
is_pow_node = node.degree == 2 && node.op == pow_index
if is_pow_node
any(node) do node2
node2.degree == 0 && !node2.constant
end
else
false
end
end
if non_constant_exponent
return L(100_000) # Return large penalty (but not as big as for NaN/Inf which are worse)
end
return sum((prediction .- dataset.y) .^ 2) / dataset.n
end
""" Some of these operations are described with examples here: https://symbolicml.org/DynamicExpressions.jl/dev/examples/base_operations/ (There are some other examples similar to this scattered throughout the discussions if wanting to try adding some other things). Hope this helps! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would like to restrict the pow operator to only allow constants in the exponent. I am trying to restrict my model to only using x^2, x^3, x^4, ..., x^n, but I have not been able to figure out how to restrict the complexity in this way. Restricting the complexity of the exponent to 1 still allows there to be variables in the exponent. I have tried this: constraints={"pow":(1,1)}, but even that allows more freedom than what I want. Does anyone know of a way to achieve what I want? My current solution is creating custom functions for each power of x, but this is proving to be very inefficient.
Beta Was this translation helpful? Give feedback.
All reactions