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

Add cli options for gradient clipping #164

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions careless/args/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,22 @@
"default":0.99,
}),

(("--clipnorm",), {
"help":"Optionally clip the norm of the gradient of each weight to be no larger than this value.",
"type": float,
"default": None,
}),

(("--clipvalue",), {
"help":"Optionally clip the gradients to be no larger than this value.",
"type": float,
"default": None,
}),

(("--global-clipnorm",), {
"help":"Optionally clip the norm of all the gradients to be no larger than this value.",
"type": float,
"default": None,
}),

)
3 changes: 3 additions & 0 deletions careless/io/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ def build_model(self, parser=None, surrogate_posterior=None, prior=None, likelih
parser.learning_rate,
parser.beta_1,
parser.beta_2,
clipnorm=parser.clipnorm,
clipvalue=parser.clipvalue,
global_clipnorm=parser.global_clipnorm,
)

model.compile(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,18 @@ def test_freeze_scales(off_file):
out_file = out + f"_0.mtz"
assert exists(out_file)

@pytest.mark.parametrize('clip_type', ['--clipvalue', '--clipnorm', '--global-clipnorm'])
def test_clipping(off_file, clip_type):
""" Test `--freeze-scales` for execution """
with TemporaryDirectory() as td:
out = td + '/out'
flags = f"mono --disable-gpu --iterations={niter} {clip_type}=1. dHKL,image_id"
command = flags + f" {off_file} {out}"
from careless.parser import parser
parser = parser.parse_args(command.split())
run_careless(parser)

out_file = out + f"_0.mtz"
assert exists(out_file)


Loading