From 86f27a6ca8988e66b11bca641c9529f622be71f6 Mon Sep 17 00:00:00 2001 From: Malik Date: Thu, 4 Apr 2024 17:04:41 -0600 Subject: [PATCH] remove large keras import --- BayesianCalibration_spm/cal_nosigma.py | 4 +++- BayesianCalibration_spm/preprocess.py | 4 ++-- pinn_spm_param/postProcess/computeError.py | 6 +++--- pinn_spm_param/postProcess/plotCorrelationPINNvsData.py | 4 ++-- pinn_spm_param/postProcess/plotData.py | 4 ++-- pinn_spm_param/postProcess/plotPINNResult.py | 4 ++-- pinn_spm_param/postProcess/plotPINNResult_movie.py | 4 ++-- pinn_spm_param/util/_losses.py | 4 ++-- pinn_spm_param/util/_rescale.py | 4 ++-- pinn_spm_param/util/custom_activations.py | 7 +++---- pinn_spm_param/util/forwardPass.py | 4 ++-- pinn_spm_param/util/init_pinn.py | 1 - pinn_spm_param/util/myNN.py | 4 ++-- pinn_spm_param/util/spm.py | 4 ++-- pinn_spm_param/util/spm_simpler.py | 4 ++-- pinn_spm_param/util/tf_lineInterp.py | 8 ++++---- pinn_spm_param/util/thermo.py | 4 ++-- pinn_spm_param/util/uocp_cs.py | 4 ++-- 18 files changed, 39 insertions(+), 39 deletions(-) diff --git a/BayesianCalibration_spm/cal_nosigma.py b/BayesianCalibration_spm/cal_nosigma.py index 252d15d..ebcd2cc 100644 --- a/BayesianCalibration_spm/cal_nosigma.py +++ b/BayesianCalibration_spm/cal_nosigma.py @@ -3,11 +3,13 @@ import os import sys -import keras import numpy as np import tensorflow as tf import tf2jax from keras import layers +from keras.backend import set_floatx + +set_floatx("float64") parser = argparse.ArgumentParser(description="BNN interface for pouch cells") parser.add_argument( diff --git a/BayesianCalibration_spm/preprocess.py b/BayesianCalibration_spm/preprocess.py index 5b1502c..ac9ff94 100644 --- a/BayesianCalibration_spm/preprocess.py +++ b/BayesianCalibration_spm/preprocess.py @@ -41,12 +41,12 @@ sys.path.append(args.utilFolder) import argument -import keras import tensorflow as tf from keras import layers, regularizers from myNN import * +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") # Read command line arguments args_spm = argument.initArg() diff --git a/pinn_spm_param/postProcess/computeError.py b/pinn_spm_param/postProcess/computeError.py index f934799..07befe2 100644 --- a/pinn_spm_param/postProcess/computeError.py +++ b/pinn_spm_param/postProcess/computeError.py @@ -8,13 +8,13 @@ from pathlib import Path import argument -import keras import tensorflow as tf from keras import layers, regularizers from myNN import * -from tensorflow import keras +from keras.backend import set_floatx + +set_floatx("float64") -keras.backend.set_floatx("float64") from forwardPass import ( from_param_list_to_str, make_data_dict, diff --git a/pinn_spm_param/postProcess/plotCorrelationPINNvsData.py b/pinn_spm_param/postProcess/plotCorrelationPINNvsData.py index 72ba82d..b39dd07 100644 --- a/pinn_spm_param/postProcess/plotCorrelationPINNvsData.py +++ b/pinn_spm_param/postProcess/plotCorrelationPINNvsData.py @@ -6,13 +6,13 @@ sys.path.append("../util") import argument -import keras import tensorflow as tf from keras import layers, regularizers from myNN import * from plotsUtil_batt import * +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") from init_pinn import initialize_nn_from_params_config, safe_load diff --git a/pinn_spm_param/postProcess/plotData.py b/pinn_spm_param/postProcess/plotData.py index eb8791c..8759d0d 100644 --- a/pinn_spm_param/postProcess/plotData.py +++ b/pinn_spm_param/postProcess/plotData.py @@ -5,13 +5,13 @@ sys.path.append("../util") import argument -import keras import tensorflow as tf from keras import layers, regularizers from myNN import * from plotsUtil_batt import * +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") print("\n\nINFO: PLOTTING DATA OBTAINED FROM FINITE DIFFERENCE\n\n") diff --git a/pinn_spm_param/postProcess/plotPINNResult.py b/pinn_spm_param/postProcess/plotPINNResult.py index 74b970b..a25f3f7 100644 --- a/pinn_spm_param/postProcess/plotPINNResult.py +++ b/pinn_spm_param/postProcess/plotPINNResult.py @@ -6,7 +6,6 @@ sys.path.append("../util") import argument -import keras import tensorflow as tf from forwardPass import ( from_param_list_to_str, @@ -17,8 +16,9 @@ from keras import layers, regularizers from myNN import * from plotsUtil_batt import * +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") print("\n\nINFO: PLOTTING RESULTS OF THE PINN TRAINING\n\n") diff --git a/pinn_spm_param/postProcess/plotPINNResult_movie.py b/pinn_spm_param/postProcess/plotPINNResult_movie.py index a4b3416..c25a58c 100644 --- a/pinn_spm_param/postProcess/plotPINNResult_movie.py +++ b/pinn_spm_param/postProcess/plotPINNResult_movie.py @@ -8,13 +8,13 @@ from pathlib import Path import argument -import keras import tensorflow as tf from keras import layers, regularizers from myNN import * from plotsUtil_batt import * +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") from forwardPass import ( from_param_list_to_str, diff --git a/pinn_spm_param/util/_losses.py b/pinn_spm_param/util/_losses.py index 6fd4647..744c625 100644 --- a/pinn_spm_param/util/_losses.py +++ b/pinn_spm_param/util/_losses.py @@ -1,10 +1,10 @@ import argument -import keras import numpy as np import tensorflow as tf from conditionalDecorator import conditional_decorator +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") # Read command line arguments args = argument.initArg() diff --git a/pinn_spm_param/util/_rescale.py b/pinn_spm_param/util/_rescale.py index 2b8e516..dbd2db3 100644 --- a/pinn_spm_param/util/_rescale.py +++ b/pinn_spm_param/util/_rescale.py @@ -1,10 +1,10 @@ import argument -import keras import numpy as np import tensorflow as tf from conditionalDecorator import conditional_decorator +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") # Read command line arguments args = argument.initArg() diff --git a/pinn_spm_param/util/custom_activations.py b/pinn_spm_param/util/custom_activations.py index ac3612f..a01e96c 100644 --- a/pinn_spm_param/util/custom_activations.py +++ b/pinn_spm_param/util/custom_activations.py @@ -1,13 +1,12 @@ import argument -import keras import numpy as np import tensorflow as tf from conditionalDecorator import conditional_decorator -from keras import backend as K from keras.layers import Activation from keras.utils import get_custom_objects +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") # Read command line arguments args = argument.initArg() @@ -24,7 +23,7 @@ def swish_activation(x): Swish activation - with beta not-traininable! """ - return x * K.sigmoid(x) + return x * tf.math.sigmoid(x) class Bswish(keras.layers.Layer): diff --git a/pinn_spm_param/util/forwardPass.py b/pinn_spm_param/util/forwardPass.py index 74fe366..cdf4d24 100644 --- a/pinn_spm_param/util/forwardPass.py +++ b/pinn_spm_param/util/forwardPass.py @@ -5,12 +5,12 @@ sys.path.append("../util") import argument -import keras import tensorflow as tf from keras import layers, regularizers from myNN import * +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") from init_pinn import initialize_nn_from_params_config diff --git a/pinn_spm_param/util/init_pinn.py b/pinn_spm_param/util/init_pinn.py index c46601e..08867e2 100644 --- a/pinn_spm_param/util/init_pinn.py +++ b/pinn_spm_param/util/init_pinn.py @@ -516,7 +516,6 @@ def initialize_nn(args, input_params): ) if not args.optimized: - import keras from keras.utils import plot_model try: diff --git a/pinn_spm_param/util/myNN.py b/pinn_spm_param/util/myNN.py index 0be9195..7e3807e 100644 --- a/pinn_spm_param/util/myNN.py +++ b/pinn_spm_param/util/myNN.py @@ -4,7 +4,6 @@ import time import argument -import keras import numpy as np import tensorflow as tf from _losses import ( @@ -25,8 +24,9 @@ from keras.layers import * from keras.models import Model from prettyPlot.progressBar import print_progress_bar +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") # Read command line arguments args = argument.initArg() diff --git a/pinn_spm_param/util/spm.py b/pinn_spm_param/util/spm.py index 3d28716..acd5ebe 100644 --- a/pinn_spm_param/util/spm.py +++ b/pinn_spm_param/util/spm.py @@ -1,11 +1,11 @@ import sys -import keras import numpy as np import tensorflow as tf from thermo import * +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") print("INFO: USING REALISTIC SPM MODEL") diff --git a/pinn_spm_param/util/spm_simpler.py b/pinn_spm_param/util/spm_simpler.py index 4f8a085..adddecf 100644 --- a/pinn_spm_param/util/spm_simpler.py +++ b/pinn_spm_param/util/spm_simpler.py @@ -1,12 +1,12 @@ import os import sys -import keras import numpy as np import tensorflow as tf from thermo import * +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") print("INFO: USING SIMPLE SPM MODEL") diff --git a/pinn_spm_param/util/tf_lineInterp.py b/pinn_spm_param/util/tf_lineInterp.py index 9983bdd..7fa06c6 100644 --- a/pinn_spm_param/util/tf_lineInterp.py +++ b/pinn_spm_param/util/tf_lineInterp.py @@ -19,11 +19,11 @@ def generateTFSpline(x, y, filename, funcname, precision="float64", mode="w+"): if mode == "w+": f.write("import argument\n") f.write("import numpy as np\n") - f.write("import keras\n") f.write("import tensorflow as tf\n") f.write("from conditionalDecorator import conditional_decorator\n") + f.write("from keras.backend import set_floatx\n") f.write("\n") - f.write(f'keras.backend.set_floatx("{precision}")\n') + f.write(f'set_floatx("{precision}")\n') f.write("\n") f.write("# Read command line arguments\n") f.write("args = argument.initArg()\n") @@ -68,11 +68,11 @@ def generateTFPoly(coeffs, filename, funcname, precision="float64", mode="w+"): if mode == "w+": f.write("import argument\n") f.write("import numpy as np\n") - f.write("import keras\n") f.write("import tensorflow as tf\n") f.write("from conditionalDecorator import conditional_decorator\n") + f.write("from keras.backend import set_floatx\n") f.write("\n") - f.write(f'keras.backend.set_floatx("{precision}")\n') + f.write(f'set_floatx("{precision}")\n') f.write("\n") f.write("# Read command line arguments\n") f.write("args = argument.initArg()\n") diff --git a/pinn_spm_param/util/thermo.py b/pinn_spm_param/util/thermo.py index 9cd62bb..d5d5f88 100644 --- a/pinn_spm_param/util/thermo.py +++ b/pinn_spm_param/util/thermo.py @@ -1,11 +1,11 @@ import sys -import keras import numpy as np import tensorflow as tf from uocp_cs import uocp_a_fun_x, uocp_c_fun_x +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") def uocp_a_simp(cs_a, csanmax): diff --git a/pinn_spm_param/util/uocp_cs.py b/pinn_spm_param/util/uocp_cs.py index 7c9d244..938d337 100644 --- a/pinn_spm_param/util/uocp_cs.py +++ b/pinn_spm_param/util/uocp_cs.py @@ -1,10 +1,10 @@ import argument -import keras import numpy as np import tensorflow as tf from conditionalDecorator import conditional_decorator +from keras.backend import set_floatx -keras.backend.set_floatx("float64") +set_floatx("float64") # Read command line arguments args = argument.initArg()