Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get loss_function_expression working on distributed workers #412

Merged
merged 2 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SymbolicRegression"
uuid = "8254be44-1295-4e6a-a16d-46603ac705cb"
authors = ["MilesCranmer <[email protected]>"]
version = "1.7.0"
version = "1.7.1"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
8 changes: 8 additions & 0 deletions src/Configure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function move_functions_to_workers(
:elementwise_loss,
:early_stop_condition,
:loss_function,
:loss_function_expression,
:complexity_mapping,
)

Expand Down Expand Up @@ -157,6 +158,13 @@ function move_functions_to_workers(
end
ops = (options.loss_function,)
example_inputs = (Node(T; val=zero(T)), dataset, options)
elseif function_set == :loss_function_expression
if options.loss_function_expression === nothing
continue
end
ops = (options.loss_function_expression,)
ex = create_expression(zero(T), options, dataset)
example_inputs = (ex, dataset, options)
elseif function_set == :complexity_mapping
if !(options.complexity_mapping isa Function)
continue
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ include("test_mlj.jl")
include("test_custom_operators_multiprocessing.jl")
end

@testitem "Testing whether we can move loss function expression to workers." tags = [:part2] begin
include("test_loss_function_expression_multiprocessing.jl")
end

@testitem "Test whether the precompilation script works." tags = [:part2] begin
include("test_precompilation.jl")
end
Expand Down
48 changes: 48 additions & 0 deletions test/test_loss_function_expression_multiprocessing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using SymbolicRegression
using Test

defs = quote
using SymbolicRegression

early_stop(loss, c) = ((loss <= 1e-10) && (c <= 4))
function my_loss_expression(ex::Expression, dataset::Dataset, options::Options)
prediction, complete = eval_tree_array(ex, dataset.X, options)
if !complete
return Inf
end
return sum((prediction .- dataset.y) .^ 2) / dataset.n
end
end

# This is needed as workers are initialized in `Core.Main`!
if (@__MODULE__) != Core.Main
Core.eval(Core.Main, defs)
eval(:(using Main: early_stop, my_loss_expression))
else
eval(defs)
end

X = randn(Float32, 5, 100)
y = @. 2 * cos(X[4, :])

options = SymbolicRegression.Options(;
binary_operators=[*, +],
unary_operators=[cos],
early_stop_condition=early_stop,
loss_function_expression=my_loss_expression,
)

hof = equation_search(
X,
y;
weights=ones(Float32, 100),
options=options,
niterations=1_000_000_000,
numprocs=2,
parallelism=:multiprocessing,
)

@test any(
early_stop(member.loss, length(get_tree(member.tree))) for
member in hof.members[hof.exists]
)
Loading