-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: of loss function expression on workers
- Loading branch information
1 parent
c7877f3
commit d953741
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 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] | ||
) |