Dimensional constraints in custom 'loss_function_expression' #972
Replies: 1 comment 1 reply
-
|
The way it works internally is in if regularization
loss_val += dimensional_regularization(tree, dataset, options)
endwhich calls out to function dimensional_regularization(
tree::Union{AbstractExpression{T},AbstractExpressionNode{T}},
dataset::Dataset{T,L},
options::AbstractOptions,
) where {T<:DATA_TYPE,L<:LOSS_TYPE}
if !violates_dimensional_constraints(tree, dataset, options)
return zero(L)
end
return convert(L, something(options.dimensional_constraint_penalty, 1000))
endwhich then is unpacked using these: function violates_dimensional_constraints(
tree::AbstractExpression, dataset::Dataset, options::AbstractOptions
)
return violates_dimensional_constraints(get_tree(tree), dataset, options)
end
function violates_dimensional_constraints(
tree::AbstractExpressionNode, dataset::Dataset, options::AbstractOptions
)
X = dataset.X
return violates_dimensional_constraints(
tree, dataset.X_units, dataset.y_units, (@view X[:, 1]), options
)
endSo I think you could just pass each This function sits in So it might look like: function my_loss(ex::TemplateExpression, dataset::Dataset{T,L}, options) where {T,L}
prediction, complete = eval_tree_array(ex, dataset, options)
if !complete
return L(Inf)
end
f = ex.trees.f
g = ex.trees.g
h = ex.trees.h
checker(args...) = SymbolicRegression.DimensionalAnalysisModule.violates_dimensional_constraints(args...)
f_violates = checker(
f,
[u"km/s", u"m/s", u"kg"], #= X_units; the input dimensions to f =#
[u"1"], #= y_units; The expected output dimensions =#
ones(T, 3), #= Example input with 3 features =#
options
)
g_violates = checker(
g,
[u"K"], #= Different input units! =#
nothing, #= No requirement for output units =#
ones(T, 1), #= Example with 1 input feature =#
options
)Note that if Note that the These could be different between f, g, and h. You can set You would then pass this to If you want to deal with the units of Hope this helps! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I know that dimensional constraints aren't working for template expressions (and I understand why it isn't trivial or even makes sense to make it available for the general case), but would it be possible to make use of it in a custom loss function?
In my case for instance, I look for equations of the form:
so I know that f should have the same dimension as y, whilst g and h should have the same dimension dividing to 1.
Any advice on where to alter the backend to make use of this information, if possible, would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions