From 45ffea04aceb385da5c32b69bedb73434ce0d4bf Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 16:58:46 -0500 Subject: [PATCH] Functions to run experiments with parametrize inputs to the neural network models --- .../roman_pots/train_dense_neural_network.py | 74 ++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 56411dc9..96f0ae03 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -46,7 +46,9 @@ def standardize(x): return standardized_tensor, mean, std def train_model(input_tensor, target_tensor, model, num_epochs, learning_rate): - + # Send model to device + model=model.to(device) + # Define the loss function and optimizer criterion = torch.nn.HuberLoss(reduction='mean', delta=1.0) optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) @@ -88,4 +90,74 @@ def train_model(input_tensor, target_tensor, model, num_epochs, learning_rate): return model +def run_experiment(input_files, target_files, hyperparameters): + + # Load input and target training data in tensors + training_RP_pos = pd.DataFrame() + training_MC_mom = pd.DataFrame() + + for i in range(1,num_training_inputs+1): + temp_training_RP_pos = pd.read_csv(hyperparameters.input_files+str(i)+'.txt', delimiter='\t', header=None) + training_RP_pos = pd.concat([training_RP_pos, temp_training_RP_pos], ignore_index=True) + temp_training_MC_mom = pd.read_csv(hyperparameters.target_files+str(i)+'.txt', delimiter='\t', header=None) + training_MC_mom = pd.concat([training_MC_mom, temp_training_MC_mom], ignore_index=True) + + training_RP_pos_tensor = torch.tensor(training_RP_pos.values, dtype=torch.float32) + training_MC_mom_tensor = torch.tensor(training_MC_mom.values, dtype=torch.float32) + + # Standardize training data + source_pz = training_RP_pos_tensor + scaled_source_pz, mean_source_pz, std_source_pz = standardize(source_pz) + target_pz = training_MC_mom_tensor[:,2].unsqueeze(1) + + source_py = torch.cat((training_RP_pos_tensor[:,2:4], training_MC_mom_tensor[:,2].unsqueeze(1)), 1) + scaled_source_py, mean_source_py, std_source_py = standardize(source_py) + target_py = training_MC_mom_tensor[:,1].unsqueeze(1) + + source_px = torch.cat((training_RP_pos_tensor[:,0:2], training_MC_mom_tensor[:,2].unsqueeze(1)), 1) + scaled_source_px, mean_source_px, std_source_px = standardize(source_px) + target_px = training_MC_mom_tensor[:,0].unsqueeze(1) + + # Initialize models + initial_model_pz = NeuralNet(size_input=hyperparameters.size_input_pz, + size_output=hyperparameters.size_output_pz, + n_layers=hyperparameters.n_layers_pz, + size_first_hidden_layer=hyperparameters.size_first_hidden_layer_pz, + multiplier=hyperparameters.muliplier_pz, + leak_rate=hyperparameters.leak_rate_pz) + initial_model_py = NeuralNet(size_input=hyperparameters.size_input_py, + size_output=hyperparameters.size_output_py, + n_layers=hyperparameters.n_layers_py, + size_first_hidden_layer=hyperparameters.size_first_hidden_layer_py, + multiplier=hyperparameters.muliplier_py, + leak_rate=hyperparameters.leak_rate_py) + initial_model_px = NeuralNet(size_input=hyperparameters.size_input_px, + size_output=hyperparameters.size_output_px, + n_layers=hyperparameters.n_layers_px, + size_first_hidden_layer=hyperparameters.size_first_hidden_layer_px, + multiplier=hyperparameters.muliplier_px, + leak_rate=hyperparameters.leak_rate_px) + + # Train models + model_pz = train_model(scaled_source_pz, target_pz, initial_model_pz, num_epochs=hyperparameters.num_epochs_pz, learning_rate=hyperparameters.learning_rate_pz) + model_py = train_model(scaled_source_py, target_py, initial_model_py, num_epochs=hyperparameters.num_epochs_py, learning_rate=hyperparameters.learning_rate_py) + model_px = train_model(scaled_source_px, target_px, initial_model_px, num_epochs=hyperparameters.num_epochs_px, learning_rate=hyperparameters.learning_rate_px) + + # Save models + torch.jit.script(model_pz).save('model_pz.pt') + torch.jit.script(model_py).save('model_py.pt') + torch.jit.script(model_px).save('model_px.pt') + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(fromfile_prefix_chars='@') + hyperparameters_list = ['--input_files', '--target_files', '--num_training_inputs', + '--num_epochs_pz', '--learning_rate_pz', '--size_input_pz', '--size_output_pz', '--n_layers_pz', '--size_first_hidden_layer_pz', '--multiplier_pz', 'leak_rate_pz', + '--num_epochs_py', '--learning_rate_py', '--size_input_py', '--size_output_py', '--n_layers_py', '--size_first_hidden_layer_py', '--multiplier_py', 'leak_rate_py', + '--num_epochs_px', '--learning_rate_px', '--size_input_px', '--size_output_px', '--n_layers_px', '--size_first_hidden_layer_px', '--multiplier_px', 'leak_rate_px'] + for hyperparameter in hyperparameters_list: + parser.add_arguments(hyperparameter) + hyperparameters = parser.parse_args(['@'+str(sys.argv[1])]) + run_experiment(hyperparameters) +