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

Account for empty validation and test sets #152

Merged
merged 7 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions src/metatensor/models/cli/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def _add_eval_model_parser(subparser: argparse._SubParsersAction) -> None:

def _eval_targets(model, dataset: Union[_BaseDataset, torch.utils.data.Subset]) -> None:
"""Evaluate an exported model on a dataset and print the RMSEs for each target."""
if len(dataset) == 0:
logger.info("This dataset is empty. No evaluation will be performed.")
return
# Attach neighbor lists to the systems:
requested_neighbor_lists = model.requested_neighbors_lists()
# working around https://github.com/lab-cosmo/metatensor/issues/521
Expand Down
2 changes: 1 addition & 1 deletion src/metatensor/models/cli/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def _train_model_hydra(options: DictConfig) -> None:
validation_size = validation_options
train_size -= validation_size

if validation_size < 0 or validation_size >= 1:
if validation_size <= 0 or validation_size >= 1:
raise ValueError("Validation set split must be between 0 and 1.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValueError("Validation set split must be between 0 and 1.")
raise ValueError("Validation set split must be larger than 0 and smaller than 1.")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then also for the training set...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


generator = torch.Generator()
Expand Down
45 changes: 45 additions & 0 deletions tests/cli/test_train_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,51 @@ def test_train_multiple_datasets(monkeypatch, tmp_path, options):
train_model(options)


def test_empty_training_set(monkeypatch, tmp_path, options):
"""Test that an error is raised if no training set is provided."""
monkeypatch.chdir(tmp_path)

shutil.copy(DATASET_PATH, "qm9_reduced_100.xyz")

options["validation_set"] = 0.6
options["test_set"] = 0.4

with pytest.raises(
ValueError, match="Fraction of the train set is smaller or equal to 0!"
):
train_model(options)


def test_empty_validation_set(monkeypatch, tmp_path, options):
"""Test that an error is raised if no validation set is provided."""
monkeypatch.chdir(tmp_path)

shutil.copy(DATASET_PATH, "qm9_reduced_100.xyz")

options["validation_set"] = 0.0
options["test_set"] = 0.4

with pytest.raises(ValueError, match="must be between 0 and 1"):
train_model(options)


def test_empty_test_set(monkeypatch, tmp_path, options):
"""Test that no error is raised if no test set is provided."""
monkeypatch.chdir(tmp_path)

shutil.copy(DATASET_PATH, "qm9_reduced_100.xyz")

options["validation_set"] = 0.4
options["test_set"] = 0.0

train_model(options)

# check if the logging is correct
with open(glob.glob("outputs/*/*/train.log")[0]) as f:
log = f.read()
assert "This dataset is empty. No evaluation" in log


@pytest.mark.parametrize(
"test_set_file, validation_set_file", [(True, False), (False, True)]
)
Expand Down