From 286c313513ad48b86727cff797c1e50e1d6b1c8b Mon Sep 17 00:00:00 2001 From: mbarbetti Date: Fri, 19 Jul 2024 18:22:43 +0200 Subject: [PATCH] updates for issue #10 --- scripts/train_ANN_isMuon.py | 4 ++-- scripts/train_GAN_GlobalPID-im.py | 4 ++-- scripts/train_GAN_GlobalPID-nm.py | 4 ++-- scripts/train_GAN_Muon.py | 4 ++-- scripts/train_GAN_Rich.py | 4 ++-- src/pidgan/algorithms/k3/BceGAN_ALP.py | 2 ++ src/pidgan/algorithms/k3/GAN.py | 3 ++- src/pidgan/algorithms/k3/WGAN_ALP.py | 2 ++ tests/algorithms/test_BceGAN.py | 26 ++++++++++++++++++-------- tests/algorithms/test_BceGAN_ALP.py | 26 ++++++++++++++++++-------- tests/algorithms/test_BceGAN_GP.py | 26 ++++++++++++++++++-------- tests/algorithms/test_CramerGAN.py | 19 +++++++++++-------- tests/algorithms/test_GAN.py | 26 ++++++++++++++++++-------- tests/algorithms/test_LSGAN.py | 26 ++++++++++++++++++-------- tests/algorithms/test_WGAN.py | 26 ++++++++++++++++++-------- tests/algorithms/test_WGAN_ALP.py | 26 ++++++++++++++++++-------- tests/algorithms/test_WGAN_GP.py | 26 ++++++++++++++++++-------- 17 files changed, 171 insertions(+), 83 deletions(-) diff --git a/scripts/train_ANN_isMuon.py b/scripts/train_ANN_isMuon.py index d0b898f..9e461d8 100644 --- a/scripts/train_ANN_isMuon.py +++ b/scripts/train_ANN_isMuon.py @@ -1,4 +1,6 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import yaml import pickle import keras as k @@ -20,8 +22,6 @@ from pidgan.utils.preprocessing import invertColumnTransformer from pidgan.utils.reports import initHPSingleton -os.environ["KERAS_BACKEND"] = "tensorflow" - DTYPE = np.float32 BATCHSIZE = 2048 diff --git a/scripts/train_GAN_GlobalPID-im.py b/scripts/train_GAN_GlobalPID-im.py index 95b88c9..7149ef9 100644 --- a/scripts/train_GAN_GlobalPID-im.py +++ b/scripts/train_GAN_GlobalPID-im.py @@ -1,4 +1,6 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import yaml import pickle import keras as k @@ -23,8 +25,6 @@ from pidgan.utils.preprocessing import invertColumnTransformer from pidgan.utils.reports import initHPSingleton -os.environ["KERAS_BACKEND"] = "tensorflow" - DTYPE = np.float32 BATCHSIZE = 2048 diff --git a/scripts/train_GAN_GlobalPID-nm.py b/scripts/train_GAN_GlobalPID-nm.py index 1a0a445..849a637 100644 --- a/scripts/train_GAN_GlobalPID-nm.py +++ b/scripts/train_GAN_GlobalPID-nm.py @@ -1,4 +1,6 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import yaml import pickle import keras as k @@ -23,8 +25,6 @@ from pidgan.utils.preprocessing import invertColumnTransformer from pidgan.utils.reports import initHPSingleton -os.environ["KERAS_BACKEND"] = "tensorflow" - DTYPE = np.float32 BATCHSIZE = 2048 diff --git a/scripts/train_GAN_Muon.py b/scripts/train_GAN_Muon.py index d5510cd..acbe0e4 100644 --- a/scripts/train_GAN_Muon.py +++ b/scripts/train_GAN_Muon.py @@ -1,4 +1,6 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import yaml import pickle import keras as k @@ -23,8 +25,6 @@ from pidgan.utils.preprocessing import invertColumnTransformer from pidgan.utils.reports import initHPSingleton -os.environ["KERAS_BACKEND"] = "tensorflow" - DTYPE = np.float32 BATCHSIZE = 2048 diff --git a/scripts/train_GAN_Rich.py b/scripts/train_GAN_Rich.py index e74f391..d78c71a 100644 --- a/scripts/train_GAN_Rich.py +++ b/scripts/train_GAN_Rich.py @@ -1,4 +1,6 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import yaml import pickle import keras as k @@ -23,8 +25,6 @@ from pidgan.utils.preprocessing import invertColumnTransformer from pidgan.utils.reports import initHPSingleton -os.environ["KERAS_BACKEND"] = "tensorflow" - DTYPE = np.float32 BATCHSIZE = 2048 diff --git a/src/pidgan/algorithms/k3/BceGAN_ALP.py b/src/pidgan/algorithms/k3/BceGAN_ALP.py index c7a69a8..b0a0826 100644 --- a/src/pidgan/algorithms/k3/BceGAN_ALP.py +++ b/src/pidgan/algorithms/k3/BceGAN_ALP.py @@ -41,6 +41,7 @@ def compile( virtual_adv_direction_upds=1, referee_optimizer=None, referee_upds_per_batch=None, + jit_compile=False, ) -> None: super().compile( metrics=metrics, @@ -50,6 +51,7 @@ def compile( discriminator_upds_per_batch=discriminator_upds_per_batch, referee_optimizer=referee_optimizer, referee_upds_per_batch=referee_upds_per_batch, + jit_compile=jit_compile, ) # Virtual adversarial direction updates diff --git a/src/pidgan/algorithms/k3/GAN.py b/src/pidgan/algorithms/k3/GAN.py index 853f610..94cc28a 100644 --- a/src/pidgan/algorithms/k3/GAN.py +++ b/src/pidgan/algorithms/k3/GAN.py @@ -96,8 +96,9 @@ def compile( discriminator_upds_per_batch=1, referee_optimizer=None, referee_upds_per_batch=None, + jit_compile=False, ) -> None: - super().compile(weighted_metrics=[]) + super().compile(weighted_metrics=[], jit_compile=jit_compile) # Metrics if not self._model_is_built: diff --git a/src/pidgan/algorithms/k3/WGAN_ALP.py b/src/pidgan/algorithms/k3/WGAN_ALP.py index e9784d3..0bb3838 100644 --- a/src/pidgan/algorithms/k3/WGAN_ALP.py +++ b/src/pidgan/algorithms/k3/WGAN_ALP.py @@ -41,6 +41,7 @@ def compile( virtual_adv_direction_upds=1, referee_optimizer=None, referee_upds_per_batch=None, + jit_compile=False, ) -> None: super().compile( metrics=metrics, @@ -50,6 +51,7 @@ def compile( discriminator_upds_per_batch=discriminator_upds_per_batch, referee_optimizer=referee_optimizer, referee_upds_per_batch=referee_upds_per_batch, + jit_compile=jit_compile, ) # Virtual adversarial direction updates diff --git a/tests/algorithms/test_BceGAN.py b/tests/algorithms/test_BceGAN.py index 9001fc7..68645ae 100644 --- a/tests/algorithms/test_BceGAN.py +++ b/tests/algorithms/test_BceGAN.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import BinaryCrossentropy as BCE -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -120,6 +120,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -169,6 +171,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("sample_weight", [w, None]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, build_first): + import keras as k from pidgan.algorithms import BceGAN if sample_weight is not None: @@ -236,14 +239,21 @@ def test_model_train(referee, sample_weight, build_first): for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) + if not build_first: + for key in ["g_loss", "d_loss", "bce"]: + comparison = ( + np.array(train.history[key]) != np.array(train.history[f"val_{key}"]) + ) + assert comparison.all() + @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001) diff --git a/tests/algorithms/test_BceGAN_ALP.py b/tests/algorithms/test_BceGAN_ALP.py index c81ca80..baed266 100644 --- a/tests/algorithms/test_BceGAN_ALP.py +++ b/tests/algorithms/test_BceGAN_ALP.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import BinaryCrossentropy as BCE -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -117,6 +117,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -169,6 +171,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("lipschitz_penalty_strategy", ["two-sided", "one-sided"]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_first): + import keras as k from pidgan.algorithms import BceGAN_ALP if sample_weight is not None: @@ -236,14 +239,21 @@ def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_f for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) + if not build_first: + for key in ["g_loss", "d_loss", "bce"]: + comparison = ( + np.array(train.history[key]) != np.array(train.history[f"val_{key}"]) + ) + assert comparison.all() + @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001) diff --git a/tests/algorithms/test_BceGAN_GP.py b/tests/algorithms/test_BceGAN_GP.py index aab7367..5317b0a 100644 --- a/tests/algorithms/test_BceGAN_GP.py +++ b/tests/algorithms/test_BceGAN_GP.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import BinaryCrossentropy as BCE -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -117,6 +117,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -167,6 +169,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("lipschitz_penalty_strategy", ["two-sided", "one-sided"]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_first): + import keras as k from pidgan.algorithms import BceGAN_GP if sample_weight is not None: @@ -233,14 +236,21 @@ def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_f for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) + if not build_first: + for key in ["g_loss", "d_loss", "bce"]: + comparison = ( + np.array(train.history[key]) != np.array(train.history[f"val_{key}"]) + ) + assert comparison.all() + @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001) diff --git a/tests/algorithms/test_CramerGAN.py b/tests/algorithms/test_CramerGAN.py index 3665a69..cee797a 100644 --- a/tests/algorithms/test_CramerGAN.py +++ b/tests/algorithms/test_CramerGAN.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import WassersteinDistance as Wass_dist -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -118,6 +118,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["bce"], [Wass_dist()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -168,6 +170,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("lipschitz_penalty_strategy", ["two-sided", "one-sided"]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_first): + import keras as k from pidgan.algorithms import CramerGAN if sample_weight is not None: @@ -233,14 +236,14 @@ def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_f for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) @pytest.mark.parametrize("metrics", [["wass_dist"], [Wass_dist()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001) diff --git a/tests/algorithms/test_GAN.py b/tests/algorithms/test_GAN.py index b98bee8..39bc736 100644 --- a/tests/algorithms/test_GAN.py +++ b/tests/algorithms/test_GAN.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import BinaryCrossentropy as BCE -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -117,6 +117,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -167,6 +169,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("use_original_loss", [True, False]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, use_original_loss, build_first): + import keras as k from pidgan.algorithms import GAN if sample_weight is not None: @@ -233,14 +236,21 @@ def test_model_train(referee, sample_weight, use_original_loss, build_first): for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) + if not build_first: + for key in ["g_loss", "d_loss", "bce"]: + comparison = ( + np.array(train.history[key]) != np.array(train.history[f"val_{key}"]) + ) + assert comparison.all() + @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001) diff --git a/tests/algorithms/test_LSGAN.py b/tests/algorithms/test_LSGAN.py index 631b21e..49c4178 100644 --- a/tests/algorithms/test_LSGAN.py +++ b/tests/algorithms/test_LSGAN.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import BinaryCrossentropy as BCE -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -117,6 +117,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -167,6 +169,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("minimize_pearson_chi2", [True, False]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, minimize_pearson_chi2, build_first): + import keras as k from pidgan.algorithms import LSGAN if sample_weight is not None: @@ -233,14 +236,21 @@ def test_model_train(referee, sample_weight, minimize_pearson_chi2, build_first) for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) + if not build_first: + for key in ["g_loss", "d_loss", "bce"]: + comparison = ( + np.array(train.history[key]) != np.array(train.history[f"val_{key}"]) + ) + assert comparison.all() + @pytest.mark.parametrize("metrics", [["bce"], [BCE()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001) diff --git a/tests/algorithms/test_WGAN.py b/tests/algorithms/test_WGAN.py index e8e8f70..0a5dc2e 100644 --- a/tests/algorithms/test_WGAN.py +++ b/tests/algorithms/test_WGAN.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import WassersteinDistance as Wass_dist -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -114,6 +114,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["wass_dist"], [Wass_dist()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -163,6 +165,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("sample_weight", [w, None]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, build_first): + import keras as k from pidgan.algorithms import WGAN if sample_weight is not None: @@ -228,14 +231,21 @@ def test_model_train(referee, sample_weight, build_first): for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) + if not build_first: + for key in ["g_loss", "d_loss", "wass_dist"]: + comparison = ( + np.array(train.history[key]) != np.array(train.history[f"val_{key}"]) + ) + assert comparison.all() + @pytest.mark.parametrize("metrics", [["wass_dist"], [Wass_dist()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001) diff --git a/tests/algorithms/test_WGAN_ALP.py b/tests/algorithms/test_WGAN_ALP.py index 07811ba..c8e27cf 100644 --- a/tests/algorithms/test_WGAN_ALP.py +++ b/tests/algorithms/test_WGAN_ALP.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import WassersteinDistance as Wass_dist -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -118,6 +118,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["wass_dist"], [Wass_dist()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -170,6 +172,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("lipschitz_penalty_strategy", ["two-sided", "one-sided"]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_first): + import keras as k from pidgan.algorithms import WGAN_ALP if sample_weight is not None: @@ -237,14 +240,21 @@ def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_f for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) + if not build_first: + for key in ["g_loss", "d_loss", "wass_dist"]: + comparison = ( + np.array(train.history[key]) != np.array(train.history[f"val_{key}"]) + ) + assert comparison.all() + @pytest.mark.parametrize("metrics", [["wass_dist"], [Wass_dist()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001) diff --git a/tests/algorithms/test_WGAN_GP.py b/tests/algorithms/test_WGAN_GP.py index 5076ab5..9741318 100644 --- a/tests/algorithms/test_WGAN_GP.py +++ b/tests/algorithms/test_WGAN_GP.py @@ -1,7 +1,9 @@ import os +os.environ["KERAS_BACKEND"] = "tensorflow" + import pytest import warnings -import keras as k +import numpy as np import tensorflow as tf from pidgan.players.classifiers import AuxClassifier @@ -9,14 +11,12 @@ from pidgan.players.generators import ResGenerator from pidgan.metrics import WassersteinDistance as Wass_dist -os.environ["KERAS_BACKEND"] = "tensorflow" - CHUNK_SIZE = int(1e4) BATCH_SIZE = 500 -x = tf.random.normal(shape=(CHUNK_SIZE, 4)) -y = tf.random.normal(shape=(CHUNK_SIZE, 8)) -w = tf.random.uniform(shape=(CHUNK_SIZE,)) +x = np.random.normal(size=(CHUNK_SIZE, 4)).astype("float32") +y = np.random.normal(size=(CHUNK_SIZE, 8)).astype("float32") +w = np.random.uniform(size=(CHUNK_SIZE,)).astype("float32") gen = ResGenerator( output_dim=y.shape[1], @@ -118,6 +118,8 @@ def test_model_use(referee): @pytest.mark.parametrize("build_first", [True, False]) @pytest.mark.parametrize("metrics", [["wass_dist"], [Wass_dist()], None]) def test_model_compilation(model, build_first, metrics): + import keras as k + if build_first: model(x, y) # to build the model @@ -168,6 +170,7 @@ def test_model_compilation(model, build_first, metrics): @pytest.mark.parametrize("lipschitz_penalty_strategy", ["two-sided", "one-sided"]) @pytest.mark.parametrize("build_first", [True, False]) def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_first): + import keras as k from pidgan.algorithms import WGAN_GP if sample_weight is not None: @@ -234,14 +237,21 @@ def test_model_train(referee, sample_weight, lipschitz_penalty_strategy, build_f for s in states: for entry in train.history[s]: - print(train.history) - print(f"{s}: {entry}") assert isinstance(entry, (int, float)) + if not build_first: + for key in ["g_loss", "d_loss", "wass_dist"]: + comparison = ( + np.array(train.history[key]) != np.array(train.history[f"val_{key}"]) + ) + assert comparison.all() + @pytest.mark.parametrize("metrics", [["wass_dist"], [Wass_dist()], None]) @pytest.mark.parametrize("sample_weight", [w, None]) def test_model_eval(model, metrics, sample_weight): + import keras as k + g_opt = k.optimizers.RMSprop(learning_rate=0.001) d_opt = k.optimizers.RMSprop(learning_rate=0.001) r_opt = k.optimizers.RMSprop(learning_rate=0.001)