|
5 | 5 | using SymbolicRegression: eval_loss, eval_cost, Dataset |
6 | 6 | using ForwardDiff |
7 | 7 | include(joinpath(@__DIR__, "..", "..", "test_params.jl")) |
8 | | - |
| 8 | + |
9 | 9 | x1 = 2.0 |
10 | | - |
| 10 | + |
11 | 11 | function make_options_maker(binop, unaop; kw...) |
12 | 12 | @nospecialize binop unaop kw |
13 | 13 | return Options(; |
|
19 | 19 | kw..., |
20 | 20 | ) |
21 | 21 | end |
22 | | - |
| 22 | + |
23 | 23 | # Initialize functions in Base.... |
24 | 24 | for unaop in |
25 | 25 | [cos, exp, safe_log, safe_log2, safe_log10, safe_sqrt, relu, gamma, safe_acosh], |
26 | 26 | binop in [sub] |
27 | | - |
| 27 | + |
28 | 28 | let |
29 | 29 | make_options = Fix{1}(Fix{2}(make_options_maker, unaop), binop) |
30 | 30 | options = make_options() |
31 | 31 | @extend_operators options |
32 | | - |
| 32 | + |
33 | 33 | # for unaop in |
34 | 34 | f_true = (x,) -> binop(abs(3.0 * unaop(x))^2.0, -1.2) |
35 | | - |
| 35 | + |
36 | 36 | # binop at outside: |
37 | 37 | const_tree = Node( |
38 | | - 5, (^)(Node(2, Node(; val=3.0) * Node(1, Node("x1"))), 2.0), Node(; val=-1.2) |
| 38 | + 5, |
| 39 | + (^)(Node(2, Node(; val=3.0) * Node(1, Node("x1"))), 2.0), |
| 40 | + Node(; val=-1.2), |
39 | 41 | ) |
40 | 42 | const_tree_bad = Node( |
41 | | - 5, (^)(Node(2, Node(; val=3.0) * Node(1, Node("x1"))), 2.1), Node(; val=-1.3) |
| 43 | + 5, |
| 44 | + (^)(Node(2, Node(; val=3.0) * Node(1, Node("x1"))), 2.1), |
| 45 | + Node(; val=-1.3), |
42 | 46 | ) |
43 | 47 | n = count_nodes(const_tree) |
44 | | - |
| 48 | + |
45 | 49 | true_result = f_true(x1) |
46 | | - |
| 50 | + |
47 | 51 | result = eval(Meta.parse(string_tree(const_tree, options))) |
48 | | - |
| 52 | + |
49 | 53 | # Test Basics |
50 | 54 | @test n == 9 |
51 | 55 | @test result == true_result |
52 | | - |
| 56 | + |
53 | 57 | types_to_test = [Float32, Float64, BigFloat] |
54 | 58 | if unaop == cos |
55 | 59 | # Other unary operators produce numbers too large |
|
62 | 66 | else |
63 | 67 | zero_tolerance = 1e-6 |
64 | 68 | end |
65 | | - |
| 69 | + |
66 | 70 | tree = convert(Node{T}, const_tree) |
67 | 71 | tree_bad = convert(Node{T}, const_tree_bad) |
68 | | - |
| 72 | + |
69 | 73 | Random.seed!(0) |
70 | 74 | N = 100 |
71 | 75 | if unaop in [safe_log, safe_log2, safe_log10, safe_acosh, safe_sqrt] |
|
77 | 81 | if unaop == safe_acosh |
78 | 82 | X = X .+ T(1.0) |
79 | 83 | end |
80 | | - |
| 84 | + |
81 | 85 | y = T.(f_true.(X[1, :])) |
82 | 86 | dataset = Dataset(X, y) |
83 | 87 | test_y, complete = eval_tree_array(tree, X, make_options()) |
84 | 88 | test_y2, complete2 = differentiable_eval_tree_array(tree, X, make_options()) |
85 | | - |
| 89 | + |
86 | 90 | # Test Evaluation |
87 | 91 | @test complete == true |
88 | 92 | @test all(abs.(test_y .- y) / N .< zero_tolerance) |
89 | 93 | @test complete2 == true |
90 | 94 | @test all(abs.(test_y2 .- y) / N .< zero_tolerance) |
91 | | - |
| 95 | + |
92 | 96 | # Test loss: |
93 | 97 | @test abs(eval_loss(tree, dataset, make_options())) < zero_tolerance |
94 | 98 | @test eval_loss(tree, dataset, make_options()) == |
95 | 99 | eval_cost(dataset, tree, make_options())[2] |
96 | | - |
| 100 | + |
97 | 101 | #Test Scoring |
98 | 102 | @test abs(eval_cost(dataset, tree, make_options(; parsimony=0.0))[1]) < |
99 | 103 | zero_tolerance |
100 | 104 | @test eval_cost(dataset, tree, make_options(; parsimony=1.0))[1] > 1.0 |
101 | 105 | @test eval_cost(dataset, tree, make_options())[1] < |
102 | 106 | eval_cost(dataset, tree_bad, make_options())[1] |
103 | | - |
| 107 | + |
104 | 108 | dataset_with_larger_baseline = deepcopy(dataset) |
105 | 109 | dataset_with_larger_baseline.baseline_loss = one(T) * 10 |
106 | 110 | @test eval_cost(dataset_with_larger_baseline, tree_bad, make_options())[1] < |
107 | 111 | eval_cost(dataset, tree_bad, make_options())[1] |
108 | | - |
| 112 | + |
109 | 113 | # Test gradients: |
110 | 114 | df_true = x -> ForwardDiff.derivative(f_true, x) |
111 | 115 | dy = T.(df_true.(X[1, :])) |
112 | 116 | test_dy = ForwardDiff.gradient( |
113 | | - _x -> sum(differentiable_eval_tree_array(tree, _x, make_options())[1]), X |
| 117 | + _x -> sum(differentiable_eval_tree_array(tree, _x, make_options())[1]), |
| 118 | + X, |
114 | 119 | ) |
115 | 120 | test_dy = test_dy[1, 1:end] |
116 | 121 | @test all(abs.(test_dy .- dy) / N .< zero_tolerance) |
|
0 commit comments