From 4c145236bf2d7f18f1d90b1362f62e79ce462840 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Mon, 14 Nov 2022 19:49:26 -0700 Subject: [PATCH 01/12] Testing out pull request --- Examples/train_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/train_network.py b/Examples/train_network.py index c40cc95b..5fb9c321 100644 --- a/Examples/train_network.py +++ b/Examples/train_network.py @@ -25,6 +25,7 @@ def main(): "batch_size" : [4096], "network_type" : ["sph_pines_traditional"], "PINN_constraint_fcn": ["pinn_alc"], + "dtype" : ['float64'] }) @@ -45,7 +46,6 @@ def run(config): from GravNN.Networks.Callbacks import SimpleCallback from GravNN.Networks.Data import get_preprocessed_data, configure_dataset, compute_input_layer_normalization_constants from GravNN.Networks.Model import PINNGravityModel - from GravNN.Networks.Networks import load_network from GravNN.Networks.utils import populate_config_objects, configure_optimizer from GravNN.Networks.Schedules import get_schedule From 8d820b3150048eefeb130316a29e6eb8ff27b015 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Mon, 14 Nov 2022 19:51:51 -0700 Subject: [PATCH 02/12] Testing out pull request --- GravNN/Networks/Configs/Eros_Configs.py | 3 ++- GravNN/Networks/Constraints.py | 1 - GravNN/Networks/utils.py | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/GravNN/Networks/Configs/Eros_Configs.py b/GravNN/Networks/Configs/Eros_Configs.py index efe2883a..4b6d8046 100644 --- a/GravNN/Networks/Configs/Eros_Configs.py +++ b/GravNN/Networks/Configs/Eros_Configs.py @@ -26,7 +26,8 @@ def get_default_eros_config(): "u_transformer": [UniformScaler(feature_range=(-1, 1))], "a_transformer": [UniformScaler(feature_range=(-1, 1))], "dummy_transformer": [DummyScaler()], - "override" : [False] + "override" : [False], + 'ref_radius' : [Eros().radius] } return data_config \ No newline at end of file diff --git a/GravNN/Networks/Constraints.py b/GravNN/Networks/Constraints.py index 81718d15..f292f7f8 100644 --- a/GravNN/Networks/Constraints.py +++ b/GravNN/Networks/Constraints.py @@ -116,7 +116,6 @@ def pinn_ALC(f, x, training): u_xx = g1.batch_jacobian(u_x, x, experimental_use_pfor=True) accel = tf.multiply(u_x, -1.0) # u_x must be first s.t. -1 dtype is inferred - laplace = laplacian(u_xx) curl_x = tf.math.subtract(u_xx[:, 2, 1], u_xx[:, 1, 2]) diff --git a/GravNN/Networks/utils.py b/GravNN/Networks/utils.py index 249d4032..1b7c46dc 100644 --- a/GravNN/Networks/utils.py +++ b/GravNN/Networks/utils.py @@ -49,7 +49,8 @@ def set_tf_expand_memory(): if sys.platform == "win32": physical_devices = tf.config.list_physical_devices("GPU") - tf.config.experimental.set_memory_growth(physical_devices[0], enable=True) + if physical_devices: + tf.config.experimental.set_memory_growth(physical_devices[0], enable=True) return tf From 1961c044ffee162a64e9c71a1d90fa0af058d3ad Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Mon, 14 Nov 2022 20:18:59 -0700 Subject: [PATCH 03/12] First pull request --- Examples/train_network.py | 2 +- GravNN/Networks/Configs/Network_Configs.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Examples/train_network.py b/Examples/train_network.py index 5fb9c321..d78b4626 100644 --- a/Examples/train_network.py +++ b/Examples/train_network.py @@ -14,7 +14,7 @@ def main(): config = get_default_eros_config() # hyperparameters which overwrite defaults - hparams = PINN_III() + hparams = PINN_IV() hparams.update(ReduceLrOnPlateauConfig()) hparams.update({ "grav_file" : [Eros().obj_8k], diff --git a/GravNN/Networks/Configs/Network_Configs.py b/GravNN/Networks/Configs/Network_Configs.py index 048e3575..6c3393aa 100644 --- a/GravNN/Networks/Configs/Network_Configs.py +++ b/GravNN/Networks/Configs/Network_Configs.py @@ -67,3 +67,13 @@ def PINN_III(): config.update(network_config) return config +def PINN_IV(): + config = PINN_III() + network_config = { + "layers" : [3, 100, 100, 100, 3], + "batch_size" : [1024], + "learning_rate" : [0.001*5] + } + config.update(network_config) + return config + From 0fb85da245852b066413650fc6f169bcdce65144 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Mon, 14 Nov 2022 21:08:34 -0700 Subject: [PATCH 04/12] Adding in callbacks --- Examples/train_network.py | 14 ++++- Examples/visualize_networks_comparison.py | 59 +++++++++++++++++++ GravNN/Networks/Callbacks.py | 70 +++++++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 Examples/visualize_networks_comparison.py diff --git a/Examples/train_network.py b/Examples/train_network.py index d78b4626..196cc7f8 100644 --- a/Examples/train_network.py +++ b/Examples/train_network.py @@ -1,4 +1,5 @@ import multiprocessing as mp +from GravNN.Networks.Callbacks import LossComponentsCallback from GravNN.Networks.script_utils import save_training from GravNN.Networks.utils import configure_run_args from GravNN.Networks.Configs import * @@ -41,6 +42,7 @@ def run(config): # Tensorflow dependent functions must be defined inside of # run function for thread-safe behavior. import numpy as np + import pandas as pd np.random.seed(config['seed'][0]) from GravNN.Networks.utils import configure_tensorflow from GravNN.Networks.Callbacks import SimpleCallback @@ -65,6 +67,7 @@ def run(config): # Train network callback = SimpleCallback(config['batch_size'][0]) + loss_callback = LossComponentsCallback(config['batch_size'][0]) schedule = get_schedule(config) history = model.fit( @@ -72,9 +75,18 @@ def run(config): epochs=config["epochs"][0], verbose=0, validation_data=val_dataset, - callbacks=[callback, schedule], + callbacks=[callback, loss_callback, schedule], ) history.history["time_delta"] = callback.time_delta + model.history = history + + # Saving history object to a dataframe for use in plots + hist_df = pd.DataFrame(history.history) + + hist_csv_file = 'history_percent.csv' + with open(hist_csv_file, mode='w') as f: + hist_df.to_csv(f) + model.save(df_file=None, history=history, transformers=transformers) # Appends the model config to a perscribed df diff --git a/Examples/visualize_networks_comparison.py b/Examples/visualize_networks_comparison.py new file mode 100644 index 00000000..cf337ca3 --- /dev/null +++ b/Examples/visualize_networks_comparison.py @@ -0,0 +1,59 @@ +''' +Plot visualization of different model hyperparameters against their corresponding errors +''' + +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np + +# Read to dataframe +column_names = ["Model", "Error", "Error_STD", "Time", "Layers", "Batch_Size", "Learning_Rate", "Activation"] +df = pd.read_csv('test_hyperparameters.txt', sep=",", names=column_names) + +# Check for nulls and drop +df = df.dropna() + +# Make graphs +print("Smallest Error: \n") +best_models = df.nsmallest(20, 'Error') +best_batch_size = best_models["Batch_Size"].mode() +best_learning_rate = best_models["Learning_Rate"].mode() +best_activation = best_models["Activation"].mode() +best_layers = best_models["Layers"].mode() +print(best_models["Layers"]) +print(best_models["Layers"].nsmallest()) + +print("Best Models: \n") +print(best_models) +print("Batch Size: " + str(best_batch_size)) +print("Learning Rate: " + str(best_learning_rate)) +print("Activation: " + str(best_activation)) +print("Layers: " + str(best_layers)) + +plt.figure(1) +plt.plot(df["Learning_Rate"].tolist(), df["Error"].tolist(), 'bo') +plt.xlabel("Learning Rate") +plt.ylabel("Error") +plt.title("Learning Rate vs. Error") +plt.show() + +plt.figure(2) +plt.plot(df["Batch_Size"].tolist(), df["Error"].tolist(), 'bo') +plt.xlabel("Batch Size") +plt.ylabel("Error") +plt.title("Batch Size vs. Error") +plt.show() + +plt.figure(3) +plt.plot(df["Layers"].tolist(), df["Error"].tolist(), 'bo') +plt.xlabel("Layers") +plt.ylabel("Error") +plt.title("Layers vs. Error") +plt.show() + +plt.figure(4) +plt.plot(df["Activation"].tolist(), df["Error"].tolist(), 'bo') +plt.xlabel("Activation") +plt.ylabel("Error") +plt.title("Activation vs. Error") +plt.show() \ No newline at end of file diff --git a/GravNN/Networks/Callbacks.py b/GravNN/Networks/Callbacks.py index fc227e33..5af874da 100644 --- a/GravNN/Networks/Callbacks.py +++ b/GravNN/Networks/Callbacks.py @@ -91,6 +91,76 @@ def on_train_end(self, logs=None): self.time_delta = np.round(self.end_time - self.train_start, 2) +class LossComponentsCallback(tf.keras.callbacks.Callback): + """Loss Components Callback that prints out loss component metrics every 100 epochs""" + def __init__(self, batch_size, print_interval=100): + super().__init__() + self.batch_size = batch_size + self.print_interval = print_interval + + self.N_train_samples = 0 + self.loss_components = [0, 0, 0, 0, 0, 0, 0] + + self.N_val_samples = 0 + self.val_loss_components = [0, 0, 0, 0, 0, 0, 0] + + def incremental_average_loss(self, avg, val, N, M): + old_avg = [N * i for i in avg] + new_avg = [sum(col)/len(col) for col in zip(*val)] + full_new_avg = [M * i for i in new_avg] + + avg_list = [old_avg, full_new_avg] + final_avg = [] + for i in range(len(full_new_avg)): + final_avg.append(full_new_avg[i] + old_avg[i]) + final_final_avg = [(i / (N+M)) for i in final_avg] + return final_final_avg + + def on_train_batch_end(self, batch, logs=None): + self.N_train_samples += self.batch_size + self.loss_components = self.incremental_average_loss( + self.loss_components, + logs['loss_components'], + self.N_train_samples, + self.batch_size + ) + + def on_test_batch_end(self, batch, logs=None): + self.N_val_samples += self.batch_size + self.val_loss_components = self.incremental_average_loss( + self.val_loss_components, + logs['loss_components'], + self.N_train_samples, + self.batch_size + ) + + def on_epoch_begin(self,epoch,logs=None): + self.N_train_samples = 0 + self.loss_components = [0, 0, 0, 0, 0, 0, 0] + + self.N_val_samples = 0 + self.val_loss_components = [0, 0, 0, 0, 0, 0, 0] + + def on_epoch_end(self, epoch, logs=None): + if epoch % self.print_interval == 0: + print("Loss Components: ") + print(self.loss_components) + print("Validation Loss Components: ") + print(self.val_loss_components) + + # Overwrite batch logs for epoch logs (to be saved in history obj) + logs['loss_components'] = self.loss_components + logs['val_loss_components'] = self.val_loss_components + + def on_train_begin(self, logs=None): + self.train_start = time.time() + self.start_time = time.time() + + def on_train_end(self, logs=None): + self.end_time = time.time() + self.time_delta = np.round(self.end_time - self.train_start, 2) + + class GradientCallback(tf.keras.callbacks.Callback): """Callback that plots out the gradients for each hidden layer after every 1000 epochs""" From 77597d4c29aad14262fcd7a748a7b31a5ed389f6 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Thu, 15 Dec 2022 21:28:58 -0700 Subject: [PATCH 05/12] Trying to get git to update...changed to PINN_III --- Examples/train_network.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/Examples/train_network.py b/Examples/train_network.py index 196cc7f8..8b6a9c0b 100644 --- a/Examples/train_network.py +++ b/Examples/train_network.py @@ -15,7 +15,7 @@ def main(): config = get_default_eros_config() # hyperparameters which overwrite defaults - hparams = PINN_IV() + hparams = PINN_III() hparams.update(ReduceLrOnPlateauConfig()) hparams.update({ "grav_file" : [Eros().obj_8k], @@ -80,15 +80,6 @@ def run(config): history.history["time_delta"] = callback.time_delta model.history = history - # Saving history object to a dataframe for use in plots - hist_df = pd.DataFrame(history.history) - - hist_csv_file = 'history_percent.csv' - with open(hist_csv_file, mode='w') as f: - hist_df.to_csv(f) - - model.save(df_file=None, history=history, transformers=transformers) - # Appends the model config to a perscribed df return model.config From c115a3775f7b2af64b3eb0740ccf0c6026d28ff6 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Thu, 15 Dec 2022 21:34:50 -0700 Subject: [PATCH 06/12] Adding the loss components to the log in Model.py --- GravNN/Networks/Model.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/GravNN/Networks/Model.py b/GravNN/Networks/Model.py index 90441aa1..8cc3a399 100644 --- a/GravNN/Networks/Model.py +++ b/GravNN/Networks/Model.py @@ -86,7 +86,7 @@ def train_step_fcn(self, data): rms_components = compute_rms_components(y_hat, y) percent_components = compute_percent_error(y_hat, y) - + updated_rms_components = self.scale_loss( tf.reduce_mean(rms_components,0), self.adaptive_constant ) @@ -120,11 +120,12 @@ def train_step_fcn(self, data): self.optimizer.apply_gradients([ (grad, var) for (grad, var) in zip(gradients, self.network.trainable_variables) if grad is not None ]) - return { "loss": loss, "percent_mean": tf.reduce_mean(percent_components), - "percent_max": tf.reduce_max(percent_components) + "percent_max": tf.reduce_max(percent_components), + "loss_components" : rms_components, + "percent_components" : percent_components # "adaptive_constant": adaptive_constant, } # 'grads' : grad_comp_list} @@ -140,7 +141,9 @@ def test_step_fcn(self, data): loss = self.loss_fcn(rms_components, percent_components) return {"loss": loss, "percent_mean": tf.reduce_mean(percent_components), - "percent_max": tf.reduce_max(percent_components) + "percent_max": tf.reduce_max(percent_components), + "loss_components" : rms_components, + "percent_components" : percent_components } # JIT wrappers From fd18a5270ac2df49e69792c124155603dea1ebc2 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Thu, 15 Dec 2022 21:37:07 -0700 Subject: [PATCH 07/12] Moved visualize_loss.py --- Scripts/Networks/visualize_loss.py | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Scripts/Networks/visualize_loss.py diff --git a/Scripts/Networks/visualize_loss.py b/Scripts/Networks/visualize_loss.py new file mode 100644 index 00000000..a4cb3f78 --- /dev/null +++ b/Scripts/Networks/visualize_loss.py @@ -0,0 +1,56 @@ +''' Visualize the magnitudes of each loss component against one another''' +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np +import ast + +df = pd.read_csv('history.csv', sep=",") +loss_components = [] +for i in range(len(df['loss_components'])): + to_ints = ast.literal_eval(df['loss_components'][i]) + loss_components.append(to_ints) + +percent_components = [] +for i in range(len(df['loss_components'])): + to_ints = ast.literal_eval(df['loss_components'][i]) + loss_components.append(to_ints) + +general_x = [j[0] for j in loss_components] +general_y = [j[1] for j in loss_components] +general_z = [j[2] for j in loss_components] +laplace = [j[3] for j in loss_components] +curl_x = [j[4] for j in loss_components] +curl_y = [j[5] for j in loss_components] +curl_z = [j[6] for j in loss_components ] + +general_x_percent = [j[0] for j in percent_components] +general_y_percent = [j[1] for j in percent_components] +general_z_percent = [j[2] for j in percent_components] +laplace_percent = [j[3] for j in percent_components] +curl_x_percent = [j[4] for j in percent_components] +curl_y_percent = [j[5] for j in percent_components] +curl_z_percent = [j[6] for j in percent_components] + +epochs = range(1,100) + +plt.plot(epochs, general_x, 'b', label='General Loss X') +plt.plot(epochs, general_y, 'g', label='General Loss Y') +plt.plot(epochs, general_z, 'r', label='General Loss Z') +plt.plot(epochs, laplace, 'c', label='Laplace Loss') +plt.plot(epochs, curl_x, 'm', label='Curl Loss X') +plt.plot(epochs, curl_y, 'y', label='Curl Loss Y') +plt.plot(epochs, curl_z, 'k', label='Curl Loss Z') + +plt.plot(epochs, general_x_percent, 'b', label='General Loss X Percent') +plt.plot(epochs, general_y_percent, 'g', label='General Loss Y Percent') +plt.plot(epochs, general_z_percent, 'r', label='General Loss Z Percent') +plt.plot(epochs, laplace_percent, 'c', label='Laplace Loss Percent') +plt.plot(epochs, curl_x_percent, 'm', label='Curl Loss X Percent') +plt.plot(epochs, curl_y_percent, 'y', label='Curl Loss Y Percent') +plt.plot(epochs, curl_z_percent, 'k', label='Curl Loss Z Percent') + +plt.title('Training and Validation Loss') +plt.xlabel('Epochs') +plt.ylabel('Loss') +plt.legend() +plt.show() \ No newline at end of file From 63c65a8ce0ad328ffa06bf5b54ca1c37532ffacc Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Thu, 15 Dec 2022 21:37:22 -0700 Subject: [PATCH 08/12] Moved visualize_networks_comparison.py --- .../Networks/visualize_networks_comparison.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Scripts/Networks/visualize_networks_comparison.py diff --git a/Scripts/Networks/visualize_networks_comparison.py b/Scripts/Networks/visualize_networks_comparison.py new file mode 100644 index 00000000..cf337ca3 --- /dev/null +++ b/Scripts/Networks/visualize_networks_comparison.py @@ -0,0 +1,59 @@ +''' +Plot visualization of different model hyperparameters against their corresponding errors +''' + +import pandas as pd +import matplotlib.pyplot as plt +import numpy as np + +# Read to dataframe +column_names = ["Model", "Error", "Error_STD", "Time", "Layers", "Batch_Size", "Learning_Rate", "Activation"] +df = pd.read_csv('test_hyperparameters.txt', sep=",", names=column_names) + +# Check for nulls and drop +df = df.dropna() + +# Make graphs +print("Smallest Error: \n") +best_models = df.nsmallest(20, 'Error') +best_batch_size = best_models["Batch_Size"].mode() +best_learning_rate = best_models["Learning_Rate"].mode() +best_activation = best_models["Activation"].mode() +best_layers = best_models["Layers"].mode() +print(best_models["Layers"]) +print(best_models["Layers"].nsmallest()) + +print("Best Models: \n") +print(best_models) +print("Batch Size: " + str(best_batch_size)) +print("Learning Rate: " + str(best_learning_rate)) +print("Activation: " + str(best_activation)) +print("Layers: " + str(best_layers)) + +plt.figure(1) +plt.plot(df["Learning_Rate"].tolist(), df["Error"].tolist(), 'bo') +plt.xlabel("Learning Rate") +plt.ylabel("Error") +plt.title("Learning Rate vs. Error") +plt.show() + +plt.figure(2) +plt.plot(df["Batch_Size"].tolist(), df["Error"].tolist(), 'bo') +plt.xlabel("Batch Size") +plt.ylabel("Error") +plt.title("Batch Size vs. Error") +plt.show() + +plt.figure(3) +plt.plot(df["Layers"].tolist(), df["Error"].tolist(), 'bo') +plt.xlabel("Layers") +plt.ylabel("Error") +plt.title("Layers vs. Error") +plt.show() + +plt.figure(4) +plt.plot(df["Activation"].tolist(), df["Error"].tolist(), 'bo') +plt.xlabel("Activation") +plt.ylabel("Error") +plt.title("Activation vs. Error") +plt.show() \ No newline at end of file From a9898cd2b7fc2aa37c0413405400be4b3690d918 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Thu, 15 Dec 2022 21:37:39 -0700 Subject: [PATCH 09/12] Moving visualize_loss.py --- Examples/visualize_networks_comparison.py | 59 ----------------------- 1 file changed, 59 deletions(-) delete mode 100644 Examples/visualize_networks_comparison.py diff --git a/Examples/visualize_networks_comparison.py b/Examples/visualize_networks_comparison.py deleted file mode 100644 index cf337ca3..00000000 --- a/Examples/visualize_networks_comparison.py +++ /dev/null @@ -1,59 +0,0 @@ -''' -Plot visualization of different model hyperparameters against their corresponding errors -''' - -import pandas as pd -import matplotlib.pyplot as plt -import numpy as np - -# Read to dataframe -column_names = ["Model", "Error", "Error_STD", "Time", "Layers", "Batch_Size", "Learning_Rate", "Activation"] -df = pd.read_csv('test_hyperparameters.txt', sep=",", names=column_names) - -# Check for nulls and drop -df = df.dropna() - -# Make graphs -print("Smallest Error: \n") -best_models = df.nsmallest(20, 'Error') -best_batch_size = best_models["Batch_Size"].mode() -best_learning_rate = best_models["Learning_Rate"].mode() -best_activation = best_models["Activation"].mode() -best_layers = best_models["Layers"].mode() -print(best_models["Layers"]) -print(best_models["Layers"].nsmallest()) - -print("Best Models: \n") -print(best_models) -print("Batch Size: " + str(best_batch_size)) -print("Learning Rate: " + str(best_learning_rate)) -print("Activation: " + str(best_activation)) -print("Layers: " + str(best_layers)) - -plt.figure(1) -plt.plot(df["Learning_Rate"].tolist(), df["Error"].tolist(), 'bo') -plt.xlabel("Learning Rate") -plt.ylabel("Error") -plt.title("Learning Rate vs. Error") -plt.show() - -plt.figure(2) -plt.plot(df["Batch_Size"].tolist(), df["Error"].tolist(), 'bo') -plt.xlabel("Batch Size") -plt.ylabel("Error") -plt.title("Batch Size vs. Error") -plt.show() - -plt.figure(3) -plt.plot(df["Layers"].tolist(), df["Error"].tolist(), 'bo') -plt.xlabel("Layers") -plt.ylabel("Error") -plt.title("Layers vs. Error") -plt.show() - -plt.figure(4) -plt.plot(df["Activation"].tolist(), df["Error"].tolist(), 'bo') -plt.xlabel("Activation") -plt.ylabel("Error") -plt.title("Activation vs. Error") -plt.show() \ No newline at end of file From 1293bc286fdec53425042f3eeaf288ab862905c2 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Thu, 15 Dec 2022 21:43:35 -0700 Subject: [PATCH 10/12] Renamed callback, will add in a % callback later --- Examples/train_network.py | 4 ++-- GravNN/Networks/Callbacks.py | 17 ++++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Examples/train_network.py b/Examples/train_network.py index 8b6a9c0b..0c093323 100644 --- a/Examples/train_network.py +++ b/Examples/train_network.py @@ -45,7 +45,7 @@ def run(config): import pandas as pd np.random.seed(config['seed'][0]) from GravNN.Networks.utils import configure_tensorflow - from GravNN.Networks.Callbacks import SimpleCallback + from GravNN.Networks.Callbacks import SimpleCallback, RMSComponentsCallback from GravNN.Networks.Data import get_preprocessed_data, configure_dataset, compute_input_layer_normalization_constants from GravNN.Networks.Model import PINNGravityModel from GravNN.Networks.utils import populate_config_objects, configure_optimizer @@ -67,7 +67,7 @@ def run(config): # Train network callback = SimpleCallback(config['batch_size'][0]) - loss_callback = LossComponentsCallback(config['batch_size'][0]) + loss_callback = RMSComponentsCallback(config['batch_size'][0]) schedule = get_schedule(config) history = model.fit( diff --git a/GravNN/Networks/Callbacks.py b/GravNN/Networks/Callbacks.py index 5af874da..7ed0026a 100644 --- a/GravNN/Networks/Callbacks.py +++ b/GravNN/Networks/Callbacks.py @@ -91,8 +91,8 @@ def on_train_end(self, logs=None): self.time_delta = np.round(self.end_time - self.train_start, 2) -class LossComponentsCallback(tf.keras.callbacks.Callback): - """Loss Components Callback that prints out loss component metrics every 100 epochs""" +class RMSComponentsCallback(tf.keras.callbacks.Callback): + """Loss Components Callback that prints out RMS component metrics every 100 epochs""" def __init__(self, batch_size, print_interval=100): super().__init__() self.batch_size = batch_size @@ -109,30 +109,33 @@ def incremental_average_loss(self, avg, val, N, M): new_avg = [sum(col)/len(col) for col in zip(*val)] full_new_avg = [M * i for i in new_avg] - avg_list = [old_avg, full_new_avg] final_avg = [] for i in range(len(full_new_avg)): final_avg.append(full_new_avg[i] + old_avg[i]) final_final_avg = [(i / (N+M)) for i in final_avg] return final_final_avg + def incremental_average(self, avg, val, N, M): + np.mean(val) + return ((N*avg + M*val)/(M+N)) + def on_train_batch_end(self, batch, logs=None): - self.N_train_samples += self.batch_size self.loss_components = self.incremental_average_loss( self.loss_components, logs['loss_components'], self.N_train_samples, self.batch_size ) + self.N_train_samples += self.batch_size def on_test_batch_end(self, batch, logs=None): - self.N_val_samples += self.batch_size self.val_loss_components = self.incremental_average_loss( self.val_loss_components, logs['loss_components'], - self.N_train_samples, + self.N_val_samples, self.batch_size ) + self.N_val_samples += self.batch_size def on_epoch_begin(self,epoch,logs=None): self.N_train_samples = 0 @@ -146,7 +149,7 @@ def on_epoch_end(self, epoch, logs=None): print("Loss Components: ") print(self.loss_components) print("Validation Loss Components: ") - print(self.val_loss_components) + print(self.val_loss_components) # Overwrite batch logs for epoch logs (to be saved in history obj) logs['loss_components'] = self.loss_components From a82e8d7e60bcc3d50ec7beae141c342f300ab795 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Thu, 15 Dec 2022 21:43:56 -0700 Subject: [PATCH 11/12] Removed PINN_IV --- GravNN/Networks/Configs/Network_Configs.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/GravNN/Networks/Configs/Network_Configs.py b/GravNN/Networks/Configs/Network_Configs.py index 8fa5a8f4..76c66745 100644 --- a/GravNN/Networks/Configs/Network_Configs.py +++ b/GravNN/Networks/Configs/Network_Configs.py @@ -66,15 +66,4 @@ def PINN_III(): "loss_fcn" : ['avg_percent_summed_rms'], } config.update(network_config) - return config - -def PINN_IV(): - config = PINN_III() - network_config = { - "layers" : [3, 100, 100, 100, 3], - "batch_size" : [1024], - "learning_rate" : [0.001*5] - } - config.update(network_config) - return config - + return config \ No newline at end of file From 4a02f185199f20a29b0571e6bd9287e866dfa099 Mon Sep 17 00:00:00 2001 From: KarinaAndersen1 Date: Thu, 15 Dec 2022 21:44:22 -0700 Subject: [PATCH 12/12] Missing a comma --- GravNN/Networks/Configs/Eros_Configs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GravNN/Networks/Configs/Eros_Configs.py b/GravNN/Networks/Configs/Eros_Configs.py index 78e24ae0..3e011af5 100644 --- a/GravNN/Networks/Configs/Eros_Configs.py +++ b/GravNN/Networks/Configs/Eros_Configs.py @@ -25,7 +25,7 @@ def get_default_eros_config(): "a_transformer": [UniformScaler(feature_range=(-1, 1))], "dummy_transformer": [DummyScaler()], "override" : [False], - 'ref_radius' : [Eros().radius] + 'ref_radius' : [Eros().radius], "ref_radius_min" : [Eros().radius_min], }