Skip to content

Commit

Permalink
Fixed imports from examples and tests, also reformatted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomDefaultUser committed Apr 17, 2024
1 parent 301813b commit 36a6c8b
Show file tree
Hide file tree
Showing 22 changed files with 1,479 additions and 867 deletions.
1 change: 0 additions & 1 deletion examples/advanced/ex04_acsd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import mala
import numpy as np
from mala.datahandling.data_repo import data_repo_path

data_path = os.path.join(data_repo_path, "Be2")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import mala
from mala import printout

from mala.datahandling.data_repo import data_repo_path

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import mala
from mala import printout

from mala.datahandling.data_repo import data_repo_path

Expand Down
2 changes: 0 additions & 2 deletions examples/advanced/ex08_visualize_observables.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os

from ase.io import read
import mala
import numpy as np

from mala.datahandling.data_repo import data_repo_path

Expand Down
1 change: 0 additions & 1 deletion examples/basic/ex04_hyperparameter_optimization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import mala
from mala import printout

from mala.datahandling.data_repo import data_repo_path

Expand Down
1 change: 0 additions & 1 deletion examples/basic/ex06_ase_calculator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

import mala
from mala import printout
from ase.io import read

from mala.datahandling.data_repo import data_repo_path
Expand Down
355 changes: 230 additions & 125 deletions test/all_lazy_loading_test.py

Large diffs are not rendered by default.

49 changes: 34 additions & 15 deletions test/basic_gpu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import torch

from mala.datahandling.data_repo import data_repo_path

data_path = os.path.join(data_repo_path, "Be2")

test_checkpoint_name = "test"
Expand All @@ -36,8 +37,10 @@ class TestGPUExecution:
Tests whether a GPU is available and then the execution on it.
"""
@pytest.mark.skipif(torch.cuda.is_available() is False,
reason="No GPU detected.")

@pytest.mark.skipif(
torch.cuda.is_available() is False, reason="No GPU detected."
)
def test_gpu_performance(self):
"""
Test whether GPU training brings performance improvements.
Expand Down Expand Up @@ -104,12 +107,27 @@ def __run(use_gpu):

# Add a snapshot we want to use in to the list.
for i in range(0, 6):
data_handler.add_snapshot("Be_snapshot0.in.npy", data_path,
"Be_snapshot0.out.npy", data_path, "tr")
data_handler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path, "va")
data_handler.add_snapshot("Be_snapshot2.in.npy", data_path,
"Be_snapshot2.out.npy", data_path, "te")
data_handler.add_snapshot(
"Be_snapshot0.in.npy",
data_path,
"Be_snapshot0.out.npy",
data_path,
"tr",
)
data_handler.add_snapshot(
"Be_snapshot1.in.npy",
data_path,
"Be_snapshot1.out.npy",
data_path,
"va",
)
data_handler.add_snapshot(
"Be_snapshot2.in.npy",
data_path,
"Be_snapshot2.out.npy",
data_path,
"te",
)
data_handler.prepare_data()
printout("Read data: DONE.", min_verbosity=0)

Expand All @@ -120,16 +138,17 @@ def __run(use_gpu):
# but it is safer this way.
####################

test_parameters.network.layer_sizes = [data_handler.
input_dimension,
100,
data_handler.
output_dimension]
test_parameters.network.layer_sizes = [
data_handler.input_dimension,
100,
data_handler.output_dimension,
]

# Setup network and trainer.
test_network = mala.Network(test_parameters)
test_trainer = mala.Trainer(test_parameters, test_network,
data_handler)
test_trainer = mala.Trainer(
test_parameters, test_network, data_handler
)
starttime = time.time()
test_trainer.train_network()

Expand Down
65 changes: 45 additions & 20 deletions test/checkpoint_hyperopt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np

from mala.datahandling.data_repo import data_repo_path

data_path = os.path.join(data_repo_path, "Be2")

checkpoint_name = "test_ho"
Expand All @@ -30,8 +31,9 @@ def test_hyperopt_checkpoint(self):
hyperopt.perform_study()
new_final_test_value = hyperopt.study.best_trial.value

assert np.isclose(original_final_test_value, new_final_test_value,
atol=accuracy)
assert np.isclose(
original_final_test_value, new_final_test_value, atol=accuracy
)

@staticmethod
def __original_setup(n_trials):
Expand Down Expand Up @@ -84,12 +86,27 @@ def __original_setup(n_trials):
data_handler = mala.DataHandler(test_parameters)

# Add all the snapshots we want to use in to the list.
data_handler.add_snapshot("Be_snapshot0.in.npy", data_path,
"Be_snapshot0.out.npy", data_path, "tr")
data_handler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path, "va")
data_handler.add_snapshot("Be_snapshot2.in.npy", data_path,
"Be_snapshot2.out.npy", data_path, "te")
data_handler.add_snapshot(
"Be_snapshot0.in.npy",
data_path,
"Be_snapshot0.out.npy",
data_path,
"tr",
)
data_handler.add_snapshot(
"Be_snapshot1.in.npy",
data_path,
"Be_snapshot1.out.npy",
data_path,
"va",
)
data_handler.add_snapshot(
"Be_snapshot2.in.npy",
data_path,
"Be_snapshot2.out.npy",
data_path,
"te",
)
data_handler.prepare_data()
printout("Read data: DONE.", min_verbosity=0)

Expand All @@ -105,20 +122,28 @@ def __original_setup(n_trials):
test_hp_optimizer = mala.HyperOpt(test_parameters, data_handler)

# Learning rate will be optimized.
test_hp_optimizer.add_hyperparameter("float", "learning_rate",
0.0000001, 0.01)
test_hp_optimizer.add_hyperparameter(
"float", "learning_rate", 0.0000001, 0.01
)

# Number of neurons per layer will be optimized.
test_hp_optimizer.add_hyperparameter("int", "ff_neurons_layer_00", 10, 100)
test_hp_optimizer.add_hyperparameter("int", "ff_neurons_layer_01", 10, 100)
test_hp_optimizer.add_hyperparameter(
"int", "ff_neurons_layer_00", 10, 100
)
test_hp_optimizer.add_hyperparameter(
"int", "ff_neurons_layer_01", 10, 100
)

# Choices for activation function at each layer will be optimized.
test_hp_optimizer.add_hyperparameter("categorical", "layer_activation_00",
choices=["ReLU", "Sigmoid"])
test_hp_optimizer.add_hyperparameter("categorical", "layer_activation_01",
choices=["ReLU", "Sigmoid"])
test_hp_optimizer.add_hyperparameter("categorical", "layer_activation_02",
choices=["ReLU", "Sigmoid"])
test_hp_optimizer.add_hyperparameter(
"categorical", "layer_activation_00", choices=["ReLU", "Sigmoid"]
)
test_hp_optimizer.add_hyperparameter(
"categorical", "layer_activation_01", choices=["ReLU", "Sigmoid"]
)
test_hp_optimizer.add_hyperparameter(
"categorical", "layer_activation_02", choices=["ReLU", "Sigmoid"]
)

# Perform hyperparameter optimization.
printout("Starting Hyperparameter optimization.", min_verbosity=0)
Expand All @@ -136,7 +161,7 @@ def __resume_checkpoint():
The hyperopt object.
"""
loaded_params, new_datahandler, new_hyperopt = \
loaded_params, new_datahandler, new_hyperopt = (
mala.HyperOptOptuna.resume_checkpoint(checkpoint_name)
)
return new_hyperopt

Loading

0 comments on commit 36a6c8b

Please sign in to comment.