Skip to content

Commit

Permalink
Blackified examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomDefaultUser committed Apr 17, 2024
1 parent 9ce07a6 commit 7502731
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 138 deletions.
33 changes: 23 additions & 10 deletions examples/advanced/ex01_checkpoint_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mala import printout

from mala.datahandling.data_repo import data_repo_path

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

"""
Expand Down Expand Up @@ -35,15 +36,27 @@ def initial_setup():
parameters.running.checkpoint_name = "ex01_checkpoint"

data_handler = mala.DataHandler(parameters)
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_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.prepare_data()

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

test_network = mala.Network(parameters)
test_trainer = mala.Trainer(parameters, test_network, data_handler)
Expand All @@ -52,12 +65,12 @@ def initial_setup():


if mala.Trainer.run_exists("ex01_checkpoint"):
parameters, network, datahandler, trainer = \
mala.Trainer.load_run("ex01_checkpoint")
parameters, network, datahandler, trainer = mala.Trainer.load_run(
"ex01_checkpoint"
)
printout("Starting resumed training.")
else:
parameters, network, datahandler, trainer = initial_setup()
printout("Starting original training.")

trainer.train_network()

15 changes: 9 additions & 6 deletions examples/advanced/ex02_shuffle_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
parameters.data.shuffling_seed = 1234

data_shuffler = mala.DataShuffler(parameters)
data_shuffler.add_snapshot("Be_snapshot0.in.npy", data_path,
"Be_snapshot0.out.npy", data_path)
data_shuffler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path)
data_shuffler.shuffle_snapshots(complete_save_path=".",
save_name="Be_shuffled*")
data_shuffler.add_snapshot(
"Be_snapshot0.in.npy", data_path, "Be_snapshot0.out.npy", data_path
)
data_shuffler.add_snapshot(
"Be_snapshot1.in.npy", data_path, "Be_snapshot1.out.npy", data_path
)
data_shuffler.shuffle_snapshots(
complete_save_path=".", save_name="Be_shuffled*"
)
26 changes: 17 additions & 9 deletions examples/advanced/ex03_tensor_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mala import printout

from mala.datahandling.data_repo import data_repo_path

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


Expand All @@ -29,17 +30,24 @@


data_handler = mala.DataHandler(parameters)
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_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.prepare_data()
parameters.network.layer_sizes = [data_handler.input_dimension,
100,
data_handler.output_dimension]
parameters.network.layer_sizes = [
data_handler.input_dimension,
100,
data_handler.output_dimension,
]

network = mala.Network(parameters)
trainer = mala.Trainer(parameters, network, data_handler)
trainer.train_network()
printout("Run finished, launch tensorboard with \"tensorboard --logdir " +
trainer.full_visualization_path + "\"")
printout(
'Run finished, launch tensorboard with "tensorboard --logdir '
+ trainer.full_visualization_path
+ '"'
)
21 changes: 15 additions & 6 deletions examples/advanced/ex04_acsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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 All @@ -29,12 +30,20 @@
# When adding data for the ACSD analysis, add preprocessed LDOS data for
# and a calculation output for the descriptor calculation.
####################
hyperoptimizer.add_snapshot("espresso-out", os.path.join(data_path, "Be_snapshot1.out"),
"numpy", os.path.join(data_path, "Be_snapshot1.out.npy"),
target_units="1/(Ry*Bohr^3)")
hyperoptimizer.add_snapshot("espresso-out", os.path.join(data_path, "Be_snapshot2.out"),
"numpy", os.path.join(data_path, "Be_snapshot2.out.npy"),
target_units="1/(Ry*Bohr^3)")
hyperoptimizer.add_snapshot(
"espresso-out",
os.path.join(data_path, "Be_snapshot1.out"),
"numpy",
os.path.join(data_path, "Be_snapshot1.out.npy"),
target_units="1/(Ry*Bohr^3)",
)
hyperoptimizer.add_snapshot(
"espresso-out",
os.path.join(data_path, "Be_snapshot2.out"),
"numpy",
os.path.join(data_path, "Be_snapshot2.out.npy"),
target_units="1/(Ry*Bohr^3)",
)

# If you plan to plot the results (recommended for exploratory searches),
# the optimizer can return the necessary quantities to plot.
Expand Down
46 changes: 30 additions & 16 deletions examples/advanced/ex05_checkpoint_hyperparameter_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mala import printout

from mala.datahandling.data_repo import data_repo_path

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

"""
Expand All @@ -29,34 +30,47 @@ def initial_setup():
parameters.hyperparameters.checkpoint_name = "ex05_checkpoint"

data_handler = mala.DataHandler(parameters)
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_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.prepare_data()

