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

Hybrid grad check #27

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Conversation

stephanmg
Copy link
Contributor

No description provided.

@stephanmg stephanmg marked this pull request as draft May 5, 2023 12:13
@stephanmg
Copy link
Contributor Author

Added test case now. See TODO in derivative_check.py though.

@stephanmg stephanmg requested a review from dilpath May 10, 2023 09:39
fiddy/derivative_check.py Outdated Show resolved Hide resolved
Comment on lines 174 to 185
for direction_index, directional_derivative in enumerate(
self.derivative.directional_derivatives
):
test_value = directional_derivative.value
test_values.append(test_value)

expected_value = []
for output_index in np.ndindex(self.output_indices):
element = self.expectation[output_index][direction_index]
expected_value.append(element)
expected_value = np.array(expected_value).reshape(test_value.shape)
expected_values.append(expected_value)
Copy link
Member

Choose a reason for hiding this comment

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

Since this is the same in NumpyIsCloseDerivativeCheck, could move it to its own function, or DerivativeCheck.__call__. Not important here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored: ExtractMethod.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I implemented the refactor in NumpyIsCloseDerivativeCheck now too.

Comment on lines 187 to 190
# debug
assert len(expected_values) == len(
test_values
), "Mismatch of step sizes"
Copy link
Member

Choose a reason for hiding this comment

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

Remove, or raise some specific exception? This is always due to a user error, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should always be a user error, removed.

Comment on lines 167 to 168
class HybridDerivativeCheck(DerivativeCheck):
method_id = "hybrid"
Copy link
Member

Choose a reason for hiding this comment

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

Could you doc the logic of this class in a class docstring here? i.e. the formula/algorithm used to determine whether gradients are correct. Will be great for when I write the remaining docs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added. LMK if more is needed.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I meant the actual formula, preferably with latex. There's an example of latex in RTD for amici.import_utils.noise_distribution_to_cost_function here [1], with the docstring here [2].

But this can also wait until you publish the formula. If you decide to wait, could you add a TODO to do this later? Would be great to add a reference to your paper when it's published, too.

[1] https://amici.readthedocs.io/en/latest/generated/amici.import_utils.html#amici.import_utils.noise_distribution_to_cost_function
[2] https://github.com/AMICI-dev/AMICI/blob/3c5e997df3655c26dde35705ef25b2a0f419fe8b/python/sdist/amici/import_utils.py#L105-L107

Comment on lines 243 to 245
except (IndexError, TypeError):
# TODO: Fix this, why does this occur?
pass
Copy link
Member

Choose a reason for hiding this comment

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

Do you have a reproducible example of this? I could take a look

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be fixed now.

fiddy/derivative_check.py Outdated Show resolved Hide resolved
tests/test_derivative.py Show resolved Hide resolved
stephanmg and others added 5 commits June 30, 2023 13:57
Co-authored-by: Dilan Pathirana <59329744+dilpath@users.noreply.github.com>
Co-authored-by: Dilan Pathirana <59329744+dilpath@users.noreply.github.com>
Co-authored-by: Dilan Pathirana <59329744+dilpath@users.noreply.github.com>
@stephanmg stephanmg marked this pull request as ready for review October 5, 2023 15:04
@stephanmg stephanmg requested a review from dilpath October 9, 2023 08:20
@stephanmg
Copy link
Contributor Author

stephanmg commented Nov 6, 2023

PR doesn't update...

@stephanmg stephanmg closed this Nov 6, 2023
@stephanmg stephanmg reopened this Nov 6, 2023
fiddy/derivative_check.py Outdated Show resolved Hide resolved
Comment on lines +187 to +188
approxs_for_param = []
grads_for_param = []
Copy link
Member

Choose a reason for hiding this comment

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

Should this be reset in the inner loop, instead of here in the outer loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be okay as-is.

):
approxs_for_param.append(approx)
grads_for_param.append(grad)
fd_range = np.percentile(approxs_for_param, [0, 100])
Copy link
Member

Choose a reason for hiding this comment

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

Is this just the min and max of approxs_for_param?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

Comment on lines 213 to 219
if (
abs(grad_mean - fd_mean)
/ abs(fd_range + np.finfo(float).eps)
) > kwargs["rtol"]:
results.append(False)
else:
results.append(False)
Copy link
Member

Choose a reason for hiding this comment

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

The handling of results needs some work. For example, here both cases of the if-else have results.append(False).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines +226 to +230
if not np.isfinite([fd_range, fd_mean]).all():
results.append(None)
else:
result = True
results.append(result)
Copy link
Member

Choose a reason for hiding this comment

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

Need to do result = ... everywhere (if going with this coding style), and finally results.append(result) at the end (outside of this else statement). I can do this, will wait until the other comments are resolved.

fiddy/derivative_check.py Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants