-
Notifications
You must be signed in to change notification settings - Fork 3
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 #30 from aai-institute/feature/optuna
Feature: Optuna
- Loading branch information
Showing
8 changed files
with
201 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,7 @@ dmypy.json | |
|
||
# Temporary | ||
runs/ | ||
*.db | ||
|
||
# Docs | ||
docs_build |
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 |
---|---|---|
|
@@ -74,6 +74,7 @@ install_requires = | |
dadaptation==3.1 | ||
matplotlib | ||
pandas | ||
optuna==3.5.0 | ||
|
||
|
||
[options.packages.find] | ||
|
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,24 @@ | ||
""" | ||
Benchmarks for operator learning. | ||
""" | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Benchmark(ABC): | ||
"""Benchmark base class.""" | ||
|
||
@abstractmethod | ||
def train_dataset(self): | ||
"""Return training data set.""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def test_dataset(self): | ||
"""Return test data set.""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def metric(self): | ||
"""Return metric.""" | ||
raise NotImplementedError |
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,39 @@ | ||
"""Sine benchmark.""" | ||
|
||
from continuity.benchmarks import Benchmark | ||
from continuity.data import DataSet, split | ||
from continuity.data.datasets import Sine | ||
from continuity.operators.losses import Loss, MSELoss | ||
|
||
|
||
class SineBenchmark(Benchmark): | ||
"""Sine benchmark.""" | ||
|
||
def __init__(self): | ||
self.num_sensors = 32 | ||
self.size = 100 | ||
self.batch_size = 1 | ||
|
||
self.dataset = Sine( | ||
num_sensors=32, | ||
size=100, | ||
batch_size=1, | ||
) | ||
|
||
self.train_dataset, self.test_dataset = split(self.dataset, 0.9) | ||
|
||
def dataset(self) -> DataSet: | ||
"""Return data set.""" | ||
return self.dataset | ||
|
||
def train_dataset(self) -> DataSet: | ||
"""Return training data set.""" | ||
return self.train_dataset | ||
|
||
def test_dataset(self) -> DataSet: | ||
"""Return test data set.""" | ||
return self.test_dataset | ||
|
||
def metric(self) -> Loss: | ||
"""Return metric.""" | ||
return MSELoss() |
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,59 @@ | ||
import torch | ||
from continuity.benchmarks.sine import SineBenchmark | ||
from continuity.callbacks import OptunaCallback | ||
from continuity.data import split, dataset_loss | ||
from continuity.operators import DeepONet | ||
import optuna | ||
|
||
# Set random seed | ||
torch.manual_seed(0) | ||
|
||
|
||
def test_optuna(): | ||
def objective(trial): | ||
trunk_width = trial.suggest_int("trunk_width", 4, 16) | ||
trunk_depth = trial.suggest_int("trunk_depth", 4, 16) | ||
num_epochs = trial.suggest_int("num_epochs", 1, 10) | ||
lr = trial.suggest_float("lr", 1e-4, 1e-3) | ||
|
||
# Data set | ||
benchmark = SineBenchmark() | ||
|
||
# Train/val split | ||
train_dataset, val_dataset = split(benchmark.train_dataset, 0.9) | ||
|
||
# Operator | ||
operator = DeepONet( | ||
benchmark.dataset.num_sensors, | ||
benchmark.dataset.coordinate_dim, | ||
benchmark.dataset.num_channels, | ||
trunk_width=trunk_width, | ||
trunk_depth=trunk_depth, | ||
) | ||
|
||
# Optimizer | ||
optimizer = torch.optim.Adam(operator.parameters(), lr=lr) | ||
|
||
operator.compile(optimizer, verbose=False) | ||
operator.fit( | ||
train_dataset, epochs=num_epochs, callbacks=[OptunaCallback(trial)] | ||
) | ||
|
||
loss_val = dataset_loss(val_dataset, operator, benchmark.metric()) | ||
print(f"loss/val: {loss_val:.4e}") | ||
|
||
return loss_val | ||
|
||
# Run hyperparameter optimization | ||
name = "test_optuna" | ||
study = optuna.create_study( | ||
direction="minimize", | ||
study_name=name, | ||
storage=f"sqlite:///{name}.db", | ||
load_if_exists=True, | ||
) | ||
study.optimize(objective, n_trials=10) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_optuna() |