-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Update docs remove extra returns from loss and extra args from callback #249
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
23c2a3d
Update docs remove extra returns from loss and extra args from callback
Vaibhavdixit02 1f2cb6a
Apply suggestions from code review
Vaibhavdixit02 b4f03cd
bump versions
Vaibhavdixit02 d394b79
lux compat
Vaibhavdixit02 9803b16
add comment about resolving and plotting in callback
Vaibhavdixit02 1ea8ed8
fix links
Vaibhavdixit02 109fdac
Update make.jl
ChrisRackauckas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -99,12 +99,14 @@ function loss(newp) | |
newprob = remake(prob, p = newp) | ||
sol = solve(newprob, saveat = 1) | ||
loss = sum(abs2, sol .- xy_data) | ||
return loss, sol | ||
return loss | ||
end | ||
|
||
# Define a callback function to monitor optimization progress | ||
function callback(p, l, sol) | ||
function callback(state, l) | ||
display(l) | ||
newprob = remake(prob, p = state.u) | ||
sol = solve(newprob, saveat = 1) | ||
plt = plot(sol, ylim = (0, 6), label = ["Current x Prediction" "Current y Prediction"]) | ||
scatter!(plt, t_data, xy_data', label = ["x Data" "y Data"]) | ||
display(plt) | ||
|
@@ -278,37 +280,28 @@ function loss(newp) | |
newprob = remake(prob, p = newp) | ||
sol = solve(newprob, saveat = 1) | ||
l = sum(abs2, sol .- xy_data) | ||
return l, sol | ||
return l | ||
end | ||
``` | ||
|
||
Notice that our loss function returns the loss value as the first return, | ||
but returns extra information (the ODE solution with the new parameters) | ||
as an extra return argument. | ||
We will explain why this extra return information is helpful in the next section. | ||
|
||
### Step 5: Solve the Optimization Problem | ||
|
||
This step will look very similar to [the first optimization tutorial](@ref first_opt), | ||
except now we have a new loss function `loss` which returns both the loss value | ||
and the associated ODE solution. | ||
(In the previous tutorial, `L` only returned the loss value.) | ||
The `Optimization.solve` function can accept an optional callback function | ||
to monitor the optimization process using extra arguments returned from `loss`. | ||
|
||
The callback syntax is always: | ||
|
||
``` | ||
callback( | ||
optimization variables, | ||
state, | ||
the current loss value, | ||
other arguments returned from the loss function, ... | ||
) | ||
``` | ||
|
||
In this case, we will provide the callback the arguments `(p, l, sol)`, | ||
since it always takes the current state of the optimization first (`p`) | ||
then the returns from the loss function (`l, sol`). | ||
In this case, we will provide the callback the arguments `(state, l)`, | ||
since it always takes the current state of the optimization first (`state`) | ||
then the current loss value (`l`). | ||
The return value of the callback function should default to `false`. | ||
`Optimization.solve` will halt if/when the callback function returns `true` instead. | ||
Typically the `return` statement would monitor the loss value | ||
|
@@ -318,16 +311,18 @@ More details about callbacks in Optimization.jl can be found | |
[here](https://docs.sciml.ai/Optimization/stable/API/solve/). | ||
|
||
```@example odefit | ||
function callback(p, l, sol) | ||
function callback(state, l) | ||
display(l) | ||
newprob = remake(prob, p = state.u) | ||
sol = solve(newprob, saveat = 1) | ||
Comment on lines
+316
to
+317
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't recompute |
||
plt = plot(sol, ylim = (0, 6), label = ["Current x Prediction" "Current y Prediction"]) | ||
scatter!(plt, t_data, xy_data', label = ["x Data" "y Data"]) | ||
display(plt) | ||
return false | ||
end | ||
``` | ||
|
||
With this callback function, every step of the optimization will display both the loss value and a plot of how the solution compares to the training data. | ||
With this callback function, every step of the optimization will display both the loss value and a plot of how the solution compares to the training data. Since we want to track the fit visually we plot the simulation at each iteration and compare it to the data. This is expensive since it requires an extra `solve` call and a plotting step for each iteration. | ||
|
||
Now, just like [the first optimization tutorial](@ref first_opt), | ||
we set up our `OptimizationFunction` and `OptimizationProblem`, | ||
|
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 |
---|---|---|
|
@@ -490,10 +490,8 @@ function loss(NN_params) | |
prob_nn, RK4(), u0 = u0, p = NN_params, saveat = tsteps, dt = dt, adaptive = false)) | ||
pred_waveform = compute_waveform(dt_data, pred, mass_ratio, model_params)[1] | ||
|
||
loss = (sum(abs2, | ||
view(waveform, obs_to_use_for_training) .- | ||
view(pred_waveform, obs_to_use_for_training))) | ||
return loss, pred_waveform | ||
loss = ( sum(abs2, view(waveform,obs_to_use_for_training) .- view(pred_waveform,obs_to_use_for_training) ) ) | ||
return loss | ||
end | ||
``` | ||
|
||
|
@@ -508,10 +506,11 @@ We'll use the following callback to save the history of the loss values. | |
```@example ude | ||
losses = [] | ||
|
||
callback(θ, l, pred_waveform; doplot = true) = begin | ||
callback(state, l; doplot = true) = begin | ||
push!(losses, l) | ||
#= Disable plotting as it trains since in docs | ||
display(l) | ||
waveform = compute_waveform(dt_data, soln, mass_ratio, model_params)[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't recompute |
||
# plot current prediction against data | ||
plt = plot(tsteps, waveform, | ||
markershape=:circle, markeralpha = 0.25, | ||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't recompute