Repository navigation
Fix: preserve direction and input identity in memoized evaluations #821
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
Merged
mrava87
merged 6 commits into
PyLops:dev
from
AHMETHAKANBEZIR1:fix/memoize-adjoint-direction
Oct 29, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dec5089
Fix: distinguish forward and adjoint memoized evaluations
AHMETHAKANBEZIR1 40e49a2
Fix: require exact memoized input values and dtype
AHMETHAKANBEZIR1 3b0bef6
doc: improved plot_memoized
mrava87 62fd21d
test: aligned tests for memoizeoperator to pylops standards
mrava87 b853e46
test: added value check in test_memoize_evals
mrava87 f82afb4
minor: improved docstring of memoizeoperator
mrava87 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| r""" | ||
| Memoized forward and adjoint evaluations | ||
| ======================================== | ||
|
|
||
| This example shows how to use the :class:`pylops.MemoizeOperator` | ||
| operator to cache repeated applications of an operator. | ||
| """ | ||
|
|
||
| import numpy as np | ||
|
|
||
| import pylops | ||
|
|
||
| ############################################################################### | ||
| # Let's start by creating a :class:`pylops.Diagonal` operator and wrapping it | ||
| # into a :class:`pylops.MemoizeOperator` operator choosing ``max_neval=2`` | ||
| # (meaning that at most 4 evaluations are cached). | ||
| Op = pylops.Diagonal(np.array([2.0, 3.0, 4.0])) | ||
| MOp = pylops.MemoizeOperator(Op, max_neval=4) | ||
|
|
||
| ############################################################################### | ||
| # Now we first apply twice the forward pass of operator (and check that only | ||
| # one entry is memoized), followed by one adjoint pass of operator (and check | ||
| # that now two entries are memoized) | ||
|
|
||
| x = np.array([1.0, -2.0, 0.5]) | ||
| y = MOp @ x | ||
| y = MOp @ x | ||
| print("Number of evaluations: ", MOp.neval) | ||
|
|
||
| xadj = MOp.H @ y | ||
| print("Number of evaluations: ", MOp.neval) | ||
|
|
||
| ############################################################################### | ||
| # Repeating either calculation reuses its own cached result. | ||
| print("y - ycached = ", y - MOp @ x) | ||
| print("xadj - xadjcached = ", xadj - MOp.H @ y) | ||
| print("Number of evaluations: ", MOp.neval) | ||
|
|
||
| ############################################################################### | ||
| # Finally, we can see that similar inputs are not interchangeable: any | ||
| # small change triggers a new memoization. | ||
|
|
||
| x_nearby = x.copy() | ||
| x_nearby[0] += 1e-6 | ||
| y_nearby = MOp @ x_nearby | ||
| print("Number of evaluations: ", MOp.neval) |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.