-
Notifications
You must be signed in to change notification settings - Fork 8
Fancy learning rate #90
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
Closed
Pengju-Sheng
wants to merge
3
commits into
cdtools-developers:master
from
Pengju-Sheng:fancy-learning-rate
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,54 @@ | ||
| import cdtools | ||
| import torch as t | ||
| from matplotlib import pyplot as plt | ||
|
|
||
| filename = 'example_data/lab_ptycho_data.cxi' | ||
| dataset = cdtools.datasets.Ptycho2DDataset.from_cxi(filename) | ||
|
|
||
| # FancyPtycho is the workhorse model | ||
| model = cdtools.models.FancyPtycho.from_dataset( | ||
| dataset, | ||
| n_modes=3, # Use 3 incoherently mixing probe modes | ||
| oversampling=2, # Simulate the probe on a 2xlarger real-space array | ||
| probe_support_radius=120, # Force the probe to 0 outside a radius of 120 pix | ||
| propagation_distance=5e-3, # Propagate the initial probe guess by 5 mm | ||
| units='mm', # Set the units for the live plots | ||
| obj_view_crop=-50, # Expands the field of view in the object plot by 50 pix | ||
| ) | ||
|
|
||
| if t.cuda.is_available(): | ||
| model.to(device='cuda') | ||
| dataset.get_as(device='cuda') | ||
|
|
||
| # For this script, we use a slightly different pattern where we explicitly | ||
| # create a `Reconstructor` class to orchestrate the reconstruction. The | ||
| # reconstructor will store the model and dataset and create an appropriate | ||
| # optimizer. This allows the optimizer to persist between loops, along with | ||
| # e.g. estimates of the moments of individual parameters | ||
| recon = cdtools.reconstructors.AdamReconstructor(model, dataset) | ||
|
|
||
| # The learning rate parameter sets the alpha for Adam. | ||
| # The beta parameters are (0.9, 0.999) by default | ||
| # The batch size sets the minibatch size | ||
| lr = {'translation_offsets':0.04,'background':0.01} | ||
| for loss in recon.optimize(50, lr=lr, batch_size=10, default_lr = 0.03): | ||
| print(model.report()) | ||
| # Because plotting can be expensive, setting a minimum plotting interval | ||
| # (in seconds) can avoid excessive replots. | ||
| model.inspect(min_interval=10) | ||
|
|
||
| # It's common to chain several different reconstruction loops. Here, we | ||
| # started with an aggressive refinement to find the probe in the previous | ||
| # loop, and now we polish the reconstruction with a lower learning rate | ||
| # and larger minibatch | ||
| for loss in recon.optimize(50, lr=0.005, batch_size=50): | ||
| print(model.report()) | ||
| model.inspect(min_interval=10) | ||
|
|
||
| # This orthogonalizes the recovered probe modes | ||
| model.tidy_probes() | ||
|
|
||
| # Setting replot_all will reopen any windows which were closed earlier | ||
| model.inspect(replot_all=True) | ||
| model.compare(dataset) | ||
| plt.show() |
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
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.