|
80 | 80 |
|
81 | 81 | tree = copy(original_tree) |
82 | 82 | did_i_run_2 = Ref(false) |
83 | | - fg!(F, G, tree) = |
84 | | - let |
85 | | - if G !== nothing |
86 | | - ŷ, dŷ_dconstants, _ = eval_grad_tree_array( |
87 | | - tree, X, operators; variable=false |
| 83 | + function my_fg!(F, G, tree) |
| 84 | + if G !== nothing |
| 85 | + ŷ, dŷ_dconstants, _ = eval_grad_tree_array(tree, X, operators; variable=false) |
| 86 | + dresult_dŷ = @. 2 * (ŷ - y) |
| 87 | + for i in eachindex(G) |
| 88 | + G[i] = sum( |
| 89 | + j -> dresult_dŷ[j] * dŷ_dconstants[i, j], |
| 90 | + eachindex(axes(dŷ_dconstants, 2), axes(dresult_dŷ, 1)), |
88 | 91 | ) |
89 | | - dresult_dŷ = @. 2 * (ŷ - y) |
90 | | - for i in eachindex(G) |
91 | | - G[i] = sum( |
92 | | - j -> dresult_dŷ[j] * dŷ_dconstants[i, j], |
93 | | - eachindex(axes(dŷ_dconstants, 2), axes(dresult_dŷ, 1)), |
94 | | - ) |
95 | | - end |
96 | | - if F !== nothing |
97 | | - did_i_run_2[] = true |
98 | | - return sum(abs2, ŷ .- y) |
99 | | - end |
100 | | - elseif F !== nothing |
101 | | - # Only f |
102 | | - return sum(abs2, tree(X, operators) .- y) |
103 | 92 | end |
| 93 | + if F !== nothing |
| 94 | + did_i_run_2[] = true |
| 95 | + return sum(abs2, ŷ .- y) |
| 96 | + end |
| 97 | + elseif F !== nothing |
| 98 | + # Only f |
| 99 | + return sum(abs2, tree(X, operators) .- y) |
104 | 100 | end |
105 | | - res = optimize(Optim.only_fg!(fg!), tree, BFGS()) |
| 101 | + end |
| 102 | + res = optimize(Optim.only_fg!(my_fg!), tree, BFGS()) |
106 | 103 |
|
107 | 104 | @test did_i_run_2[] |
108 | 105 | @test isapprox( |
|
0 commit comments