-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from nlesc-nano/gpytorch
Add support for GPytorch
- Loading branch information
Showing
34 changed files
with
1,543 additions
and
1,110 deletions.
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
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
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,68 @@ | ||
#!/usr/bin/env python | ||
|
||
import logging | ||
from pathlib import Path | ||
|
||
import torch | ||
|
||
from swan.dataset import FingerprintsData, split_dataset | ||
from swan.modeller import GPModeller | ||
from swan.modeller.models import GaussianProcess | ||
from swan.utils.log_config import configure_logger | ||
from swan.utils.plot import create_confidence_plot, create_scatter_plot | ||
|
||
# Starting logger | ||
configure_logger(Path(".")) | ||
LOGGER = logging.getLogger(__name__) | ||
|
||
# Set float size default | ||
torch.set_default_dtype(torch.float32) | ||
|
||
# Path to the DATASET | ||
path_data = Path("tests/files/thousand.csv") | ||
|
||
# Training variables | ||
nepoch = 100 | ||
properties = [ | ||
# "Dissocation energy (nucleofuge)", | ||
# "Dissociation energy (electrofuge)", | ||
# "Electroaccepting power(w+)", | ||
# "Electrodonating power (w-)", | ||
# "Electronegativity (chi=-mu)", | ||
# "Electronic chemical potential (mu)", | ||
# "Electronic chemical potential (mu+)", | ||
# "Electronic chemical potential (mu-)", | ||
# "Electrophilicity index (w=omega)", | ||
# "Global Dual Descriptor Deltaf+", | ||
# "Global Dual Descriptor Deltaf-", | ||
"Hardness (eta)", | ||
# "Hyperhardness (gamma)", | ||
# "Net Electrophilicity", | ||
# "Softness (S)" | ||
] | ||
num_labels = len(properties) | ||
|
||
# Datasets | ||
data = FingerprintsData(path_data, properties=properties, sanitize=False) | ||
|
||
# Split the data into training and validation set | ||
partition = split_dataset(data.fingerprints, data.labels, frac=(0.8, 0.2)) | ||
|
||
# Model | ||
model = GaussianProcess(partition.features_trainset, partition.labels_trainset.flatten()) | ||
|
||
# training and validation | ||
researcher = GPModeller(model, data, use_cuda=False, replace_state=True) | ||
researcher.set_optimizer("Adam", lr=0.5) | ||
researcher.set_scheduler(None) | ||
trained_multivariate, expected_train = researcher.train_model(nepoch, partition) | ||
|
||
# # Print validation scatterplot | ||
print("validation regression") | ||
multi, label_validset = researcher.validate_model() | ||
|
||
create_confidence_plot( | ||
multi, label_validset.flatten(), properties[0], "validation_scatterplot") | ||
|
||
create_scatter_plot( | ||
multi.mean.reshape(-1, 1), label_validset, properties, "simple_scatterplot") |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
"""Swan API.""" | ||
from .__version__ import __version__ | ||
|
||
from .modeller import Modeller, SKModeller | ||
from swan.dataset import TorchGeometricGraphData, FingerprintsData, DGLGraphData | ||
from .modeller.models import FingerprintFullyConnected, MPNN, SE3Transformer | ||
from .dataset import DGLGraphData, FingerprintsData, TorchGeometricGraphData | ||
from .modeller import SKModeller, TorchModeller | ||
from .modeller.models import (MPNN, FingerprintFullyConnected, GaussianProcess, | ||
SE3Transformer) | ||
|
||
__all__ = [ | ||
"__version__", "Modeller", "SKModeller", | ||
"__version__", "TorchModeller", "SKModeller", | ||
"TorchGeometricGraphData", "FingerprintsData", "DGLGraphData", | ||
"FingerprintFullyConnected", "MPNN", "SE3Transformer"] | ||
"FingerprintFullyConnected", "MPNN", "SE3Transformer", "GaussianProcess"] |
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,6 @@ | ||
from .dgl_graph_data import DGLGraphData | ||
from .fingerprints_data import FingerprintsData | ||
from .splitter import split_dataset, load_split_dataset | ||
from .torch_geometric_graph_data import TorchGeometricGraphData | ||
from .dgl_graph_data import DGLGraphData | ||
|
||
__all__ = ["DGLGraphData", "FingerprintsData", "TorchGeometricGraphData"] | ||
__all__ = ["DGLGraphData", "FingerprintsData", "TorchGeometricGraphData", "load_split_dataset", "split_dataset"] |
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,52 @@ | ||
from typing import Generic, NamedTuple, Tuple, TypeVar, Union | ||
|
||
import numpy as np | ||
import torch | ||
|
||
from ..state import StateH5 | ||
from ..type_hints import PathLike | ||
|
||
T_co = TypeVar('T_co', bound=Union[np.ndarray, torch.Tensor], covariant=True) | ||
|
||
|
||
class SplitDataset(NamedTuple, Generic[T_co]): | ||
indices: np.ndarray # Shuffled indices to split the data | ||
ntrain: int # Number of points used for training | ||
features_trainset: T_co # Features for training | ||
features_validset: T_co # Features for validation | ||
labels_trainset: T_co # Labels for training | ||
labels_validset: T_co # Labels for validation | ||
|
||
|
||
def split_dataset(features: T_co, labels: T_co, frac: Tuple[float, float] = (0.8, 0.2)) -> SplitDataset: | ||
"""Split the fingerprint dataset into a training and validation set. | ||
Parameters | ||
---------- | ||
features | ||
Dataset features | ||
labels | ||
Dataset labels | ||
frac | ||
fraction to divide the dataset, by default [0.8, 0.2] | ||
""" | ||
# Generate random indices to train and validate the model | ||
size = len(features) | ||
indices = np.arange(size) | ||
np.random.shuffle(indices) | ||
|
||
ntrain = int(size * frac[0]) | ||
features_trainset = features[indices[:ntrain]] | ||
features_validset = features[indices[ntrain:]] | ||
labels_trainset = labels[indices[:ntrain]] | ||
labels_validset = labels[indices[ntrain:]] | ||
|
||
return SplitDataset(indices, ntrain, features_trainset, features_validset, labels_trainset, labels_validset) | ||
|
||
|
||
def load_split_dataset(state_file: PathLike = "swan_state.h5"): | ||
"""Load the split data used for training from the state file.""" | ||
state = StateH5(state_file) | ||
return SplitDataset(*[ | ||
state.retrieve_data(x) for x in ( | ||
'indices', 'ntrain', 'features_trainset', 'features_validset', 'labels_trainset', 'labels_validset')]) |
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,4 +1,5 @@ | ||
from .modeller import Modeller | ||
from .gp_modeller import GPModeller | ||
from .scikit_modeller import SKModeller | ||
from .torch_modeller import TorchModeller | ||
|
||
_all__ = ["Modeller", "SKModeller"] | ||
__all__ = ["GPModeller", "SKModeller", "TorchModeller"] |
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.