-
Notifications
You must be signed in to change notification settings - Fork 3
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 QoL helpers for building 'closures' #63
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c55df3f
Add QoL helpers for training
SimonBoothroyd e6cadcd
Fix circular import
SimonBoothroyd 2091f7a
Fix type casting
SimonBoothroyd 2213668
Fix printing None
SimonBoothroyd 024668c
Fix printing None
SimonBoothroyd d96c61e
Improve verbose logging
SimonBoothroyd 93bf8ff
Clean-up extra util functions
SimonBoothroyd af3887e
Add tests
SimonBoothroyd c7f50c2
Add thermo default closure test and unify api
SimonBoothroyd 116c364
Remove train helper for now
SimonBoothroyd d9a6291
Fix tests
SimonBoothroyd 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
"""Custom parameter optimizers.""" | ||
|
||
from descent.optim._lm import LevenbergMarquardtConfig, levenberg_marquardt | ||
from descent.optim._lm import ClosureFn, LevenbergMarquardtConfig, levenberg_marquardt | ||
|
||
__all__ = ["LevenbergMarquardtConfig", "levenberg_marquardt"] | ||
__all__ = ["ClosureFn", "LevenbergMarquardtConfig", "levenberg_marquardt"] |
This file contains 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 |
---|---|---|
@@ -1 +1,5 @@ | ||
"""Targets to train / assess models to / against.""" | ||
|
||
from descent.targets._targets import combine_closures | ||
|
||
__all__ = ["combine_closures"] |
This file contains 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,79 @@ | ||
import logging | ||
|
||
import torch | ||
|
||
import descent.optim | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def combine_closures( | ||
closures: dict[str, descent.optim.ClosureFn], | ||
weights: dict[str, float] | None = None, | ||
verbose: bool = False, | ||
) -> descent.optim.ClosureFn: | ||
"""Combine multiple closures into a single closure. | ||
|
||
Args: | ||
closures: A dictionary of closure functions. | ||
weights: Optional dictionary of weights for each closure function. | ||
verbose: Whether to log the loss of each closure function. | ||
|
||
Returns: | ||
A combined closure function. | ||
""" | ||
|
||
weights = weights if weights is not None else {name: 1.0 for name in closures} | ||
|
||
if len(closures) == 0: | ||
raise NotImplementedError("At least one closure function is required.") | ||
|
||
if {*closures} != {*weights}: | ||
raise ValueError("The closures and weights must have the same keys.") | ||
|
||
def combined_closure_fn( | ||
x: torch.Tensor, compute_gradient: bool, compute_hessian: bool | ||
) -> tuple[torch.Tensor, torch.Tensor | None, torch.Tensor | None]: | ||
|
||
loss = [] | ||
grad = None if not compute_gradient else [] | ||
hess = None if not compute_hessian else [] | ||
|
||
verbose_rows = [] | ||
|
||
for name, closure_fn in closures.items(): | ||
|
||
local_loss, local_grad, local_hess = closure_fn( | ||
x, compute_gradient, compute_hessian | ||
) | ||
|
||
loss.append(weights[name] * local_loss) | ||
|
||
if compute_gradient: | ||
grad.append(weights[name] * local_grad) | ||
if compute_hessian: | ||
hess.append(weights[name] * local_hess) | ||
|
||
if verbose: | ||
verbose_rows.append( | ||
{"target": name, "loss": float(f"{local_loss:.5f}")} | ||
) | ||
|
||
loss = sum(loss[1:], loss[0]) | ||
|
||
if compute_gradient: | ||
grad = sum(grad[1:], grad[0]) | ||
if compute_hessian: | ||
hess = sum(hess[1:], hess[0]) | ||
|
||
if verbose: | ||
import pandas | ||
|
||
_LOGGER.info( | ||
"loss breakdown:\n" | ||
+ pandas.DataFrame(verbose_rows).to_string(index=False) | ||
) | ||
|
||
return loss.detach(), grad, hess | ||
|
||
return combined_closure_fn |
This file contains 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 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 |
---|---|---|
|
@@ -16,9 +16,12 @@ | |
import smee.mm | ||
import smee.utils | ||
import torch | ||
from rdkit import Chem | ||
|
||
import descent.optim | ||
import descent.train | ||
import descent.utils.dataset | ||
import descent.utils.loss | ||
import descent.utils.molecule | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
@@ -138,24 +141,6 @@ class _Observables(typing.NamedTuple): | |
_SystemDict = dict[SimulationKey, smee.TensorSystem] | ||
|
||
|
||
def _map_smiles(smiles: str) -> str: | ||
"""Add atom mapping to a SMILES string if it is not already present.""" | ||
params = Chem.SmilesParserParams() | ||
params.removeHs = False | ||
|
||
mol = Chem.AddHs(Chem.MolFromSmiles(smiles, params)) | ||
|
||
map_idxs = sorted(atom.GetAtomMapNum() for atom in mol.GetAtoms()) | ||
|
||
if map_idxs == list(range(1, len(map_idxs) + 1)): | ||
return smiles | ||
|
||
for i, atom in enumerate(mol.GetAtoms()): | ||
atom.SetAtomMapNum(i + 1) | ||
|
||
return Chem.MolToSmiles(mol) | ||
|
||
|
||
def create_dataset(*rows: DataEntry) -> datasets.Dataset: | ||
"""Create a dataset from a list of existing data points. | ||
|
||
|
@@ -167,12 +152,12 @@ def create_dataset(*rows: DataEntry) -> datasets.Dataset: | |
""" | ||
|
||
for row in rows: | ||
row["smiles_a"] = _map_smiles(row["smiles_a"]) | ||
row["smiles_a"] = descent.utils.molecule.map_smiles(row["smiles_a"]) | ||
|
||
if row["smiles_b"] is None: | ||
continue | ||
|
||
row["smiles_b"] = _map_smiles(row["smiles_b"]) | ||
row["smiles_b"] = descent.utils.molecule.map_smiles(row["smiles_b"]) | ||
|
||
# TODO: validate rows | ||
table = pyarrow.Table.from_pylist([*rows], schema=DATA_SCHEMA) | ||
|
@@ -582,6 +567,7 @@ def predict( | |
output_dir: pathlib.Path, | ||
cached_dir: pathlib.Path | None = None, | ||
per_type_scales: dict[DataType, float] | None = None, | ||
verbose: bool = False, | ||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: | ||
"""Predict the properties in a dataset using molecular simulation, or by reweighting | ||
previous simulation data. | ||
|
@@ -596,6 +582,7 @@ def predict( | |
from. | ||
per_type_scales: The scale factor to apply to each data type. A default of 1.0 | ||
will be used for any data type not specified. | ||
verbose: Whether to log additional information. | ||
""" | ||
|
||
entries: list[DataEntry] = [*descent.utils.dataset.iter_dataset(dataset)] | ||
|
@@ -616,6 +603,8 @@ def predict( | |
reference = [] | ||
reference_std = [] | ||
|
||
verbose_rows = [] | ||
|
||
per_type_scales = per_type_scales if per_type_scales is not None else {} | ||
|
||
for entry, keys in zip(entries, entry_to_simulation): | ||
|
@@ -631,10 +620,77 @@ def predict( | |
torch.nan if entry["std"] is None else entry["std"] * abs(type_scale) | ||
) | ||
|
||
if verbose: | ||
std_ref = "" if entry["std"] is None else " ± {float(entry['std']):.3f}" | ||
|
||
verbose_rows.append( | ||
{ | ||
"type": f'{entry["type"]} [{entry["units"]}]', | ||
"smiles_a": descent.utils.molecule.unmap_smiles(entry["smiles_a"]), | ||
"smiles_b": ( | ||
"" | ||
if entry["smiles_b"] is None | ||
else descent.utils.molecule.unmap_smiles(entry["smiles_b"]) | ||
), | ||
"pred": f"{float(value):.3f} ± {float(std):.3f}", | ||
"ref": f"{float(entry['value']):.3f}{std_ref}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
} | ||
) | ||
|
||
if verbose: | ||
import pandas | ||
|
||
_LOGGER.info(f"predicted {len(entries)} properties") | ||
_LOGGER.info("\n" + pandas.DataFrame(verbose_rows).to_string(index=False)) | ||
|
||
predicted = torch.stack(predicted) | ||
predicted_std = torch.stack(predicted_std) | ||
|
||
reference = smee.utils.tensor_like(reference, predicted) | ||
reference_std = smee.utils.tensor_like(reference_std, predicted_std) | ||
|
||
return reference, reference_std, predicted, predicted_std | ||
|
||
|
||
def default_closure( | ||
trainable: descent.train.Trainable, | ||
topologies: dict[str, smee.TensorTopology], | ||
dataset: datasets.Dataset, | ||
scales: dict[DataType, float], | ||
verbose: bool = False, | ||
) -> descent.optim.ClosureFn: | ||
"""Return a default closure function for training against thermodynamic | ||
properties. | ||
|
||
Args: | ||
trainable: The wrapper around trainable parameters. | ||
topologies: The topologies of the molecules present in the dataset, with keys | ||
of mapped SMILES patterns. | ||
dataset: The dataset to train against. | ||
scales: The scale factor to apply to each data type. | ||
verbose: Whether to log additional information about predictions. | ||
|
||
Returns: | ||
The default closure function. | ||
""" | ||
|
||
def closure_fn( | ||
x: torch.Tensor, | ||
compute_gradient: bool, | ||
compute_hessian: bool, | ||
): | ||
force_field = trainable.to_force_field(x) | ||
|
||
y_ref, y_ref_std, y_pred, y_pred_std = descent.targets.thermo.predict( | ||
dataset, force_field, topologies, pathlib.Path.cwd(), None, scales, verbose | ||
) | ||
loss, gradient, hessian = ((y_pred - y_ref) ** 2).sum(), None, None | ||
|
||
if compute_hessian: | ||
hessian = descent.utils.loss.approximate_hessian(x, y_pred) | ||
if compute_gradient: | ||
gradient = torch.autograd.grad(loss, x, retain_graph=True)[0].detach() | ||
|
||
return loss.detach(), gradient, hessian | ||
|
||
return closure_fn |
This file contains 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, should be fixed in the latest commit!