hyperoptimizer = mala.HyperOpt(parameters, data_handler)
hyperoptimizer.add_hyperparameter("float", "learning_rate",
0.0000001, 0.01)
hyperoptimizer.add_hyperparameter(
"float", "learning_rate", 0.0000001, 0.01
)
hyperoptimizer.add_hyperparameter("int", "ff_neurons_layer_00", 10, 100)
hyperoptimizer.add_hyperparameter("int", "ff_neurons_layer_01", 10, 100)
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_00",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_01",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_02",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_00", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_01", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_02", choices=["ReLU", "Sigmoid"]
)

return parameters, data_handler, hyperoptimizer


if mala.HyperOptOptuna.checkpoint_exists("ex05_checkpoint"):
parameters, datahandler, hyperoptimizer = \
mala.HyperOptOptuna.resume_checkpoint(
"ex05_checkpoint")
parameters, datahandler, hyperoptimizer = (
mala.HyperOptOptuna.resume_checkpoint("ex05_checkpoint")
)
else:
parameters, datahandler, hyperoptimizer = initial_setup()

# Perform hyperparameter optimization.
hyperoptimizer.perform_study()

45 changes: 28 additions & 17 deletions examples/advanced/ex06_distributed_hyperparameter_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mala import printout

from mala.datahandling.data_repo import data_repo_path

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

"""
Expand Down Expand Up @@ -36,7 +37,7 @@
parameters.hyperparameters.checkpoint_name = "ex06"
parameters.hyperparameters.hyper_opt_method = "optuna"
parameters.hyperparameters.study_name = "ex06"
parameters.hyperparameters.rdb_storage = 'sqlite:///ex06.db'
parameters.hyperparameters.rdb_storage = "sqlite:///ex06.db"

# Hyperparameter optimization can be further refined by using ensemble training
# at each step and by using a different metric then the validation loss
Expand All @@ -50,27 +51,37 @@

data_handler = mala.DataHandler(parameters)

data_handler.add_snapshot("Be_snapshot1.in.npy", data_path,
"Be_snapshot1.out.npy", data_path, "tr",
calculation_output_file=
os.path.join(data_path, "Be_snapshot1.out"))
data_handler.add_snapshot("Be_snapshot2.in.npy", data_path,
"Be_snapshot2.out.npy", data_path, "va",
calculation_output_file=
os.path.join(data_path, "Be_snapshot2.out"))
data_handler.add_snapshot(
"Be_snapshot1.in.npy",
data_path,
"Be_snapshot1.out.npy",
data_path,
"tr",
calculation_output_file=os.path.join(data_path, "Be_snapshot1.out"),
)
data_handler.add_snapshot(
"Be_snapshot2.in.npy",
data_path,
"Be_snapshot2.out.npy",
data_path,
"va",
calculation_output_file=os.path.join(data_path, "Be_snapshot2.out"),
)
data_handler.prepare_data()


hyperoptimizer = mala.HyperOpt(parameters, data_handler)
hyperoptimizer.add_hyperparameter("float", "learning_rate",
0.0000001, 0.01)
hyperoptimizer.add_hyperparameter("float", "learning_rate", 0.0000001, 0.01)
hyperoptimizer.add_hyperparameter("int", "ff_neurons_layer_00", 10, 100)
hyperoptimizer.add_hyperparameter("int", "ff_neurons_layer_01", 10, 100)
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_00",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_01",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical", "layer_activation_02",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_00", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_01", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_02", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.perform_study()
hyperoptimizer.set_optimal_parameters()
60 changes: 40 additions & 20 deletions examples/advanced/ex07_advanced_hyperparameter_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mala import printout

from mala.datahandling.data_repo import data_repo_path

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

"""
Expand Down Expand Up @@ -33,30 +34,49 @@ def optimize_hyperparameters(hyper_optimizer):
data_handler = mala.DataHandler(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.")

hyperoptimizer = mala.HyperOpt(parameters, data_handler)
parameters.network.layer_sizes = [data_handler.input_dimension,
100, 100,
data_handler.output_dimension]
hyperoptimizer.add_hyperparameter("categorical", "trainingtype",
choices=["Adam", "SGD"])
hyperoptimizer.add_hyperparameter("categorical",
"layer_activation_00",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical",
"layer_activation_01",
choices=["ReLU", "Sigmoid"])
hyperoptimizer.add_hyperparameter("categorical",
"layer_activation_02",
choices=["ReLU", "Sigmoid"])
parameters.network.layer_sizes = [
data_handler.input_dimension,
100,
100,
data_handler.output_dimension,
]
hyperoptimizer.add_hyperparameter(
"categorical", "trainingtype", choices=["Adam", "SGD"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_00", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_01", choices=["ReLU", "Sigmoid"]
)
hyperoptimizer.add_hyperparameter(
"categorical", "layer_activation_02", choices=["ReLU", "Sigmoid"]
)

hyperoptimizer.perform_study()
hyperoptimizer.set_optimal_parameters()
Expand Down
Loading

0 comments on commit 7502731

Please sign in to comment.