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 save function to GNNModel #29

Merged
merged 9 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The rules for this file:
- @mattwthompson

### Added
- `GNNModel.save` function (PR #29)
- `GNNModel.load` function (PR #26)
- `convolution_dropout` and `readout_dropout` keywords to GNNModel (PR #26)

Expand Down
127 changes: 121 additions & 6 deletions openff/nagl/nn/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from openff.nagl.features.bonds import BondFeature
from openff.nagl.molecule._dgl.batch import DGLMoleculeBatch
from openff.nagl.molecule._dgl.molecule import DGLMolecule
from openff.nagl.nn.postprocess import PostprocessLayerMeta
from openff.nagl.nn.activation import ActivationFunction
from openff.nagl.nn.gcn._base import GCNStackMeta


def rmse_loss(pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
Expand Down Expand Up @@ -113,6 +116,47 @@ def _as_nagl(self):


class GNNModel(BaseGNNModel):
"""
A model that applies a graph convolutional step followed by
pooling and readout steps.

Parameters
----------
convolution_architecture: Union[str, GCNStackMeta]
The graph convolution architecture.
This can be given either as a class,
e.g. :class:`~openff.nagl.nn.gcn._sage.SAGEConvStack`
or as a string, e.g. ``"SAGEConv"``.
n_convolution_hidden_features: int
The number of hidden features in the convolutional layers.
n_convolution_layers: int
The number of convolutional layers.
n_readout_hidden_features: int
The number of hidden features in the readout layers.
n_readout_layers: int
The number of readout layers.
lilyminium marked this conversation as resolved.
Show resolved Hide resolved
activation_function: Union[str, ActivationFunction]
The activation function to use.
This can be given either as a class,
e.g. :class:`~openff.nagl.nn.activation.ActivationFunction.ReLU`,
or as a string, e.g. ``"ReLU"``.
lilyminium marked this conversation as resolved.
Show resolved Hide resolved
postprocess_layer: Union[str, PostprocessLayerMeta]
The postprocess layer to use.
This can be given either as a class,
e.g. :class:`~openff.nagl.nn.postprocess.ComputePartialCharges`,
or as a string, e.g. ``"compute_partial_charges"``.
atom_features: Tuple[AtomFeature, ...]
The atom features to use.
bond_features: Tuple[BondFeature, ...]
The bond features to use.
loss_function: Callable[[torch.Tensor, torch.Tensor], torch.Tensor]
The loss function. This is RMSE by default, but can be any function
that takes a predicted and target tensor and returns a scalar loss tensor.
convolution_dropout: float
The dropout probability to use in the convolutional layers.
readout_dropout: float
The dropout probability to use in the readout layers.
"""
@classmethod
def from_yaml_file(cls, *paths, **kwargs):
import yaml
Expand All @@ -134,13 +178,13 @@ def n_atom_features(self) -> int:

def __init__(
self,
convolution_architecture: str,
convolution_architecture: Union[str, "GCNStackMeta"],
n_convolution_hidden_features: int,
n_convolution_layers: int,
n_readout_hidden_features: int,
n_readout_layers: int,
activation_function: str,
postprocess_layer: str,
activation_function: Union[str, "ActivationFunction"],
postprocess_layer: Union[str, "PostprocessLayerMeta"],
readout_name: str,
learning_rate: float,
atom_features: Tuple["AtomFeature", ...],
Expand Down Expand Up @@ -201,6 +245,21 @@ def __init__(
def compute_property(
self, molecule: "Molecule", as_numpy: bool = False
) -> "torch.Tensor":
"""
Compute the trained property for a molecule.

Parameters
----------
molecule: :class:`~openff.toolkit.topology.Molecule`
The molecule to compute the property for.
as_numpy: bool
Whether to return the result as a numpy array.
If ``False``, the result will be a ``torch.Tensor``.

Returns
-------
result: torch.Tensor or numpy.ndarray
"""
try:
values = self._compute_property_dgl(molecule)
except MissingOptionalDependencyError:
Expand Down Expand Up @@ -260,9 +319,37 @@ def _validate_features(features, feature_class):

@classmethod
def load(cls, model: str, eval_mode: bool = True):
import torch

model_kwargs = torch.load(model)
"""
Load a model from a file.

Parameters
----------
model: str
The path to the model to load.
This should be a file containing a dictionary of
hyperparameters and a state dictionary,
with the keys "hyperparameters" and "state_dict".
This can be created using the `save` method.
eval_mode: bool
Whether to set the model to evaluation mode.

Returns
-------
model: GNNModel

Examples
--------

>>> model.save("model.pt")
>>> new_model = GNNModel.load("model.pt")

Notes
-----
This method is not compatible with normal Pytorch
models saved with ``torch.save``, as it expects
a dictionary of hyperparameters and a state dictionary.
"""
model_kwargs = torch.load(str(model))
if isinstance(model_kwargs, dict):
model = cls(**model_kwargs["hyperparameters"])
model.load_state_dict(model_kwargs["state_dict"])
Expand All @@ -274,3 +361,31 @@ def load(cls, model: str, eval_mode: bool = True):
model.eval()

return model

def save(self, path: str):
"""
Save this model to a file.

Parameters
----------
path: str
The path to save this file to.

Examples
--------

>>> model.save("model.pt")
>>> new_model = GNNModel.load("model.pt")

Notes
-----
This method writes a dictionary of the hyperparameters and the state dictionary,
with the keys "hyperparameters" and "state_dict".
"""
torch.save(
{
"hyperparameters": self.hparams,
"state_dict": self.state_dict(),
},
str(path),
)
8 changes: 8 additions & 0 deletions openff/nagl/tests/nn/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,11 @@ def test_load(self, openff_methane_uncharged):
charges = model.compute_property(openff_methane_uncharged, as_numpy=True)
expected = np.array([-0.111393, 0.027848, 0.027848, 0.027848, 0.027848])
assert_allclose(charges, expected, atol=1e-5)

def test_save(self, am1bcc_model, openff_methane_uncharged, tmpdir):
with tmpdir.as_cwd():
am1bcc_model.save("model.pt")
model = GNNModel.load("model.pt", eval_mode=True)
charges = model.compute_property(openff_methane_uncharged, as_numpy=True)
expected = np.array([-0.143774, 0.035943, 0.035943, 0.035943, 0.035943])
assert_allclose(charges, expected, atol=1e-5)