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

Gradient check example #1497

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft

Gradient check example #1497

wants to merge 8 commits into from

Conversation

m-philipps
Copy link
Contributor

I'd be happy about suggestions on the "Best practices" and "How to fix my gradients".

@codecov-commenter
Copy link

codecov-commenter commented Oct 20, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.90%. Comparing base (bff4bdf) to head (0e76b2b).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1497      +/-   ##
===========================================
- Coverage    82.94%   82.90%   -0.05%     
===========================================
  Files          163      163              
  Lines        13790    13790              
===========================================
- Hits         11438    11432       -6     
- Misses        2352     2358       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@dweindl dweindl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I think I would just skip over check_grad and directly introduce check_grad_multi_eps. The latter performs much better.

doc/example/gradient_check.ipynb Outdated Show resolved Hide resolved
doc/example/gradient_check.ipynb Show resolved Hide resolved
@m-philipps
Copy link
Contributor Author

I think I would just skip over check_grad and directly introduce check_grad_multi_eps. The latter performs much better.

I agree that check_grad_multi_eps is convenient to use in practice; I like showcasing check_grad first to build it up like a tutorial, such that it is easier to understand what check_grad_multi_eps is doing for people who aren't so familiar with gradient checks. I'm also fine with changing it though.

Copy link
Collaborator

@PaulJonasJost PaulJonasJost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks for this.

Comment on lines +653 to +657
kwargs = {
"x_free": self.amici_object_builder.petab_problem.x_free_indices
}
return super().check_gradients_match_finite_differences(
*args, x=x, x_free=x_free, **kwargs
*args, x=x, **kwargs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason for those changes? I would feel like keeping an argument explicit always gives more info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was necessary to fix the error described in #1494

"source": [
"# Gradient checks\n",
"\n",
"It is best practice to do gradient checks before and after gradient-based optimization.\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to include some rationale for why to check it afterwards and what to look for. I.e. except for parameters with active bounds, the values should be close to 0. At the same time, this might make it difficult to get good FD approximations.

}
},
"source": [
"#### Set up an example problem\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the heading levels aren't consistent yet (https://pypesto--1497.org.readthedocs.build/en/1497/example/gradient_check.html)

"source": [
"#### Set up an example problem\n",
"\n",
"Create the pypesto problem and a random vector of parameter values."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter only happens in the next section. Keep text/code coherent.

Comment on lines +267 to +268
"Explanation of the gradient check result columns:\n",
"- `grad`: Objective gradient\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add blank line before listing for proper list rendering (see https://pypesto--1497.org.readthedocs.build/en/1497/example/gradient_check.html)

"### How to \"fix\" my gradients?\n",
"\n",
"- Find suitable simulation tolerances.\n",
"- Consider switching from adjoint to forward sensitivities, which tend to be more robust.\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether to include that ASA/FSA point. Generally, ASA should be able to provide accurate gradients too.

"cell_type": "markdown",
"metadata": {},
"source": [
"### How to \"fix\" my gradients?\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should be distinguished here whether one brought one's own gradient function or whether one is using the petab-amici-pipeline. Most points only apply to the latter.

"- Find suitable simulation tolerances.\n",
"- Consider switching from adjoint to forward sensitivities, which tend to be more robust.\n",
"- Check the simulation logs for Warnings and Errors.\n",
"- Ensure that the model is correct.\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, the question is not so much about the model. With a wrong model, we should still get accurate gradients. It's more about the correctness of the gradient computation.

@m-philipps m-philipps marked this pull request as draft November 6, 2024 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants