From 4a5c33521a39211743a55a42ff2363cc0d9fdcc7 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Wed, 10 Jan 2024 13:06:46 -0500 Subject: [PATCH 01/31] Add initial script for training neural network for roman pots momentum reconstruction. Function create a parametried dense neural network and train it with standardized input data. --- .../roman_pots/train_dense_neural_network.py | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 benchmarks/roman_pots/train_dense_neural_network.py diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py new file mode 100644 index 00000000..0e0b176e --- /dev/null +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -0,0 +1,92 @@ +import pandas as pd +import numpy as np +import seaborn as sb +import torch +import torch.nn as nn +import torch.optim as optim +import torch.optim.lr_scheduler as lr_scheduler +import matplotlib.pyplot as plt +torch.set_default_dtype(torch.float32) + +if torch.cuda.is_available(): + device = torch.device("cuda") + print("GPU is available!") +else: + device = torch.device("cpu") + print("GPU not found. Using CPU.") + +class NeuralNet(nn.Module): + def __init__(self, size_input, size_output, n_layers, size_firsthiddenlayer=128, multiplier=0.5, leak_rate=0.025): + super().__init__() + self.fc = [] + self.relu = [] + self.n_layers = n_layers + self.fc.append(nn.Linear(size_input,size_firsthiddenlayer)) + self.relu.append(nn.LeakyReLU(leak_rate)) + for i in range(1,n_layers-1): + size_currenthiddenlayer = int(size_firsthiddenlayer*multiplier**i) + self.fc.append(nn.Linear(int(size_currenthiddenlayer/multiplier), size_currenthiddenlayer)) + self.relu.append(nn.LeakyReLU(leak_rate)) + self.fc.append(nn.Linear(size_currenthiddenlayer, size_output)) + self.fc=nn.ParameterList(self.fc) + self.relu=nn.ParameterList(self.relu) + print("Create a network with the linear layers "+str(self.fc)) + print("and leaky relu activation layers "+str(self.relu)) + + def forward(self, x): + for i in range(0,self.n_layers-1): + x = self.fc[i](x) + x = self.relu[i](x) + x = self.fc[self.n_layers-1](x) + return x + +def standardize(tensor): + mean = torch.mean(tensor, axis=0) + std = torch.std(tensor, axis=0) + standardized_tensor = (tensor - mean) / std + return standardized_tensor + +def train_model(input_tensor, target_tensor, model): + + # Define the loss function and optimizer + criterion = torch.nn.HuberLoss(reduction='mean', delta=1.0) + optimizer = torch.optim.Adam(model.parameters(), lr=0.01) + + # Create a learning rate scheduler + scheduler = lr_scheduler.ReduceLROnPlateau(optimizer,'min',patience=100,cooldown=100,factor=0.5,threshold=1e-4,verbose=True) + + # Track the losses + losses = [] + + # Train the model + for epoch in range(num_epochs): + # Forward pass + inputs, targets = input_tensor.to(device), target_tensor.to(device) + predictions = model(inputs) + loss = criterion(predictions, targets) + + # Backward and optimize + optimizer.zero_grad() + loss.backward() + optimizer.step() + + # Track the loss value at each epoch + losses.append(loss.item()) + + # Step the learning rate scheduler + scheduler.step(loss) + + # Print progress + if (epoch + 1) % 10 == 0: + print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.3e}') + + # Plot the loss values + plt.plot(range(1, num_epochs+1), losses) + plt.xlabel('Epoch') + plt.ylabel('Loss') + plt.title('Loss as a Function of Epoch') + plt.savefig('Loss vs Epoch') + + return model + + From 77a117d0ee33a1ebbedb3f7e847a8f08b95cce1d Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Thu, 11 Jan 2024 22:29:07 -0500 Subject: [PATCH 02/31] Simplify layer description code using nn.ModuleList --- .../roman_pots/train_dense_neural_network.py | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 0e0b176e..6f5b178f 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -16,29 +16,28 @@ print("GPU not found. Using CPU.") class NeuralNet(nn.Module): - def __init__(self, size_input, size_output, n_layers, size_firsthiddenlayer=128, multiplier=0.5, leak_rate=0.025): - super().__init__() - self.fc = [] - self.relu = [] - self.n_layers = n_layers - self.fc.append(nn.Linear(size_input,size_firsthiddenlayer)) - self.relu.append(nn.LeakyReLU(leak_rate)) - for i in range(1,n_layers-1): - size_currenthiddenlayer = int(size_firsthiddenlayer*multiplier**i) - self.fc.append(nn.Linear(int(size_currenthiddenlayer/multiplier), size_currenthiddenlayer)) - self.relu.append(nn.LeakyReLU(leak_rate)) - self.fc.append(nn.Linear(size_currenthiddenlayer, size_output)) - self.fc=nn.ParameterList(self.fc) - self.relu=nn.ParameterList(self.relu) - print("Create a network with the linear layers "+str(self.fc)) - print("and leaky relu activation layers "+str(self.relu)) + def __init__(self, size_input, size_output, n_layers, size_first_hidden_layer=128, multiplier=0.5, leak_rate=0.025): + super().__init__() + self.layers = nn.ModuleList() + + size_current_hidden_layer = size_first_hidden_layer + + self.layers.append(nn.Linear(size_input, size_current_hidden_layer)) + for i in range(n_layers - 2): + self.layers.append(nn.LeakyReLU(leak_rate)) + self.layers.append(nn.Linear(size_current_hidden_layer, int(size_current_hidden_layer * multiplier))) + size_current_hidden_layer = int(size_current_hidden_layer * multiplier) + self.layers.append(nn.LeakyReLU(leak_rate)) + self.layers.append(nn.Linear(size_current_hidden_layer, size_output)) + + print("Create a network with the following layers:") + for layer in self.layers: + print(layer) def forward(self, x): - for i in range(0,self.n_layers-1): - x = self.fc[i](x) - x = self.relu[i](x) - x = self.fc[self.n_layers-1](x) - return x + for layer in self.layers: + x = layer(x) + return x def standardize(tensor): mean = torch.mean(tensor, axis=0) From 1a5d06e8702c3fb990a79ad82abf9d9978d900ec Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 11:01:08 -0500 Subject: [PATCH 03/31] Calculate the sample mean and standard deviation to pass on as standardization parameters during inference --- benchmarks/roman_pots/train_dense_neural_network.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 6f5b178f..b74dcbec 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -39,11 +39,11 @@ def forward(self, x): x = layer(x) return x -def standardize(tensor): - mean = torch.mean(tensor, axis=0) - std = torch.std(tensor, axis=0) - standardized_tensor = (tensor - mean) / std - return standardized_tensor +def standardize(x): + mean = torch.mean(x, axis=0) + std = torch.std(x, axis=0) + standardized_tensor = (x - mean) / std + return standardized_tensor, mean, std def train_model(input_tensor, target_tensor, model): From 7398a0bf2928c61db88576143bbf0fe3cd46ed01 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 11:27:23 -0500 Subject: [PATCH 04/31] Use number of epochs and learning rate as training hyper parameters --- benchmarks/roman_pots/train_dense_neural_network.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index b74dcbec..3956b82d 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -45,11 +45,11 @@ def standardize(x): standardized_tensor = (x - mean) / std return standardized_tensor, mean, std -def train_model(input_tensor, target_tensor, model): +def train_model(input_tensor, target_tensor, model, num_epochs, learning_rate): # Define the loss function and optimizer criterion = torch.nn.HuberLoss(reduction='mean', delta=1.0) - optimizer = torch.optim.Adam(model.parameters(), lr=0.01) + optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # Create a learning rate scheduler scheduler = lr_scheduler.ReduceLROnPlateau(optimizer,'min',patience=100,cooldown=100,factor=0.5,threshold=1e-4,verbose=True) From c4e1703967c930975d0089ea4a029352339b7200 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 12:46:42 -0500 Subject: [PATCH 05/31] Update number format and precision in training progress message --- benchmarks/roman_pots/train_dense_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 3956b82d..56411dc9 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -77,7 +77,7 @@ def train_model(input_tensor, target_tensor, model, num_epochs, learning_rate): # Print progress if (epoch + 1) % 10 == 0: - print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.3e}') + print("Epoch "+str(epoch+1)+"/"+str(num_epochs)+", Loss: "+"{0:0.10f}".format(loss.item())) # Plot the loss values plt.plot(range(1, num_epochs+1), losses) From 45ffea04aceb385da5c32b69bedb73434ce0d4bf Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 16:58:46 -0500 Subject: [PATCH 06/31] 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) + From 92bc46e98b7b71e2df988142262cb18a2489e0d9 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 17:55:37 -0500 Subject: [PATCH 07/31] Import argparse and sys python modules --- benchmarks/roman_pots/train_dense_neural_network.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 96f0ae03..89ca9805 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -6,6 +6,8 @@ import torch.optim as optim import torch.optim.lr_scheduler as lr_scheduler import matplotlib.pyplot as plt +import argparse +import sys torch.set_default_dtype(torch.float32) if torch.cuda.is_available(): From 5ad5a79b04469a10288c58d9c0f01d2407a06a6a Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 17:56:59 -0500 Subject: [PATCH 08/31] Fix typo in call to add_argument function --- benchmarks/roman_pots/train_dense_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 89ca9805..c15c6067 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -158,7 +158,7 @@ def run_experiment(input_files, target_files, hyperparameters): '--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) + parser.add_argument(hyperparameter) hyperparameters = parser.parse_args(['@'+str(sys.argv[1])]) run_experiment(hyperparameters) From e1b0786d375778f3479ae94559031ad2c5717572 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 17:59:22 -0500 Subject: [PATCH 09/31] Fix typo. Missing '--' in list of arguments. --- benchmarks/roman_pots/train_dense_neural_network.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index c15c6067..362f69fe 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -154,9 +154,9 @@ def run_experiment(input_files, target_files, hyperparameters): 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'] + '--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_argument(hyperparameter) hyperparameters = parser.parse_args(['@'+str(sys.argv[1])]) From c4310f35cfe31f0cfcbe7642363b95a185344a8a Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 18:01:24 -0500 Subject: [PATCH 10/31] Input file locations will be passed along with list of hyperparameters --- benchmarks/roman_pots/train_dense_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 362f69fe..f481b6a6 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -92,7 +92,7 @@ def train_model(input_tensor, target_tensor, model, num_epochs, learning_rate): return model -def run_experiment(input_files, target_files, hyperparameters): +def run_experiment(hyperparameters): # Load input and target training data in tensors training_RP_pos = pd.DataFrame() From 6b7dae7882347d5ef29e3e856e5403164866f9ad Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 18:03:24 -0500 Subject: [PATCH 11/31] Fetch the number of training inputs from hyperparameter list --- benchmarks/roman_pots/train_dense_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index f481b6a6..140e4f9c 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -98,7 +98,7 @@ def run_experiment(hyperparameters): training_RP_pos = pd.DataFrame() training_MC_mom = pd.DataFrame() - for i in range(1,num_training_inputs+1): + for i in range(1,int(hyperparameters.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) From 34eee4539bbd2935e3c9e57648ecce79ad2ecc80 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 18:07:14 -0500 Subject: [PATCH 12/31] Fix typo in variable name `multiplier` --- benchmarks/roman_pots/train_dense_neural_network.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 140e4f9c..36bc8e76 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -125,19 +125,19 @@ def run_experiment(hyperparameters): 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, + multiplier=hyperparameters.multiplier_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, + multiplier=hyperparameters.multiplier_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, + multiplier=hyperparameters.multiplier_px, leak_rate=hyperparameters.leak_rate_px) # Train models From 2e25da51387bad209a5f7c6f47a250ad69370955 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 12 Jan 2024 18:13:15 -0500 Subject: [PATCH 13/31] Cast hyperparameters to the appropriate numerical datatypes --- .../roman_pots/train_dense_neural_network.py | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 36bc8e76..eba99968 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -121,29 +121,29 @@ def run_experiment(hyperparameters): 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.multiplier_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.multiplier_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.multiplier_px, - leak_rate=hyperparameters.leak_rate_px) + initial_model_pz = NeuralNet(size_input=int(hyperparameters.size_input_pz), + size_output=int(hyperparameters.size_output_pz), + n_layers=int(hyperparameters.n_layers_pz), + size_first_hidden_layer=int(hyperparameters.size_first_hidden_layer_pz), + multiplier=float(hyperparameters.multiplier_pz), + leak_rate=float(hyperparameters.leak_rate_pz)) + initial_model_py = NeuralNet(size_input=int(hyperparameters.size_input_py), + size_output=int(hyperparameters.size_output_py), + n_layers=int(hyperparameters.n_layers_py), + size_first_hidden_layer=int(hyperparameters.size_first_hidden_layer_py), + multiplier=float(hyperparameters.multiplier_py), + leak_rate=float(hyperparameters.leak_rate_py)) + initial_model_px = NeuralNet(size_input=int(hyperparameters.size_input_px), + size_output=int(hyperparameters.size_output_px), + n_layers=int(hyperparameters.n_layers_px), + size_first_hidden_layer=int(hyperparameters.size_first_hidden_layer_px), + multiplier=float(hyperparameters.multiplier_px), + leak_rate=float(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) + model_pz = train_model(scaled_source_pz, target_pz, initial_model_pz, num_epochs=int(hyperparameters.num_epochs_pz), learning_rate=float(hyperparameters.learning_rate_pz)) + model_py = train_model(scaled_source_py, target_py, initial_model_py, num_epochs=int(hyperparameters.num_epochs_py), learning_rate=float(hyperparameters.learning_rate_py)) + model_px = train_model(scaled_source_px, target_px, initial_model_px, num_epochs=int(hyperparameters.num_epochs_px), learning_rate=float(hyperparameters.learning_rate_px)) # Save models torch.jit.script(model_pz).save('model_pz.pt') From 65310c728aa5ad35bf09aa941227f70b1fa00fac Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 19 Jan 2024 03:25:04 -0500 Subject: [PATCH 14/31] First commit of snakefile for roman pots neural network training. Generate model files and assign unique names based on sha512 hash of concatenated hyperparamater values. --- benchmarks/roman_pots/Snakefile | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 benchmarks/roman_pots/Snakefile diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile new file mode 100644 index 00000000..3c2fff45 --- /dev/null +++ b/benchmarks/roman_pots/Snakefile @@ -0,0 +1,75 @@ +from itertools import product +import hashlib + +DETECTOR_PATH = os.environ["DETECTOR_PATH"] +DETECTOR_VERSION = ["23.12.0"] +DETECTOR_CONFIG = ["epic_craterlake"] +NUM_TRAINING_INPUTS = [100,10] +NUM_EPOCHS_PZ = [10, 100, 1000] +LEARNING_RATE_PZ = [10,5] +SIZE_INPUT_PZ = [10,4] +SIZE_OUTPUT_PZ = [10,3] +N_LAYERS_PZ = [10,2] +SIZE_FIRST_HIDDEN_LAYER_PZ = [10] +MULTIPLIER_PZ = [10] +LEAK_RATE_PZ = [0.025] +NUM_EPOCHS_PY = [10, 100, 1000] +LEARNING_RATE_PY = [10] +SIZE_INPUT_PY = [10] +SIZE_OUTPUT_PY = [10] +N_LAYERS_PY = [10] +SIZE_FIRST_HIDDEN_LAYER_PY = [10] +MULTIPLIER_PY = [10] +LEAK_RATE_PY = [0.025] +NUM_EPOCHS_PX = [10, 100, 1000] +LEARNING_RATE_PX = [10] +SIZE_INPUT_PX = [10] +SIZE_OUTPUT_PX = [10] +N_LAYERS_PX = [10] +SIZE_FIRST_HIDDEN_LAYER_PX = [10] +MULTIPLIER_PX = [10] +LEAK_RATE_PX = [0.025] +MODEL_VERSION = [ + hashlib.sha512("_".join(map(str,x)).encode()).hexdigest() + for x in product( + DETECTOR_VERSION, DETECTOR_CONFIG, 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 + ) +] + +rule all: + input: + expand("results/{detector_version}/{detector_config}/detector_benchmarks/roman_pots/ml/metadata/{model_version}.txt", + detector_version=DETECTOR_VERSION, + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION) + + +rule roman_pots_generate_neural_network_configs: + input: + output: + expand("results/{detector_version}/{detector_config}/detector_benchmarks/roman_pots/ml/metadata/{model_version}.txt", + detector_version=DETECTOR_VERSION, + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION) + run: + for detector_version, detector_config, 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 in \ + product(DETECTOR_VERSION, DETECTOR_CONFIG, 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): + output_dir = "results/"+str(detector_version)+"/"+str(detector_config)+"/detector_benchmarks/roman_pots/ml/metadata" + output_file = str(detector_version)+"_"+str(detector_config)+"_"+str(num_training_inputs)+"_"+\ + str(num_epochs_pz)+"_"+str(learning_rate_pz)+"_"+str(size_input_pz)+"_"+str(size_output_pz)+"_"+str(n_layers_pz)+"_"+str(size_first_hidden_layer_pz)+"_"+str(multiplier_pz)+"_"+str(leak_rate_pz)+"_"+\ + str(num_epochs_py)+"_"+str(learning_rate_py)+"_"+str(size_input_py)+"_"+str(size_output_py)+"_"+str(n_layers_py)+"_"+str(size_first_hidden_layer_py)+"_"+str(multiplier_py)+"_"+str(leak_rate_py)+"_"+\ + str(num_epochs_px)+"_"+str(learning_rate_px)+"_"+str(size_input_px)+"_"+str(size_output_px)+"_"+str(n_layers_px)+"_"+str(size_first_hidden_layer_px)+"_"+str(multiplier_px)+"_"+str(leak_rate_px) + output_file_location = open(str(output_dir)+"/"+str(hashlib.sha512(output_file.encode()).hexdigest())+".txt","w") + output_file_location.write(output_file) + print(output_file_location) + output_file_location.close() + From 130ff48ee394bdba2483d0ee8571520c3209a15c Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 19 Jan 2024 12:26:44 -0500 Subject: [PATCH 15/31] Unhash the detector config and detector version. Only hash the parameter string. --- benchmarks/roman_pots/Snakefile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 3c2fff45..5d985771 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -2,8 +2,8 @@ from itertools import product import hashlib DETECTOR_PATH = os.environ["DETECTOR_PATH"] -DETECTOR_VERSION = ["23.12.0"] -DETECTOR_CONFIG = ["epic_craterlake"] +DETECTOR_VERSION = ["23.11.0","23.12.0"] +DETECTOR_CONFIG = ["epic_craterlake", "epic_brycecanyon"] NUM_TRAINING_INPUTS = [100,10] NUM_EPOCHS_PZ = [10, 100, 1000] LEARNING_RATE_PZ = [10,5] @@ -32,7 +32,7 @@ LEAK_RATE_PX = [0.025] MODEL_VERSION = [ hashlib.sha512("_".join(map(str,x)).encode()).hexdigest() for x in product( - DETECTOR_VERSION, DETECTOR_CONFIG, NUM_TRAINING_INPUTS, + 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 @@ -41,7 +41,7 @@ MODEL_VERSION = [ rule all: input: - expand("results/{detector_version}/{detector_config}/detector_benchmarks/roman_pots/ml/metadata/{model_version}.txt", + expand("results/{detector_version}/{detector_config}/detector_benchmarks/roman_pots/ml/metadata/{detector_version}_{detector_config}_{model_version}.txt", detector_version=DETECTOR_VERSION, detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) @@ -50,7 +50,7 @@ rule all: rule roman_pots_generate_neural_network_configs: input: output: - expand("results/{detector_version}/{detector_config}/detector_benchmarks/roman_pots/ml/metadata/{model_version}.txt", + expand("results/{detector_version}/{detector_config}/detector_benchmarks/roman_pots/ml/metadata/{detector_version}_{detector_config}_{model_version}.txt", detector_version=DETECTOR_VERSION, detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) @@ -64,11 +64,11 @@ rule roman_pots_generate_neural_network_configs: 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): output_dir = "results/"+str(detector_version)+"/"+str(detector_config)+"/detector_benchmarks/roman_pots/ml/metadata" - output_file = str(detector_version)+"_"+str(detector_config)+"_"+str(num_training_inputs)+"_"+\ + output_file = str(num_training_inputs)+"_"+\ str(num_epochs_pz)+"_"+str(learning_rate_pz)+"_"+str(size_input_pz)+"_"+str(size_output_pz)+"_"+str(n_layers_pz)+"_"+str(size_first_hidden_layer_pz)+"_"+str(multiplier_pz)+"_"+str(leak_rate_pz)+"_"+\ str(num_epochs_py)+"_"+str(learning_rate_py)+"_"+str(size_input_py)+"_"+str(size_output_py)+"_"+str(n_layers_py)+"_"+str(size_first_hidden_layer_py)+"_"+str(multiplier_py)+"_"+str(leak_rate_py)+"_"+\ str(num_epochs_px)+"_"+str(learning_rate_px)+"_"+str(size_input_px)+"_"+str(size_output_px)+"_"+str(n_layers_px)+"_"+str(size_first_hidden_layer_px)+"_"+str(multiplier_px)+"_"+str(leak_rate_px) - output_file_location = open(str(output_dir)+"/"+str(hashlib.sha512(output_file.encode()).hexdigest())+".txt","w") + output_file_location = open(str(output_dir)+"/"+str(detector_version)+"_"+str(detector_config)+"_"+str(hashlib.sha512(output_file.encode()).hexdigest())+".txt","w") output_file_location.write(output_file) print(output_file_location) output_file_location.close() From 08fa4c5eea21c9a4bd2bb3c57457d7e63576b31e Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Fri, 19 Jan 2024 13:25:45 -0500 Subject: [PATCH 16/31] Parametrise subsystem and model type for readability --- benchmarks/roman_pots/Snakefile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 5d985771..6f755f2a 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -2,6 +2,8 @@ from itertools import product import hashlib DETECTOR_PATH = os.environ["DETECTOR_PATH"] +SUBSYSTEM = "roman_pots" +BENCHMARK = "dense_neural_network" DETECTOR_VERSION = ["23.11.0","23.12.0"] DETECTOR_CONFIG = ["epic_craterlake", "epic_brycecanyon"] NUM_TRAINING_INPUTS = [100,10] @@ -41,7 +43,7 @@ MODEL_VERSION = [ rule all: input: - expand("results/{detector_version}/{detector_config}/detector_benchmarks/roman_pots/ml/metadata/{detector_version}_{detector_config}_{model_version}.txt", + expand("results/{detector_version}/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/{detector_version}_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", detector_version=DETECTOR_VERSION, detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) @@ -50,7 +52,7 @@ rule all: rule roman_pots_generate_neural_network_configs: input: output: - expand("results/{detector_version}/{detector_config}/detector_benchmarks/roman_pots/ml/metadata/{detector_version}_{detector_config}_{model_version}.txt", + expand("results/{detector_version}/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/{detector_version}_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", detector_version=DETECTOR_VERSION, detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) @@ -63,12 +65,12 @@ rule roman_pots_generate_neural_network_configs: 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): - output_dir = "results/"+str(detector_version)+"/"+str(detector_config)+"/detector_benchmarks/roman_pots/ml/metadata" + output_dir = "results/"+str(detector_version)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata" output_file = str(num_training_inputs)+"_"+\ str(num_epochs_pz)+"_"+str(learning_rate_pz)+"_"+str(size_input_pz)+"_"+str(size_output_pz)+"_"+str(n_layers_pz)+"_"+str(size_first_hidden_layer_pz)+"_"+str(multiplier_pz)+"_"+str(leak_rate_pz)+"_"+\ str(num_epochs_py)+"_"+str(learning_rate_py)+"_"+str(size_input_py)+"_"+str(size_output_py)+"_"+str(n_layers_py)+"_"+str(size_first_hidden_layer_py)+"_"+str(multiplier_py)+"_"+str(leak_rate_py)+"_"+\ str(num_epochs_px)+"_"+str(learning_rate_px)+"_"+str(size_input_px)+"_"+str(size_output_px)+"_"+str(n_layers_px)+"_"+str(size_first_hidden_layer_px)+"_"+str(multiplier_px)+"_"+str(leak_rate_px) - output_file_location = open(str(output_dir)+"/"+str(detector_version)+"_"+str(detector_config)+"_"+str(hashlib.sha512(output_file.encode()).hexdigest())+".txt","w") + output_file_location = open(str(output_dir)+"/"+str(detector_version)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(hashlib.sha512(output_file.encode()).hexdigest())+".txt","w") output_file_location.write(output_file) print(output_file_location) output_file_location.close() From 2cbb87e0186c85acb64f566a658a0349182159d1 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Sun, 21 Jan 2024 21:58:50 -0500 Subject: [PATCH 17/31] Truncate the hash to first 6 hexdigits. 6 hexdigits is equivalent to 6*4=24 bits. The number of unique hashes that we can generate before the probability of hash collision between any 2 model hashes reaches 50% is sqrt(2*0.5*2^(24))=4096. We are unlikely to run training over hyperparameter space that large on a single iteration. --- benchmarks/roman_pots/Snakefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 6f755f2a..d6609471 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -31,8 +31,9 @@ N_LAYERS_PX = [10] SIZE_FIRST_HIDDEN_LAYER_PX = [10] MULTIPLIER_PX = [10] LEAK_RATE_PX = [0.025] +MAX_HASH = 6 MODEL_VERSION = [ - hashlib.sha512("_".join(map(str,x)).encode()).hexdigest() + hashlib.sha512("_".join(map(str,x)).encode()).hexdigest()[:MAX_HASH] for x in product( 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, @@ -70,7 +71,7 @@ rule roman_pots_generate_neural_network_configs: str(num_epochs_pz)+"_"+str(learning_rate_pz)+"_"+str(size_input_pz)+"_"+str(size_output_pz)+"_"+str(n_layers_pz)+"_"+str(size_first_hidden_layer_pz)+"_"+str(multiplier_pz)+"_"+str(leak_rate_pz)+"_"+\ str(num_epochs_py)+"_"+str(learning_rate_py)+"_"+str(size_input_py)+"_"+str(size_output_py)+"_"+str(n_layers_py)+"_"+str(size_first_hidden_layer_py)+"_"+str(multiplier_py)+"_"+str(leak_rate_py)+"_"+\ str(num_epochs_px)+"_"+str(learning_rate_px)+"_"+str(size_input_px)+"_"+str(size_output_px)+"_"+str(n_layers_px)+"_"+str(size_first_hidden_layer_px)+"_"+str(multiplier_px)+"_"+str(leak_rate_px) - output_file_location = open(str(output_dir)+"/"+str(detector_version)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(hashlib.sha512(output_file.encode()).hexdigest())+".txt","w") + output_file_location = open(str(output_dir)+"/"+str(detector_version)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(hashlib.sha512(output_file.encode()).hexdigest()[:MAX_HASH])+".txt","w") output_file_location.write(output_file) print(output_file_location) output_file_location.close() From 81bf1d5369c17fd490dd3a35c04c0f73ea395c6e Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Mon, 22 Jan 2024 11:55:58 -0600 Subject: [PATCH 18/31] Introduce steering file to simulate events for model training purposes --- benchmarks/roman_pots/steering_file.py | 164 +++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 benchmarks/roman_pots/steering_file.py diff --git a/benchmarks/roman_pots/steering_file.py b/benchmarks/roman_pots/steering_file.py new file mode 100644 index 00000000..2ec59b01 --- /dev/null +++ b/benchmarks/roman_pots/steering_file.py @@ -0,0 +1,164 @@ +#!/usr/bin/env python +""" +DD4hep simulation with some argument parsing +Based on M. Frank and F. Gaede runSim.py + @author A.Sailer + @version 0.1 +Modified with standard EIC EPIC requirements. +""" +from __future__ import absolute_import, unicode_literals +import logging +import sys +import math + +#from DDSim import SystemOfUnits +from DDSim.DD4hepSimulation import DD4hepSimulation +from g4units import mm, GeV, MeV, radian + +SIM = DD4hepSimulation() +################################################################ +# Steering file to shoot particles into the Roman Pots +# used to generate the training data for RP ML algorithm. +# +# The default magnetic field settings are for 18x275 GeV. +################################################################# + +############################################################# +# Particle Gun simulations use these options +# --> comment out if using MC input +############################################################# + +#SIM.enableDetailedShowerMode = False +SIM.enableG4GPS = False +SIM.enableG4Gun = False +SIM.enableGun = True +#SIM.gun.direction = (math.sin(-0.025*radian), 0, math.cos(-0.025*radian)) +SIM.crossingAngleBoost = -0.025*radian + + +## InputFiles for simulation .stdhep, .slcio, .HEPEvt, .hepevt, .hepmc, .pairs files are supported +SIM.inputFiles = [] +## Macro file to execute for runType 'run' or 'vis' +SIM.macroFile = "" +## number of events to simulate, used in batch mode +##SIM.numberOfEvents = 5000 +## Outputfile from the simulation,only lcio output is supported +##SIM.outputFile = "RP_eDep_and_thresholds_10_6_2023_1k_events.edm4hep.root" +SIM.runType = "run" +## FourVector of translation for the Smearing of the Vertex position: x y z t +SIM.vertexOffset = [0.0, 0.0, 0.0, 0.0] +## FourVector of the Sigma for the Smearing of the Vertex position: x y z t +SIM.vertexSigma = [0.0, 0.0, 0.0, 0.0] + + + +################################################################################ +## Configuration for the magnetic field (stepper) +################################################################################ +SIM.field.delta_chord = 1e-03 +SIM.field.delta_intersection = 1e-03 +SIM.field.delta_one_step = .5e-01*mm +SIM.field.eps_max = 1e-03 +SIM.field.eps_min = 1e-04 +#SIM.field.equation = "Mag_UsualEqRhs" +SIM.field.largest_step = 100.0*mm +SIM.field.min_chord_step = 1.e-2*mm +SIM.field.stepper = "HelixSimpleRunge" + +## choose the distribution of the random direction for theta +## +## Options for random distributions: +## +## 'uniform' is the default distribution, flat in theta +## 'cos(theta)' is flat in cos(theta) +## 'eta', or 'pseudorapidity' is flat in pseudorapity +## 'ffbar' is distributed according to 1+cos^2(theta) +## +## Setting a distribution will set isotrop = True +## +SIM.gun.distribution = 'uniform' +SIM.gun.energy = 275.0*GeV ## default energy value is MeV +#SIM.gun.momentumMin = 274.0*GeV +#SIM.gun.momentumMax = 275.0*GeV + +## isotropic distribution for the particle gun +## +## use the options phiMin, phiMax, thetaMin, and thetaMax to limit the range of randomly distributed directions +## if one of these options is not None the random distribution will be set to True and cannot be turned off! +## +SIM.gun.isotrop = True +#SIM.gun.multiplicity = 1 +SIM.gun.particle = "proton" + +## Minimal azimuthal angle for random distribution +##SIM.gun.phiMin = 0 + +## position of the particle gun, 3 vector. unit mm +##SIM.gun.position = (0, 0, 0) +SIM.gun.thetaMin = 0.000*radian +SIM.gun.thetaMax = 0.005*radian + + +################################################################################ +## Configuration for the output levels of DDG4 components +################################################################################ + +## Output level for input sources +SIM.output.inputStage = 3 + +## Output level for Geant4 kernel +SIM.output.kernel = 3 + +## Output level for ParticleHandler +SIM.output.part = 3 + +## Output level for Random Number Generator setup +SIM.output.random = 6 + + +################################################################################ +## Configuration for the Particle Handler/ MCTruth treatment +################################################################################ + +## Enable lots of printout on simulated hits and MC-truth information +SIM.part.enableDetailedHitsAndParticleInfo = False + +## Keep all created particles +SIM.part.keepAllParticles = True + +## Minimal distance between particle vertex and endpoint of parent after +## which the vertexIsNotEndpointOfParent flag is set +## +SIM.part.minDistToParentVertex = 2.2e-14 + +## MinimalKineticEnergy to store particles created in the tracking region +#SIM.part.minimalKineticEnergy = 1*MeV + +## Printout at End of Tracking +SIM.part.printEndTracking = False + +## Printout at Start of Tracking +SIM.part.printStartTracking = False + +## List of processes to save, on command line give as whitespace separated string in quotation marks +SIM.part.saveProcesses = ['Decay'] + + +################################################################################ +## Configuration for the PhysicsList +################################################################################ +SIM.physics.decays = True +SIM.physics.list = "FTFP_BERT" # "FTFP_BERT" + +################################################################################ +## Properties for the random number generator +################################################################################ + +## If True, calculate random seed for each event based on eventID and runID +## allows reproducibility even when SkippingEvents +SIM.random.enableEventSeed = False +SIM.random.file = None +SIM.random.luxury = 1 +SIM.random.replace_gRandom = True +SIM.random.seed = None +SIM.random.type = None From 1e6607697367f1ded60095348498b025454d93c0 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Tue, 23 Jan 2024 00:14:11 -0500 Subject: [PATCH 19/31] Add rule to generate training events. Use DETECTOR_VERSION inferred from environment for creating directory structure --- benchmarks/roman_pots/Snakefile | 36 ++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index d6609471..29b914ef 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -2,9 +2,9 @@ from itertools import product import hashlib DETECTOR_PATH = os.environ["DETECTOR_PATH"] +DETECTOR_VERSION = os.environ["DETECTOR_VERSION"] SUBSYSTEM = "roman_pots" BENCHMARK = "dense_neural_network" -DETECTOR_VERSION = ["23.11.0","23.12.0"] DETECTOR_CONFIG = ["epic_craterlake", "epic_brycecanyon"] NUM_TRAINING_INPUTS = [100,10] NUM_EPOCHS_PZ = [10, 100, 1000] @@ -41,37 +41,53 @@ MODEL_VERSION = [ 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 ) ] +INPUT_STEERING_FILE = "steering_file.py" +NFILES = range(1,3) +NEVENTS_PER_FILE = 10 rule all: input: - expand("results/{detector_version}/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/{detector_version}_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", - detector_version=DETECTOR_VERSION, + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.edm4hep.root", detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION) + index=NFILES), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION) + + +rule roman_pots_generate_events: + input: + output: + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.edm4hep.root", + detector_config=DETECTOR_CONFIG, + index=NFILES) + run: + for detector_config, index in product(DETECTOR_CONFIG, NFILES): + os.system("npsim --steeringFile "+str(INPUT_STEERING_FILE)+" --compactFile "+str(DETECTOR_PATH)+"/"+str(detector_config)+".xml --outputFile results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(index)+".edm4hep.root -N "+str(NEVENTS_PER_FILE)) + rule roman_pots_generate_neural_network_configs: input: output: - expand("results/{detector_version}/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/{detector_version}_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", - detector_version=DETECTOR_VERSION, + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) run: - for detector_version, detector_config, num_training_inputs, \ + for detector_config, 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 in \ - product(DETECTOR_VERSION, DETECTOR_CONFIG, NUM_TRAINING_INPUTS, + product(DETECTOR_CONFIG, 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): - output_dir = "results/"+str(detector_version)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata" + output_dir = "results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata" output_file = str(num_training_inputs)+"_"+\ str(num_epochs_pz)+"_"+str(learning_rate_pz)+"_"+str(size_input_pz)+"_"+str(size_output_pz)+"_"+str(n_layers_pz)+"_"+str(size_first_hidden_layer_pz)+"_"+str(multiplier_pz)+"_"+str(leak_rate_pz)+"_"+\ str(num_epochs_py)+"_"+str(learning_rate_py)+"_"+str(size_input_py)+"_"+str(size_output_py)+"_"+str(n_layers_py)+"_"+str(size_first_hidden_layer_py)+"_"+str(multiplier_py)+"_"+str(leak_rate_py)+"_"+\ str(num_epochs_px)+"_"+str(learning_rate_px)+"_"+str(size_input_px)+"_"+str(size_output_px)+"_"+str(n_layers_px)+"_"+str(size_first_hidden_layer_px)+"_"+str(multiplier_px)+"_"+str(leak_rate_px) - output_file_location = open(str(output_dir)+"/"+str(detector_version)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(hashlib.sha512(output_file.encode()).hexdigest()[:MAX_HASH])+".txt","w") + output_file_location = open(str(output_dir)+"/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(hashlib.sha512(output_file.encode()).hexdigest()[:MAX_HASH])+".txt","w") output_file_location.write(output_file) print(output_file_location) output_file_location.close() From 570f99d253f86ab3670604d5a14d7b0ed8159c08 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Tue, 23 Jan 2024 09:48:04 -0500 Subject: [PATCH 20/31] Use epic_ip6 geometry for faster processing of far-forward events --- benchmarks/roman_pots/Snakefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 29b914ef..7f86dc0b 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -5,7 +5,7 @@ DETECTOR_PATH = os.environ["DETECTOR_PATH"] DETECTOR_VERSION = os.environ["DETECTOR_VERSION"] SUBSYSTEM = "roman_pots" BENCHMARK = "dense_neural_network" -DETECTOR_CONFIG = ["epic_craterlake", "epic_brycecanyon"] +DETECTOR_CONFIG = ["epic_ip6"] NUM_TRAINING_INPUTS = [100,10] NUM_EPOCHS_PZ = [10, 100, 1000] LEARNING_RATE_PZ = [10,5] @@ -42,8 +42,8 @@ MODEL_VERSION = [ ) ] INPUT_STEERING_FILE = "steering_file.py" -NFILES = range(1,3) -NEVENTS_PER_FILE = 10 +NFILES = range(1,11) +NEVENTS_PER_FILE = 100 rule all: input: From dd7e61b14c3db7cfcfaf83467c5e67bc5e1202d5 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Tue, 23 Jan 2024 13:01:41 -0500 Subject: [PATCH 21/31] Add rule to extract hit information necessary for model training --- benchmarks/roman_pots/Snakefile | 17 ++ .../preprocess_model_training_data.cxx | 148 ++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 benchmarks/roman_pots/preprocess_model_training_data.cxx diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 7f86dc0b..c73e3c4c 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -50,6 +50,9 @@ rule all: expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.edm4hep.root", detector_config=DETECTOR_CONFIG, index=NFILES), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/processed_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.txt", + detector_config=DETECTOR_CONFIG, + index=NFILES), expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) @@ -65,6 +68,20 @@ rule roman_pots_generate_events: for detector_config, index in product(DETECTOR_CONFIG, NFILES): os.system("npsim --steeringFile "+str(INPUT_STEERING_FILE)+" --compactFile "+str(DETECTOR_PATH)+"/"+str(detector_config)+".xml --outputFile results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(index)+".edm4hep.root -N "+str(NEVENTS_PER_FILE)) +rule preprocess_model_training_data: + input: + data = expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.edm4hep.root", + detector_config=DETECTOR_CONFIG, + index=NFILES), + script = "preprocess_model_training_data.cxx" + output: + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/processed_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.txt", + detector_config=DETECTOR_CONFIG, + index=NFILES) + run: + for f_input in input.data: + os.system("root -q -b "+str(input.script)+"\"(\\\""+str(f_input)+"\\\",\\\""+str(f_input.replace(".edm4hep.root",".txt").replace("raw_data","processed_data"))+"\\\")\"") + rule roman_pots_generate_neural_network_configs: diff --git a/benchmarks/roman_pots/preprocess_model_training_data.cxx b/benchmarks/roman_pots/preprocess_model_training_data.cxx new file mode 100644 index 00000000..748c939a --- /dev/null +++ b/benchmarks/roman_pots/preprocess_model_training_data.cxx @@ -0,0 +1,148 @@ +//------------------------- +// +// Hit reader to relate hits at Roman Pots to momentum vectors from MC. +// +// Input(s): output file from npsim particle gun for RP particles. +// +// Output(s): txt file with training information with px_mc, py_mc, pz_mc, x_rp, slope_xrp, y_rp, slope_yrp +// +// +// Author: Alex Jentsch +//------------------------ + + +using namespace std; + +void preprocess_model_training_data(TString inputFile, TString outputFile) { + + string fileName; + TFile* inputRootFile; + TTree* rootTree; + + // MC information + TH1D* h_eta_MC = new TH1D("h_eta", ";Pseudorapidity, #eta", 100, 0.0, 15.0); + TH1D* h_px_MC = new TH1D("px_MC", ";p_{x} [GeV/c]", 100, -10.0, 10.0); + TH1D* h_py_MC = new TH1D("py_MC", ";p_{y} [GeV/c]", 100, -10.0, 10.0); + TH1D* h_pt_MC = new TH1D("pt_MC", ";p_{t} [GeV/c]", 100, 0.0, 2.0); + TH1D* h_pz_MC = new TH1D("pz_MC", ";p_{z} [GeV/c]", 100, 0.0, 320.0); + TH1D* h_theta_MC = new TH1D("theta_MC", ";#theta [mrad]", 100, 0.0, 25.0); + TH1D* h_phi_MC = new TH1D("phi_MC", ";#phi [rad]", 100, -3.2, 3.2); + + // Roman pots + TH1D* h_px_RomanPots = new TH1D("px_RomanPots", ";p_{x} [GeV/c]", 100, -10.0, 10.0); + TH1D* h_py_RomanPots = new TH1D("py_RomanPots", ";p_{y} [GeV/c]", 100, -10.0, 10.0); + TH1D* h_pt_RomanPots = new TH1D("pt_RomanPots", ";p_{t} [GeV/c]", 100, 0.0, 2.0); + TH1D* h_pz_RomanPots = new TH1D("pz_RomanPots", ";p_{z} [GeV/c]", 100, 0.0, 320.0); + + int fileCounter = 0; + int iEvent = 0; + + inputRootFile = new TFile(inputFile); + if (!inputRootFile) { + cout << "MISSING_ROOT_FILE" << fileName << endl; + return; + } + + TTree* evtTree = (TTree*)inputRootFile->Get("events"); + + int numEvents = evtTree->GetEntries(); + + TTreeReader tree_reader(evtTree); // !the tree reader + + ofstream outputTrainingFile; + outputTrainingFile.open(outputFile); + + // MC particles + TTreeReaderArray mc_px_array = { tree_reader, "MCParticles.momentum.x" }; + TTreeReaderArray mc_py_array = { tree_reader, "MCParticles.momentum.y" }; + TTreeReaderArray mc_pz_array = { tree_reader, "MCParticles.momentum.z" }; + TTreeReaderArray mc_mass_array = { tree_reader, "MCParticles.mass" }; + TTreeReaderArray mc_pdg_array = { tree_reader, "MCParticles.PDG" }; + TTreeReaderArray mc_genStatus_array = { tree_reader, "MCParticles.generatorStatus" }; + + // Roman pots -- momentum vector + + // hit locations (for debugging) + TTreeReaderArray global_hit_RP_x = { tree_reader, "ForwardRomanPotHits.position.x" }; + TTreeReaderArray global_hit_RP_y = { tree_reader, "ForwardRomanPotHits.position.y" }; + TTreeReaderArray global_hit_RP_z = { tree_reader, "ForwardRomanPotHits.position.z" }; + + cout << "file has " << evtTree->GetEntries() << " events..." << endl; + + tree_reader.SetEntriesRange(0, evtTree->GetEntries()); + while (tree_reader.Next()) { + + cout << "Reading event: " << iEvent << endl; + + if (mc_px_array.GetSize() > 1) { + cout << "Event has more than one particle -- skip..." << endl; + } + + bool hasMCProton = false; + bool hitLayerOne = false; + bool hitLayerTwo = false; + + double mcProtonMomentum[3]; + TVector3 mctrk; + + for (int imc = 0; imc < mc_px_array.GetSize(); imc++) { + mctrk.SetXYZ(mc_px_array[imc], mc_py_array[imc], mc_pz_array[imc]); + + if (mc_pdg_array[imc] == 2212 && mc_genStatus_array[imc] == 1) { //only checking for protons here -- change as desired + + mcProtonMomentum[0] = mctrk.Px(); + mcProtonMomentum[1] = mctrk.Py(); + mcProtonMomentum[2] = mctrk.Pz(); + hasMCProton = true; + } + } + + double hit1minZ = 25099.0; + double hit1maxZ = 26022.0; + double hit2minZ = 27099.0; + double hit2maxZ = 28022.0; + + double rpHitLayerOne[3]; + double rpHitLayerTwo[3]; + + double slopeXRP = 0.0; + double slopeYRP = 0.0; + + // roman pots reco tracks + for (int iRPPart = 0; iRPPart < global_hit_RP_x.GetSize(); iRPPart++) { + + if (global_hit_RP_z[iRPPart] > hit1minZ && global_hit_RP_z[iRPPart] < hit1maxZ) { + + rpHitLayerOne[0] = global_hit_RP_x[iRPPart]; + rpHitLayerOne[1] = global_hit_RP_y[iRPPart]; + rpHitLayerOne[2] = global_hit_RP_z[iRPPart]; + hitLayerOne = true; + } + + if (global_hit_RP_z[iRPPart] > hit2minZ && global_hit_RP_z[iRPPart] < hit2maxZ) { + + rpHitLayerTwo[0] = global_hit_RP_x[iRPPart]; + rpHitLayerTwo[1] = global_hit_RP_y[iRPPart]; + rpHitLayerTwo[2] = global_hit_RP_z[iRPPart]; + hitLayerTwo = true; + } + } + + if (hasMCProton && hitLayerOne && hitLayerTwo) { + + double slope_x = (rpHitLayerTwo[0] - rpHitLayerOne[0]) / (rpHitLayerTwo[2] - rpHitLayerOne[2]); + double slope_y = (rpHitLayerTwo[1] - rpHitLayerOne[1]) / (rpHitLayerTwo[2] - rpHitLayerOne[2]); + + outputTrainingFile << mcProtonMomentum[0] << "\t" << mcProtonMomentum[1] << "\t" << mcProtonMomentum[2] << "\t"; + outputTrainingFile << rpHitLayerTwo[0] << "\t" << slope_x << "\t" << rpHitLayerTwo[1] << "\t" << slope_y << endl; + } + + iEvent++; + } // event loop + + inputRootFile->Close(); + outputTrainingFile.close(); + + return; +} + From ec5d176e1f8806c19d3e44719b780782e9090fe5 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Tue, 23 Jan 2024 13:49:17 -0500 Subject: [PATCH 22/31] Use realistic hyperparameter values --- benchmarks/roman_pots/Snakefile | 44 ++++++++++++++++----------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index c73e3c4c..1fc9b7f1 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -6,30 +6,30 @@ DETECTOR_VERSION = os.environ["DETECTOR_VERSION"] SUBSYSTEM = "roman_pots" BENCHMARK = "dense_neural_network" DETECTOR_CONFIG = ["epic_ip6"] -NUM_TRAINING_INPUTS = [100,10] -NUM_EPOCHS_PZ = [10, 100, 1000] -LEARNING_RATE_PZ = [10,5] -SIZE_INPUT_PZ = [10,4] -SIZE_OUTPUT_PZ = [10,3] -N_LAYERS_PZ = [10,2] -SIZE_FIRST_HIDDEN_LAYER_PZ = [10] -MULTIPLIER_PZ = [10] +NUM_TRAINING_INPUTS = [10,100] +NUM_EPOCHS_PZ = [1000,10000] +LEARNING_RATE_PZ = [0.01] +SIZE_INPUT_PZ = [4] +SIZE_OUTPUT_PZ = [1] +N_LAYERS_PZ = [3,6] +SIZE_FIRST_HIDDEN_LAYER_PZ = [128] +MULTIPLIER_PZ = [0.5] LEAK_RATE_PZ = [0.025] -NUM_EPOCHS_PY = [10, 100, 1000] -LEARNING_RATE_PY = [10] -SIZE_INPUT_PY = [10] -SIZE_OUTPUT_PY = [10] -N_LAYERS_PY = [10] -SIZE_FIRST_HIDDEN_LAYER_PY = [10] -MULTIPLIER_PY = [10] +NUM_EPOCHS_PY = [1000,10000] +LEARNING_RATE_PY = [0.01] +SIZE_INPUT_PY = [3] +SIZE_OUTPUT_PY = [1] +N_LAYERS_PY = [3,6] +SIZE_FIRST_HIDDEN_LAYER_PY = [128] +MULTIPLIER_PY = [0.5] LEAK_RATE_PY = [0.025] -NUM_EPOCHS_PX = [10, 100, 1000] -LEARNING_RATE_PX = [10] -SIZE_INPUT_PX = [10] -SIZE_OUTPUT_PX = [10] -N_LAYERS_PX = [10] -SIZE_FIRST_HIDDEN_LAYER_PX = [10] -MULTIPLIER_PX = [10] +NUM_EPOCHS_PX = [1000,10000] +LEARNING_RATE_PX = [0.01] +SIZE_INPUT_PX = [3] +SIZE_OUTPUT_PX = [1] +N_LAYERS_PX = [3,7] +SIZE_FIRST_HIDDEN_LAYER_PX = [128] +MULTIPLIER_PX = [0.5] LEAK_RATE_PX = [0.025] MAX_HASH = 6 MODEL_VERSION = [ From d89bae8cc6ddbff71599b9fd78f5cccd824d1d8a Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Wed, 24 Jan 2024 04:13:51 -0500 Subject: [PATCH 23/31] Add rule to train network and save models and artifacts. --- benchmarks/roman_pots/Snakefile | 114 +++++++++++++++--- .../roman_pots/train_dense_neural_network.py | 56 +++++---- 2 files changed, 134 insertions(+), 36 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 1fc9b7f1..afbb2af3 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -6,8 +6,7 @@ DETECTOR_VERSION = os.environ["DETECTOR_VERSION"] SUBSYSTEM = "roman_pots" BENCHMARK = "dense_neural_network" DETECTOR_CONFIG = ["epic_ip6"] -NUM_TRAINING_INPUTS = [10,100] -NUM_EPOCHS_PZ = [1000,10000] +NUM_EPOCHS_PZ = [100] LEARNING_RATE_PZ = [0.01] SIZE_INPUT_PZ = [4] SIZE_OUTPUT_PZ = [1] @@ -15,7 +14,7 @@ N_LAYERS_PZ = [3,6] SIZE_FIRST_HIDDEN_LAYER_PZ = [128] MULTIPLIER_PZ = [0.5] LEAK_RATE_PZ = [0.025] -NUM_EPOCHS_PY = [1000,10000] +NUM_EPOCHS_PY = [100] LEARNING_RATE_PY = [0.01] SIZE_INPUT_PY = [3] SIZE_OUTPUT_PY = [1] @@ -23,7 +22,7 @@ N_LAYERS_PY = [3,6] SIZE_FIRST_HIDDEN_LAYER_PY = [128] MULTIPLIER_PY = [0.5] LEAK_RATE_PY = [0.025] -NUM_EPOCHS_PX = [1000,10000] +NUM_EPOCHS_PX = [100] LEARNING_RATE_PX = [0.01] SIZE_INPUT_PX = [3] SIZE_OUTPUT_PX = [1] @@ -32,18 +31,19 @@ SIZE_FIRST_HIDDEN_LAYER_PX = [128] MULTIPLIER_PX = [0.5] LEAK_RATE_PX = [0.025] MAX_HASH = 6 +NFILES = range(1,11) +NEVENTS_PER_FILE = [100] +NUM_TRAINING_INPUTS = [int(0.5*max(NFILES)),int(0.7*max(NFILES))] MODEL_VERSION = [ hashlib.sha512("_".join(map(str,x)).encode()).hexdigest()[:MAX_HASH] for x in product( - NUM_TRAINING_INPUTS, + NEVENTS_PER_FILE, 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 ) ] INPUT_STEERING_FILE = "steering_file.py" -NFILES = range(1,11) -NEVENTS_PER_FILE = 100 rule all: input: @@ -54,10 +54,29 @@ rule all: detector_config=DETECTOR_CONFIG, index=NFILES), expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_py_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_px_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_py_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_px_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) + rule roman_pots_generate_events: input: output: @@ -82,8 +101,6 @@ rule preprocess_model_training_data: for f_input in input.data: os.system("root -q -b "+str(input.script)+"\"(\\\""+str(f_input)+"\\\",\\\""+str(f_input.replace(".edm4hep.root",".txt").replace("raw_data","processed_data"))+"\\\")\"") - - rule roman_pots_generate_neural_network_configs: input: output: @@ -91,21 +108,88 @@ rule roman_pots_generate_neural_network_configs: detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) run: - for detector_config, num_training_inputs, \ + for detector_config, nevents_per_file, 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 in \ - product(DETECTOR_CONFIG, NUM_TRAINING_INPUTS, + product(DETECTOR_CONFIG, NEVENTS_PER_FILE, 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): output_dir = "results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata" - output_file = str(num_training_inputs)+"_"+\ + output_file = str(nevents_per_file)+"_"+str(num_training_inputs)+"_"+\ str(num_epochs_pz)+"_"+str(learning_rate_pz)+"_"+str(size_input_pz)+"_"+str(size_output_pz)+"_"+str(n_layers_pz)+"_"+str(size_first_hidden_layer_pz)+"_"+str(multiplier_pz)+"_"+str(leak_rate_pz)+"_"+\ str(num_epochs_py)+"_"+str(learning_rate_py)+"_"+str(size_input_py)+"_"+str(size_output_py)+"_"+str(n_layers_py)+"_"+str(size_first_hidden_layer_py)+"_"+str(multiplier_py)+"_"+str(leak_rate_py)+"_"+\ str(num_epochs_px)+"_"+str(learning_rate_px)+"_"+str(size_input_px)+"_"+str(size_output_px)+"_"+str(n_layers_px)+"_"+str(size_first_hidden_layer_px)+"_"+str(multiplier_px)+"_"+str(leak_rate_px) - output_file_location = open(str(output_dir)+"/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(hashlib.sha512(output_file.encode()).hexdigest()[:MAX_HASH])+".txt","w") - output_file_location.write(output_file) + model_hash = hashlib.sha512(output_file.encode()).hexdigest()[:MAX_HASH] + output_file_location = open(str(output_dir)+"/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_hash)+".txt","w") + output_file_content = "--input_files\nresults/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/processed_data/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_\n"+\ + "--model_version\n"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_hash)+"\n"+\ + "--nevents_per_file\n"+str(nevents_per_file)+"\n"+\ + "--num_training_inputs\n"+str(num_training_inputs)+"\n"+\ + "--num_epochs_pz\n"+str(num_epochs_pz)+"\n"+\ + "--learning_rate_pz\n"+str(learning_rate_pz)+"\n"+\ + "--size_input_pz\n"+str(size_input_pz)+"\n"+\ + "--size_output_pz\n"+str(size_output_pz)+"\n"+\ + "--n_layers_pz\n"+str(n_layers_pz)+"\n"+\ + "--size_first_hidden_layer_pz\n"+str(size_first_hidden_layer_pz)+"\n"+\ + "--multiplier_pz\n"+str(multiplier_pz)+"\n"+\ + "--leak_rate_pz\n"+str(leak_rate_pz)+"\n"+\ + "--num_epochs_py\n"+str(num_epochs_py)+"\n"+\ + "--learning_rate_py\n"+str(learning_rate_py)+"\n"+\ + "--size_input_py\n"+str(size_input_py)+"\n"+\ + "--size_output_py\n"+str(size_output_py)+"\n"+\ + "--n_layers_py\n"+str(n_layers_py)+"\n"+\ + "--size_first_hidden_layer_py\n"+str(size_first_hidden_layer_py)+"\n"+\ + "--multiplier_py\n"+str(multiplier_py)+"\n"+\ + "--leak_rate_py\n"+str(leak_rate_py)+"\n"+\ + "--num_epochs_px\n"+str(num_epochs_px)+"\n"+\ + "--learning_rate_px\n"+str(learning_rate_px)+"\n"+\ + "--size_input_px\n"+str(size_input_px)+"\n"+\ + "--size_output_px\n"+str(size_output_px)+"\n"+\ + "--n_layers_px\n"+str(n_layers_px)+"\n"+\ + "--size_first_hidden_layer_px\n"+str(size_first_hidden_layer_px)+"\n"+\ + "--multiplier_px\n"+str(multiplier_px)+"\n"+\ + "--leak_rate_px\n"+str(leak_rate_px) + output_file_location.write(output_file_content) print(output_file_location) output_file_location.close() - + +rule roman_pots_train_neural_networks: + input: + script = "train_dense_neural_network.py" + output: + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_py_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_px_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_py_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION), + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_px_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", + detector_config=DETECTOR_CONFIG, + model_version=MODEL_VERSION) + + run: + for detector_config, model_version in product(DETECTOR_CONFIG,MODEL_VERSION): + os.system("mkdir -p results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models") + os.system("python "+str(input.script)+" results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".txt") + os.system("mv model_pz_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".pt results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/") + os.system("mv model_py_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".pt results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/") + os.system("mv model_px_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".pt results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/") + os.system("mv LossVsEpoch_model_pz_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".png results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/") + os.system("mv LossVsEpoch_model_py_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".png results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/") + os.system("mv LossVsEpoch_model_px_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".png results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/") + + + + + diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index eba99968..5f9dec91 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -47,7 +47,24 @@ def standardize(x): standardized_tensor = (x - mean) / std return standardized_tensor, mean, std -def train_model(input_tensor, target_tensor, model, num_epochs, learning_rate): +def train_model(name, input_tensor, target_tensor, model, hyperparameters): + # Set hyperparameters + match name: + case "model_pz": + num_epochs = int(hyperparameters.num_epochs_pz) + learning_rate = float(hyperparameters.learning_rate_pz) + case "model_py": + num_epochs = int(hyperparameters.num_epochs_py) + learning_rate = float(hyperparameters.learning_rate_py) + case "model_px": + num_epochs = int(hyperparameters.num_epochs_px) + learning_rate = float(hyperparameters.learning_rate_px) + case _: + print("No model name provided. Return without further processing") + return + print("Set number of epochs and learning rate to "+str(num_epochs)+" and "+str(learning_rate)+" for "+str(name)+" training.") + + # Send model to device model=model.to(device) @@ -84,28 +101,28 @@ def train_model(input_tensor, target_tensor, model, num_epochs, learning_rate): print("Epoch "+str(epoch+1)+"/"+str(num_epochs)+", Loss: "+"{0:0.10f}".format(loss.item())) # Plot the loss values + plt.figure() plt.plot(range(1, num_epochs+1), losses) plt.xlabel('Epoch') plt.ylabel('Loss') plt.title('Loss as a Function of Epoch') - plt.savefig('Loss vs Epoch') + plt.yscale('log') + plt.savefig("LossVsEpoch_"+name+"_"+str(hyperparameters.model_version)+".png") - return model + torch.jit.script(model).save(name+"_"+str(hyperparameters.model_version)+".pt") + return def run_experiment(hyperparameters): - # Load input and target training data in tensors - training_RP_pos = pd.DataFrame() - training_MC_mom = pd.DataFrame() + # Load training data in tensors + training_data = pd.DataFrame() for i in range(1,int(hyperparameters.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) + temp_training_data = pd.read_csv(hyperparameters.input_files+str(i)+'.txt', delimiter='\t', header=None) + training_data = pd.concat([training_data, temp_training_data], 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) + training_RP_pos_tensor = torch.tensor(training_data.iloc[:,3:7].values, dtype=torch.float32) + training_MC_mom_tensor = torch.tensor(training_data.iloc[:,0:3].values, dtype=torch.float32) # Standardize training data source_pz = training_RP_pos_tensor @@ -141,19 +158,16 @@ def run_experiment(hyperparameters): leak_rate=float(hyperparameters.leak_rate_px)) # Train models - model_pz = train_model(scaled_source_pz, target_pz, initial_model_pz, num_epochs=int(hyperparameters.num_epochs_pz), learning_rate=float(hyperparameters.learning_rate_pz)) - model_py = train_model(scaled_source_py, target_py, initial_model_py, num_epochs=int(hyperparameters.num_epochs_py), learning_rate=float(hyperparameters.learning_rate_py)) - model_px = train_model(scaled_source_px, target_px, initial_model_px, num_epochs=int(hyperparameters.num_epochs_px), learning_rate=float(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') + train_model("model_pz", scaled_source_pz, target_pz, initial_model_pz, hyperparameters) + train_model("model_py", scaled_source_py, target_py, initial_model_py, hyperparameters) + train_model("model_px", scaled_source_px, target_px, initial_model_px, hyperparameters) + # Print end statement + print("Training completed using "+str(int(hyperparameters.nevents_per_file)*int(hyperparameters.num_training_inputs))+" generated events.") if __name__ == "__main__": parser = argparse.ArgumentParser(fromfile_prefix_chars='@') - hyperparameters_list = ['--input_files', '--target_files', '--num_training_inputs', + hyperparameters_list = ['--input_files', '--model_version', '--nevents_per_file', '--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'] From f34159a1a52c0e438c23b0f0c83aa095857aa953 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Wed, 24 Jan 2024 04:42:03 -0500 Subject: [PATCH 24/31] Unnecessary because directory structure is automatically created by rule --- benchmarks/roman_pots/Snakefile | 1 - 1 file changed, 1 deletion(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index afbb2af3..6c3c462e 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -180,7 +180,6 @@ rule roman_pots_train_neural_networks: run: for detector_config, model_version in product(DETECTOR_CONFIG,MODEL_VERSION): - os.system("mkdir -p results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models") os.system("python "+str(input.script)+" results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".txt") os.system("mv model_pz_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".pt results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/") os.system("mv model_py_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".pt results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/") From d81ab52cef693b7a61afcd80ec111142307f10f6 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Thu, 25 Jan 2024 15:25:07 -0500 Subject: [PATCH 25/31] Variable is an array with one element. So, indexing needs to be used --- benchmarks/roman_pots/Snakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 6c3c462e..d71c8ed5 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -85,7 +85,7 @@ rule roman_pots_generate_events: index=NFILES) run: for detector_config, index in product(DETECTOR_CONFIG, NFILES): - os.system("npsim --steeringFile "+str(INPUT_STEERING_FILE)+" --compactFile "+str(DETECTOR_PATH)+"/"+str(detector_config)+".xml --outputFile results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(index)+".edm4hep.root -N "+str(NEVENTS_PER_FILE)) + os.system("npsim --steeringFile "+str(INPUT_STEERING_FILE)+" --compactFile "+str(DETECTOR_PATH)+"/"+str(detector_config)+".xml --outputFile results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(index)+".edm4hep.root -N "+str(NEVENTS_PER_FILE[0])) rule preprocess_model_training_data: input: From 805c2cde0d8784e81b4c1413feb4fc8c7eba302a Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Thu, 25 Jan 2024 18:14:32 -0500 Subject: [PATCH 26/31] Split up default target rule into 3 rules so that proper dependency is obeyed. Execute snakemake all1/2/3 in sequence to avoid errors --- benchmarks/roman_pots/Snakefile | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index d71c8ed5..a0a136ef 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -45,17 +45,23 @@ MODEL_VERSION = [ ] INPUT_STEERING_FILE = "steering_file.py" -rule all: +rule all1: input: expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.edm4hep.root", detector_config=DETECTOR_CONFIG, index=NFILES), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/processed_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.txt", - detector_config=DETECTOR_CONFIG, - index=NFILES), expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), + model_version=MODEL_VERSION) + +rule all2: + input: + expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/processed_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.txt", + detector_config=DETECTOR_CONFIG, + index=NFILES) + +rule all3: + input: expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION), @@ -75,8 +81,6 @@ rule all: detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) - - rule roman_pots_generate_events: input: output: From 2d1a65e6763633ba36cc09e357d29078d45a1048 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Tue, 30 Jan 2024 03:58:03 -0500 Subject: [PATCH 27/31] Simplify workflow. Build dependency between generate and process events rules. Parallelization at generation stage through use of wildcards. --- benchmarks/roman_pots/Snakefile | 57 ++++++++++++++++----------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index a0a136ef..3827c080 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -43,24 +43,20 @@ MODEL_VERSION = [ 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 ) ] -INPUT_STEERING_FILE = "steering_file.py" -rule all1: +rule target_generate: input: - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.edm4hep.root", + expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.edm4hep.root", + detector_config=DETECTOR_CONFIG, + index=NFILES), + expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.txt", detector_config=DETECTOR_CONFIG, index=NFILES), expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", detector_config=DETECTOR_CONFIG, model_version=MODEL_VERSION) -rule all2: - input: - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/processed_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.txt", - detector_config=DETECTOR_CONFIG, - index=NFILES) - -rule all3: +rule target_train: input: expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", detector_config=DETECTOR_CONFIG, @@ -82,28 +78,31 @@ rule all3: model_version=MODEL_VERSION) rule roman_pots_generate_events: + input: + script="steering_file.py" + params: + detector_path=DETECTOR_PATH, + nevents_per_file=NEVENTS_PER_FILE + output: + "results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.edm4hep.root" + shell: + """ + npsim --steeringFile {input.script} \ + --compactFile {params.detector_path}/{wildcards.detector_config}.xml \ + --outputFile {output} \ + -N {params.nevents_per_file} + """ + +rule roman_pots_preprocess_model_training_data: input: - output: - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.edm4hep.root", - detector_config=DETECTOR_CONFIG, - index=NFILES) - run: - for detector_config, index in product(DETECTOR_CONFIG, NFILES): - os.system("npsim --steeringFile "+str(INPUT_STEERING_FILE)+" --compactFile "+str(DETECTOR_PATH)+"/"+str(detector_config)+".xml --outputFile results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(index)+".edm4hep.root -N "+str(NEVENTS_PER_FILE[0])) - -rule preprocess_model_training_data: - input: - data = expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/raw_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.edm4hep.root", - detector_config=DETECTOR_CONFIG, - index=NFILES), + data = "results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.edm4hep.root", script = "preprocess_model_training_data.cxx" output: - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/processed_data/"+str(DETECTOR_VERSION)+"_{detector_config}_{index}.txt", - detector_config=DETECTOR_CONFIG, - index=NFILES) - run: - for f_input in input.data: - os.system("root -q -b "+str(input.script)+"\"(\\\""+str(f_input)+"\\\",\\\""+str(f_input.replace(".edm4hep.root",".txt").replace("raw_data","processed_data"))+"\\\")\"") + "results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.txt" + shell: + """ + root -q -b {input.script}\"(\\\"{input.data}\\\",\\\"{output}\\\")\" + """ rule roman_pots_generate_neural_network_configs: input: From acbb6c562ea4fe870c733a2557cc16f7e58c6765 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Tue, 30 Jan 2024 16:29:08 -0500 Subject: [PATCH 28/31] Use a nested directory approach to uniquely identify models instead of hash following https://waterdata.usgs.gov/blog/snakemake-for-ml-experiments/. Establishes a DAG with greater parallelization of processes. --- benchmarks/roman_pots/Snakefile | 312 +++++++++--------- .../roman_pots/train_dense_neural_network.py | 120 +++---- 2 files changed, 207 insertions(+), 225 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 3827c080..d0d66e4f 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -1,197 +1,195 @@ from itertools import product -import hashlib DETECTOR_PATH = os.environ["DETECTOR_PATH"] DETECTOR_VERSION = os.environ["DETECTOR_VERSION"] SUBSYSTEM = "roman_pots" BENCHMARK = "dense_neural_network" -DETECTOR_CONFIG = ["epic_ip6"] -NUM_EPOCHS_PZ = [100] -LEARNING_RATE_PZ = [0.01] -SIZE_INPUT_PZ = [4] -SIZE_OUTPUT_PZ = [1] -N_LAYERS_PZ = [3,6] -SIZE_FIRST_HIDDEN_LAYER_PZ = [128] -MULTIPLIER_PZ = [0.5] -LEAK_RATE_PZ = [0.025] -NUM_EPOCHS_PY = [100] -LEARNING_RATE_PY = [0.01] -SIZE_INPUT_PY = [3] -SIZE_OUTPUT_PY = [1] -N_LAYERS_PY = [3,6] -SIZE_FIRST_HIDDEN_LAYER_PY = [128] -MULTIPLIER_PY = [0.5] -LEAK_RATE_PY = [0.025] -NUM_EPOCHS_PX = [100] -LEARNING_RATE_PX = [0.01] -SIZE_INPUT_PX = [3] -SIZE_OUTPUT_PX = [1] -N_LAYERS_PX = [3,7] -SIZE_FIRST_HIDDEN_LAYER_PX = [128] -MULTIPLIER_PX = [0.5] -LEAK_RATE_PX = [0.025] -MAX_HASH = 6 -NFILES = range(1,11) -NEVENTS_PER_FILE = [100] -NUM_TRAINING_INPUTS = [int(0.5*max(NFILES)),int(0.7*max(NFILES))] -MODEL_VERSION = [ - hashlib.sha512("_".join(map(str,x)).encode()).hexdigest()[:MAX_HASH] - for x in product( - NEVENTS_PER_FILE, 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 - ) -] +DETECTOR_CONFIG = "epic_ip6" +NEVENTS_PER_FILE = 5 +NFILES = range(1,6) +MODEL_PZ = { + 'num_epochs' : [100], + 'learning_rate' : [0.01], + 'size_input' : [4], + 'size_output': [1], + 'n_layers' : [3,6], + 'size_first_hidden_layer' : [128], + 'multiplier' : [0.5], + 'leak_rate' : [0.025], +} +MODEL_PY = { + 'num_epochs' : [100], + 'learning_rate' : [0.01], + 'size_input' : [3], + 'size_output': [1], + 'n_layers' : [3,6], + 'size_first_hidden_layer' : [128], + 'multiplier' : [0.5], + 'leak_rate' : [0.025] +} +MODEL_PX = { + 'num_epochs' : [100], + 'learning_rate' : [0.01], + 'size_input' : [3], + 'size_output': [1], + 'n_layers' : [3,7], + 'size_first_hidden_layer' : [128], + 'multiplier' : [0.5], + 'leak_rate' : [0.025] +} -rule target_generate: +rule all: input: - expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.edm4hep.root", - detector_config=DETECTOR_CONFIG, + expand("results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.edm4hep.root", index=NFILES), - expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.txt", - detector_config=DETECTOR_CONFIG, + expand("results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.txt", index=NFILES), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION) + expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_pz/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/model_pz.pt", + detector_config=DETECTOR_CONFIG, + num_epochs=MODEL_PZ["num_epochs"], + learning_rate=MODEL_PZ["learning_rate"], + size_input=MODEL_PZ["size_input"], + size_output=MODEL_PZ["size_output"], + n_layers=MODEL_PZ["n_layers"], + size_first_hidden_layer=MODEL_PZ["size_first_hidden_layer"], + multiplier=MODEL_PZ["multiplier"], + leak_rate=MODEL_PZ["leak_rate"] + ), + expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_pz/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/LossVsEpoch_model_pz.png", + detector_config=DETECTOR_CONFIG, + num_epochs=MODEL_PZ["num_epochs"], + learning_rate=MODEL_PZ["learning_rate"], + size_input=MODEL_PZ["size_input"], + size_output=MODEL_PZ["size_output"], + n_layers=MODEL_PZ["n_layers"], + size_first_hidden_layer=MODEL_PZ["size_first_hidden_layer"], + multiplier=MODEL_PZ["multiplier"], + leak_rate=MODEL_PZ["leak_rate"] + ), + expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_py/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/model_py.pt", + detector_config=DETECTOR_CONFIG, + num_epochs=MODEL_PY["num_epochs"], + learning_rate=MODEL_PY["learning_rate"], + size_input=MODEL_PY["size_input"], + size_output=MODEL_PY["size_output"], + n_layers=MODEL_PY["n_layers"], + size_first_hidden_layer=MODEL_PY["size_first_hidden_layer"], + multiplier=MODEL_PY["multiplier"], + leak_rate=MODEL_PY["leak_rate"] + ), + expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_py/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/LossVsEpoch_model_py.png", + detector_config=DETECTOR_CONFIG, + num_epochs=MODEL_PY["num_epochs"], + learning_rate=MODEL_PY["learning_rate"], + size_input=MODEL_PY["size_input"], + size_output=MODEL_PY["size_output"], + n_layers=MODEL_PY["n_layers"], + size_first_hidden_layer=MODEL_PY["size_first_hidden_layer"], + multiplier=MODEL_PY["multiplier"], + leak_rate=MODEL_PY["leak_rate"] + ), + expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_px/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/model_px.pt", + detector_config=DETECTOR_CONFIG, + num_epochs=MODEL_PX["num_epochs"], + learning_rate=MODEL_PX["learning_rate"], + size_input=MODEL_PX["size_input"], + size_output=MODEL_PX["size_output"], + n_layers=MODEL_PX["n_layers"], + size_first_hidden_layer=MODEL_PX["size_first_hidden_layer"], + multiplier=MODEL_PX["multiplier"], + leak_rate=MODEL_PX["leak_rate"] + ), + expand("results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_px/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/LossVsEpoch_model_px.png", + detector_config=DETECTOR_CONFIG, + num_epochs=MODEL_PX["num_epochs"], + learning_rate=MODEL_PX["learning_rate"], + size_input=MODEL_PX["size_input"], + size_output=MODEL_PX["size_output"], + n_layers=MODEL_PX["n_layers"], + size_first_hidden_layer=MODEL_PX["size_first_hidden_layer"], + multiplier=MODEL_PX["multiplier"], + leak_rate=MODEL_PX["leak_rate"] + ) + + -rule target_train: - input: - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_py_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_px_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_py_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_px_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION) rule roman_pots_generate_events: input: script="steering_file.py" params: detector_path=DETECTOR_PATH, - nevents_per_file=NEVENTS_PER_FILE + nevents_per_file=NEVENTS_PER_FILE, + detector_config=DETECTOR_CONFIG output: - "results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.edm4hep.root" + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.edm4hep.root" shell: """ npsim --steeringFile {input.script} \ - --compactFile {params.detector_path}/{wildcards.detector_config}.xml \ + --compactFile {params.detector_path}/{params.detector_config}.xml \ --outputFile {output} \ -N {params.nevents_per_file} """ rule roman_pots_preprocess_model_training_data: input: - data = "results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.edm4hep.root", + data = "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.edm4hep.root", script = "preprocess_model_training_data.cxx" output: - "results/"+DETECTOR_VERSION+"/{detector_config}/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_{detector_config}_{index}.txt" + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.txt" shell: """ root -q -b {input.script}\"(\\\"{input.data}\\\",\\\"{output}\\\")\" """ -rule roman_pots_generate_neural_network_configs: - input: - output: - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.txt", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION) - run: - for detector_config, nevents_per_file, 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 in \ - product(DETECTOR_CONFIG, NEVENTS_PER_FILE, 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): - output_dir = "results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata" - output_file = str(nevents_per_file)+"_"+str(num_training_inputs)+"_"+\ - str(num_epochs_pz)+"_"+str(learning_rate_pz)+"_"+str(size_input_pz)+"_"+str(size_output_pz)+"_"+str(n_layers_pz)+"_"+str(size_first_hidden_layer_pz)+"_"+str(multiplier_pz)+"_"+str(leak_rate_pz)+"_"+\ - str(num_epochs_py)+"_"+str(learning_rate_py)+"_"+str(size_input_py)+"_"+str(size_output_py)+"_"+str(n_layers_py)+"_"+str(size_first_hidden_layer_py)+"_"+str(multiplier_py)+"_"+str(leak_rate_py)+"_"+\ - str(num_epochs_px)+"_"+str(learning_rate_px)+"_"+str(size_input_px)+"_"+str(size_output_px)+"_"+str(n_layers_px)+"_"+str(size_first_hidden_layer_px)+"_"+str(multiplier_px)+"_"+str(leak_rate_px) - model_hash = hashlib.sha512(output_file.encode()).hexdigest()[:MAX_HASH] - output_file_location = open(str(output_dir)+"/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_hash)+".txt","w") - output_file_content = "--input_files\nresults/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/processed_data/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_\n"+\ - "--model_version\n"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_hash)+"\n"+\ - "--nevents_per_file\n"+str(nevents_per_file)+"\n"+\ - "--num_training_inputs\n"+str(num_training_inputs)+"\n"+\ - "--num_epochs_pz\n"+str(num_epochs_pz)+"\n"+\ - "--learning_rate_pz\n"+str(learning_rate_pz)+"\n"+\ - "--size_input_pz\n"+str(size_input_pz)+"\n"+\ - "--size_output_pz\n"+str(size_output_pz)+"\n"+\ - "--n_layers_pz\n"+str(n_layers_pz)+"\n"+\ - "--size_first_hidden_layer_pz\n"+str(size_first_hidden_layer_pz)+"\n"+\ - "--multiplier_pz\n"+str(multiplier_pz)+"\n"+\ - "--leak_rate_pz\n"+str(leak_rate_pz)+"\n"+\ - "--num_epochs_py\n"+str(num_epochs_py)+"\n"+\ - "--learning_rate_py\n"+str(learning_rate_py)+"\n"+\ - "--size_input_py\n"+str(size_input_py)+"\n"+\ - "--size_output_py\n"+str(size_output_py)+"\n"+\ - "--n_layers_py\n"+str(n_layers_py)+"\n"+\ - "--size_first_hidden_layer_py\n"+str(size_first_hidden_layer_py)+"\n"+\ - "--multiplier_py\n"+str(multiplier_py)+"\n"+\ - "--leak_rate_py\n"+str(leak_rate_py)+"\n"+\ - "--num_epochs_px\n"+str(num_epochs_px)+"\n"+\ - "--learning_rate_px\n"+str(learning_rate_px)+"\n"+\ - "--size_input_px\n"+str(size_input_px)+"\n"+\ - "--size_output_px\n"+str(size_output_px)+"\n"+\ - "--n_layers_px\n"+str(n_layers_px)+"\n"+\ - "--size_first_hidden_layer_px\n"+str(size_first_hidden_layer_px)+"\n"+\ - "--multiplier_px\n"+str(multiplier_px)+"\n"+\ - "--leak_rate_px\n"+str(leak_rate_px) - output_file_location.write(output_file_content) - print(output_file_location) - output_file_location.close() - -rule roman_pots_train_neural_networks: +rule roman_pots_train_model_pz: input: + data = ["results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.txt".format(index=index) for index in NFILES], script = "train_dense_neural_network.py" + params: + detector_version=DETECTOR_VERSION, + detector_config=DETECTOR_CONFIG, + subsystem=SUBSYSTEM, + benchmark=BENCHMARK output: - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_py_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/model_px_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.pt", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_pz_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_py_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION), - expand("results/"+str(DETECTOR_VERSION)+"/{detector_config}/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/LossVsEpoch_model_px_"+str(DETECTOR_VERSION)+"_{detector_config}_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_{model_version}.png", - detector_config=DETECTOR_CONFIG, - model_version=MODEL_VERSION) - - run: - for detector_config, model_version in product(DETECTOR_CONFIG,MODEL_VERSION): - os.system("python "+str(input.script)+" results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/metadata/"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".txt") - os.system("mv model_pz_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".pt results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/") - os.system("mv model_py_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".pt results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/") - os.system("mv model_px_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".pt results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/trained_models/") - os.system("mv LossVsEpoch_model_pz_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".png results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/") - os.system("mv LossVsEpoch_model_py_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".png results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/") - os.system("mv LossVsEpoch_model_px_"+str(DETECTOR_VERSION)+"_"+str(detector_config)+"_"+str(SUBSYSTEM)+"_"+str(BENCHMARK)+"_"+str(model_version)+".png results/"+str(DETECTOR_VERSION)+"/"+str(detector_config)+"/detector_benchmarks/"+str(SUBSYSTEM)+"/"+str(BENCHMARK)+"/artifacts/") - - + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_pz/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/model_pz.pt", + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_pz/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/LossVsEpoch_model_pz.png" + shell: + """ + python {input.script} --input_files {input.data} --model_name model_pz --model_dir results/{params.detector_version}/{params.detector_config}/detector_benchmarks/{params.subsystem}/{params.benchmark}/artifacts/model_pz/num_epochs_{wildcards.num_epochs}/learning_rate_{wildcards.learning_rate}/size_input_{wildcards.size_input}/size_output_{wildcards.size_output}/n_layers_{wildcards.n_layers}/size_first_hidden_layer_{wildcards.size_first_hidden_layer}/multiplier_{wildcards.multiplier}/leak_rate_{wildcards.leak_rate} --num_epochs {wildcards.num_epochs} --learning_rate {wildcards.learning_rate} --size_input {wildcards.size_input} --size_output {wildcards.size_output} --n_layers {wildcards.n_layers} --size_first_hidden_layer {wildcards.size_first_hidden_layer} --multiplier {wildcards.multiplier} --leak_rate {wildcards.leak_rate} + """ +rule roman_pots_train_model_py: + input: + data = ["results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.txt".format(index=index) for index in NFILES], + script = "train_dense_neural_network.py" + params: + detector_version=DETECTOR_VERSION, + detector_config=DETECTOR_CONFIG, + subsystem=SUBSYSTEM, + benchmark=BENCHMARK + output: + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_py/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/model_py.pt", + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_py/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/LossVsEpoch_model_py.png" + shell: + """ + python {input.script} --input_files {input.data} --model_name model_py --model_dir results/{params.detector_version}/{params.detector_config}/detector_benchmarks/{params.subsystem}/{params.benchmark}/artifacts/model_py/num_epochs_{wildcards.num_epochs}/learning_rate_{wildcards.learning_rate}/size_input_{wildcards.size_input}/size_output_{wildcards.size_output}/n_layers_{wildcards.n_layers}/size_first_hidden_layer_{wildcards.size_first_hidden_layer}/multiplier_{wildcards.multiplier}/leak_rate_{wildcards.leak_rate} --num_epochs {wildcards.num_epochs} --learning_rate {wildcards.learning_rate} --size_input {wildcards.size_input} --size_output {wildcards.size_output} --n_layers {wildcards.n_layers} --size_first_hidden_layer {wildcards.size_first_hidden_layer} --multiplier {wildcards.multiplier} --leak_rate {wildcards.leak_rate} + """ +rule roman_pots_train_model_px: + input: + data = ["results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.txt".format(index=index) for index in NFILES], + script = "train_dense_neural_network.py" + params: + detector_version=DETECTOR_VERSION, + detector_config=DETECTOR_CONFIG, + subsystem=SUBSYSTEM, + benchmark=BENCHMARK + output: + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_px/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/model_px.pt", + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_px/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/LossVsEpoch_model_px.png" + shell: + """ + python {input.script} --input_files {input.data} --model_name model_px --model_dir results/{params.detector_version}/{params.detector_config}/detector_benchmarks/{params.subsystem}/{params.benchmark}/artifacts/model_px/num_epochs_{wildcards.num_epochs}/learning_rate_{wildcards.learning_rate}/size_input_{wildcards.size_input}/size_output_{wildcards.size_output}/n_layers_{wildcards.n_layers}/size_first_hidden_layer_{wildcards.size_first_hidden_layer}/multiplier_{wildcards.multiplier}/leak_rate_{wildcards.leak_rate} --num_epochs {wildcards.num_epochs} --learning_rate {wildcards.learning_rate} --size_input {wildcards.size_input} --size_output {wildcards.size_output} --n_layers {wildcards.n_layers} --size_first_hidden_layer {wildcards.size_first_hidden_layer} --multiplier {wildcards.multiplier} --leak_rate {wildcards.leak_rate} + """ diff --git a/benchmarks/roman_pots/train_dense_neural_network.py b/benchmarks/roman_pots/train_dense_neural_network.py index 5f9dec91..36bd9bd6 100644 --- a/benchmarks/roman_pots/train_dense_neural_network.py +++ b/benchmarks/roman_pots/train_dense_neural_network.py @@ -8,6 +8,8 @@ import matplotlib.pyplot as plt import argparse import sys +import hashlib + torch.set_default_dtype(torch.float32) if torch.cuda.is_available(): @@ -47,30 +49,13 @@ def standardize(x): standardized_tensor = (x - mean) / std return standardized_tensor, mean, std -def train_model(name, input_tensor, target_tensor, model, hyperparameters): - # Set hyperparameters - match name: - case "model_pz": - num_epochs = int(hyperparameters.num_epochs_pz) - learning_rate = float(hyperparameters.learning_rate_pz) - case "model_py": - num_epochs = int(hyperparameters.num_epochs_py) - learning_rate = float(hyperparameters.learning_rate_py) - case "model_px": - num_epochs = int(hyperparameters.num_epochs_px) - learning_rate = float(hyperparameters.learning_rate_px) - case _: - print("No model name provided. Return without further processing") - return - print("Set number of epochs and learning rate to "+str(num_epochs)+" and "+str(learning_rate)+" for "+str(name)+" training.") - - +def train_model(input_tensor, target_tensor, model, hyperparameters): # 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) + optimizer = torch.optim.Adam(model.parameters(), lr=hyperparameters.learning_rate) # Create a learning rate scheduler scheduler = lr_scheduler.ReduceLROnPlateau(optimizer,'min',patience=100,cooldown=100,factor=0.5,threshold=1e-4,verbose=True) @@ -79,7 +64,7 @@ def train_model(name, input_tensor, target_tensor, model, hyperparameters): losses = [] # Train the model - for epoch in range(num_epochs): + for epoch in range(hyperparameters.num_epochs): # Forward pass inputs, targets = input_tensor.to(device), target_tensor.to(device) predictions = model(inputs) @@ -98,18 +83,18 @@ def train_model(name, input_tensor, target_tensor, model, hyperparameters): # Print progress if (epoch + 1) % 10 == 0: - print("Epoch "+str(epoch+1)+"/"+str(num_epochs)+", Loss: "+"{0:0.10f}".format(loss.item())) + print("Epoch "+str(epoch+1)+"/"+str(hyperparameters.num_epochs)+", Loss: "+"{0:0.10f}".format(loss.item())) # Plot the loss values plt.figure() - plt.plot(range(1, num_epochs+1), losses) + plt.plot(range(1, hyperparameters.num_epochs+1), losses) plt.xlabel('Epoch') plt.ylabel('Loss') plt.title('Loss as a Function of Epoch') plt.yscale('log') - plt.savefig("LossVsEpoch_"+name+"_"+str(hyperparameters.model_version)+".png") + plt.savefig(hyperparameters.model_dir+"/LossVsEpoch_"+hyperparameters.model_name+".png") - torch.jit.script(model).save(name+"_"+str(hyperparameters.model_version)+".pt") + torch.jit.script(model).save(hyperparameters.model_dir+"/"+hyperparameters.model_name+".pt") return def run_experiment(hyperparameters): @@ -117,63 +102,62 @@ def run_experiment(hyperparameters): # Load training data in tensors training_data = pd.DataFrame() - for i in range(1,int(hyperparameters.num_training_inputs)+1): - temp_training_data = pd.read_csv(hyperparameters.input_files+str(i)+'.txt', delimiter='\t', header=None) + for i in hyperparameters.input_files: + temp_training_data = pd.read_csv(i, delimiter='\t', header=None) training_data = pd.concat([training_data, temp_training_data], ignore_index=True) training_RP_pos_tensor = torch.tensor(training_data.iloc[:,3:7].values, dtype=torch.float32) training_MC_mom_tensor = torch.tensor(training_data.iloc[:,0:3].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) + match hyperparameters.model_name: + case "model_pz": + source = training_RP_pos_tensor + scaled_source, mean_source, std_source = standardize(source) + target = training_MC_mom_tensor[:,2].unsqueeze(1) + + case "model_py": + source = torch.cat((training_RP_pos_tensor[:,2:4], training_MC_mom_tensor[:,2].unsqueeze(1)), 1) + scaled_source, mean_source, std_source = standardize(source) + target = training_MC_mom_tensor[:,1].unsqueeze(1) + + case "model_px": + source = torch.cat((training_RP_pos_tensor[:,0:2], training_MC_mom_tensor[:,2].unsqueeze(1)), 1) + scaled_source, mean_source, std_source = standardize(source) + target = training_MC_mom_tensor[:,0].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) + case _: + print("No model name provided. Stop further processing") + return # Initialize models - initial_model_pz = NeuralNet(size_input=int(hyperparameters.size_input_pz), - size_output=int(hyperparameters.size_output_pz), - n_layers=int(hyperparameters.n_layers_pz), - size_first_hidden_layer=int(hyperparameters.size_first_hidden_layer_pz), - multiplier=float(hyperparameters.multiplier_pz), - leak_rate=float(hyperparameters.leak_rate_pz)) - initial_model_py = NeuralNet(size_input=int(hyperparameters.size_input_py), - size_output=int(hyperparameters.size_output_py), - n_layers=int(hyperparameters.n_layers_py), - size_first_hidden_layer=int(hyperparameters.size_first_hidden_layer_py), - multiplier=float(hyperparameters.multiplier_py), - leak_rate=float(hyperparameters.leak_rate_py)) - initial_model_px = NeuralNet(size_input=int(hyperparameters.size_input_px), - size_output=int(hyperparameters.size_output_px), - n_layers=int(hyperparameters.n_layers_px), - size_first_hidden_layer=int(hyperparameters.size_first_hidden_layer_px), - multiplier=float(hyperparameters.multiplier_px), - leak_rate=float(hyperparameters.leak_rate_px)) - + initial_model = NeuralNet(size_input=int(hyperparameters.size_input), + size_output=int(hyperparameters.size_output), + n_layers=int(hyperparameters.n_layers), + size_first_hidden_layer=int(hyperparameters.size_first_hidden_layer), + multiplier=float(hyperparameters.multiplier), + leak_rate=float(hyperparameters.leak_rate)) + # Train models - train_model("model_pz", scaled_source_pz, target_pz, initial_model_pz, hyperparameters) - train_model("model_py", scaled_source_py, target_py, initial_model_py, hyperparameters) - train_model("model_px", scaled_source_px, target_px, initial_model_px, hyperparameters) - + train_model(scaled_source, target, initial_model, hyperparameters) + # Print end statement - print("Training completed using "+str(int(hyperparameters.nevents_per_file)*int(hyperparameters.num_training_inputs))+" generated events.") + print("Training completed using "+str(len(hyperparameters.input_files))+" files with "+str(training_RP_pos_tensor.shape[0])+" eligible events") if __name__ == "__main__": - parser = argparse.ArgumentParser(fromfile_prefix_chars='@') - hyperparameters_list = ['--input_files', '--model_version', '--nevents_per_file', '--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_argument(hyperparameter) - hyperparameters = parser.parse_args(['@'+str(sys.argv[1])]) + parser = argparse.ArgumentParser(description="Train neural network model for roman pots") + parser.add_argument('--input_files', type=str, nargs='+', required=True, help='Specify a location of input files.') + parser.add_argument('--model_name', type=str, required=True, help='Specify model name.') + parser.add_argument('--model_dir', type=str, required=True, help='Specify location to save model') + parser.add_argument('--num_epochs', type=int, required=True, help='Specify number of epochs') + parser.add_argument('--learning_rate', type=float, required=True, help='Specify learning rate') + parser.add_argument('--size_input', type=int, required=True, help='Specify input size') + parser.add_argument('--size_output', type=int, required=True, help='Specify output size') + parser.add_argument('--n_layers', type=int, required=True, help='Specify number of layers') + parser.add_argument('--size_first_hidden_layer', type=int, required=True, help='Size of first hidden layer') + parser.add_argument('--multiplier', type=float, required=True, help='Specify mutilplier to calculate size of subsequent hidden layers') + parser.add_argument('--leak_rate', type=float, required=True, help='Specify leak rate') + hyperparameters = parser.parse_args() run_experiment(hyperparameters) From 5496e3c6720439820c8593f09a0ca41de2fd3591 Mon Sep 17 00:00:00 2001 From: David Ruth Date: Sun, 11 Feb 2024 21:53:51 -0500 Subject: [PATCH 29/31] Changed Snakefile and preprocess_model_training_data.cxx to include the Low Pt model training --- benchmarks/roman_pots/Snakefile | 38 ++++++++++++++++++- .../preprocess_model_training_data.cxx | 19 ++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index d0d66e4f..86ae5e2c 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -136,10 +136,11 @@ rule roman_pots_preprocess_model_training_data: data = "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/raw_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.edm4hep.root", script = "preprocess_model_training_data.cxx" output: - "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.txt" + full = "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_{index}.txt", + lo = "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_lo_{index}.txt" shell: """ - root -q -b {input.script}\"(\\\"{input.data}\\\",\\\"{output}\\\")\" + root -q -b {input.script}\"(\\\"{input.data}\\\",\\\"{output.full}\\\",\\\"{output.lo}\\\")\" """ rule roman_pots_train_model_pz: @@ -193,3 +194,36 @@ rule roman_pots_train_model_px: python {input.script} --input_files {input.data} --model_name model_px --model_dir results/{params.detector_version}/{params.detector_config}/detector_benchmarks/{params.subsystem}/{params.benchmark}/artifacts/model_px/num_epochs_{wildcards.num_epochs}/learning_rate_{wildcards.learning_rate}/size_input_{wildcards.size_input}/size_output_{wildcards.size_output}/n_layers_{wildcards.n_layers}/size_first_hidden_layer_{wildcards.size_first_hidden_layer}/multiplier_{wildcards.multiplier}/leak_rate_{wildcards.leak_rate} --num_epochs {wildcards.num_epochs} --learning_rate {wildcards.learning_rate} --size_input {wildcards.size_input} --size_output {wildcards.size_output} --n_layers {wildcards.n_layers} --size_first_hidden_layer {wildcards.size_first_hidden_layer} --multiplier {wildcards.multiplier} --leak_rate {wildcards.leak_rate} """ +rule roman_pots_train_model_py_lo: + input: + data = ["results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_lo_{index}.txt".format(index=index) for index in NFILES], + script = "train_dense_neural_network.py" + params: + detector_version=DETECTOR_VERSION, + detector_config=DETECTOR_CONFIG, + subsystem=SUBSYSTEM, + benchmark=BENCHMARK + output: + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_py/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/model_py_lo.pt", + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_py/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/LossVsEpoch_model_py_lo.png" + shell: + """ + python {input.script} --input_files {input.data} --model_name model_py --model_dir results/{params.detector_version}/{params.detector_config}/detector_benchmarks/{params.subsystem}/{params.benchmark}/artifacts/model_py/num_epochs_{wildcards.num_epochs}/learning_rate_{wildcards.learning_rate}/size_input_{wildcards.size_input}/size_output_{wildcards.size_output}/n_layers_{wildcards.n_layers}/size_first_hidden_layer_{wildcards.size_first_hidden_layer}/multiplier_{wildcards.multiplier}/leak_rate_{wildcards.leak_rate} --num_epochs {wildcards.num_epochs} --learning_rate {wildcards.learning_rate} --size_input {wildcards.size_input} --size_output {wildcards.size_output} --n_layers {wildcards.n_layers} --size_first_hidden_layer {wildcards.size_first_hidden_layer} --multiplier {wildcards.multiplier} --leak_rate {wildcards.leak_rate} + """ + +rule roman_pots_train_model_px_lo: + input: + data = ["results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/processed_data/"+DETECTOR_VERSION+"_"+DETECTOR_CONFIG+"_lo_{index}.txt".format(index=index) for index in NFILES], + script = "train_dense_neural_network.py" + params: + detector_version=DETECTOR_VERSION, + detector_config=DETECTOR_CONFIG, + subsystem=SUBSYSTEM, + benchmark=BENCHMARK + output: + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_px/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/model_px_lo.pt", + "results/"+DETECTOR_VERSION+"/"+DETECTOR_CONFIG+"/detector_benchmarks/"+SUBSYSTEM+"/"+BENCHMARK+"/artifacts/model_px/num_epochs_{num_epochs}/learning_rate_{learning_rate}/size_input_{size_input}/size_output_{size_output}/n_layers_{n_layers}/size_first_hidden_layer_{size_first_hidden_layer}/multiplier_{multiplier}/leak_rate_{leak_rate}/LossVsEpoch_model_px_lo.png" + shell: + """ + python {input.script} --input_files {input.data} --model_name model_px --model_dir results/{params.detector_version}/{params.detector_config}/detector_benchmarks/{params.subsystem}/{params.benchmark}/artifacts/model_px/num_epochs_{wildcards.num_epochs}/learning_rate_{wildcards.learning_rate}/size_input_{wildcards.size_input}/size_output_{wildcards.size_output}/n_layers_{wildcards.n_layers}/size_first_hidden_layer_{wildcards.size_first_hidden_layer}/multiplier_{wildcards.multiplier}/leak_rate_{wildcards.leak_rate} --num_epochs {wildcards.num_epochs} --learning_rate {wildcards.learning_rate} --size_input {wildcards.size_input} --size_output {wildcards.size_output} --n_layers {wildcards.n_layers} --size_first_hidden_layer {wildcards.size_first_hidden_layer} --multiplier {wildcards.multiplier} --leak_rate {wildcards.leak_rate} + """ diff --git a/benchmarks/roman_pots/preprocess_model_training_data.cxx b/benchmarks/roman_pots/preprocess_model_training_data.cxx index 748c939a..47d7e4c6 100644 --- a/benchmarks/roman_pots/preprocess_model_training_data.cxx +++ b/benchmarks/roman_pots/preprocess_model_training_data.cxx @@ -9,11 +9,11 @@ // // Author: Alex Jentsch //------------------------ - +//Low PT preprocessing added by David Ruth using namespace std; -void preprocess_model_training_data(TString inputFile, TString outputFile) { +void preprocess_model_training_data(TString inputFile, TString outputFile, TString outputFile_lo) { string fileName; TFile* inputRootFile; @@ -52,6 +52,8 @@ void preprocess_model_training_data(TString inputFile, TString outputFile) { ofstream outputTrainingFile; outputTrainingFile.open(outputFile); + ofstream outputTrainingFile_lo; + outputTrainingFile_lo.open(outputFile_lo); // MC particles TTreeReaderArray mc_px_array = { tree_reader, "MCParticles.momentum.x" }; TTreeReaderArray mc_py_array = { tree_reader, "MCParticles.momentum.y" }; @@ -81,6 +83,7 @@ void preprocess_model_training_data(TString inputFile, TString outputFile) { bool hasMCProton = false; bool hitLayerOne = false; bool hitLayerTwo = false; + bool lowPT = false; double mcProtonMomentum[3]; TVector3 mctrk; @@ -127,7 +130,12 @@ void preprocess_model_training_data(TString inputFile, TString outputFile) { hitLayerTwo = true; } } - + if (hasMCProton) { + double Pt = sqrt(pow(mcProtonMomentum[0],2) + pow(mcProtonMomentum[1],2)); + if(Pt < 0.3) { + lowPT = true; + } + } if (hasMCProton && hitLayerOne && hitLayerTwo) { double slope_x = (rpHitLayerTwo[0] - rpHitLayerOne[0]) / (rpHitLayerTwo[2] - rpHitLayerOne[2]); @@ -135,6 +143,10 @@ void preprocess_model_training_data(TString inputFile, TString outputFile) { outputTrainingFile << mcProtonMomentum[0] << "\t" << mcProtonMomentum[1] << "\t" << mcProtonMomentum[2] << "\t"; outputTrainingFile << rpHitLayerTwo[0] << "\t" << slope_x << "\t" << rpHitLayerTwo[1] << "\t" << slope_y << endl; + if (lowPT) { + outputTrainingFile_lo << mcProtonMomentum[0] << "\t" << mcProtonMomentum[1] << "\t" << mcProtonMomentum[2] << "\t"; + outputTrainingFile_lo << rpHitLayerTwo[0] << "\t" << slope_x << "\t" << rpHitLayerTwo[1] << "\t" << slope_y << endl; + } } iEvent++; @@ -142,6 +154,7 @@ void preprocess_model_training_data(TString inputFile, TString outputFile) { inputRootFile->Close(); outputTrainingFile.close(); + outputTrainingFile_lo.close(); return; } From b0c519832d0072c5ff40a56335b49b5b08ddc898 Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Tue, 4 Jun 2024 08:47:51 -0400 Subject: [PATCH 30/31] Extract detector version --- benchmarks/roman_pots/Snakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmarks/roman_pots/Snakefile b/benchmarks/roman_pots/Snakefile index 86ae5e2c..e80af748 100644 --- a/benchmarks/roman_pots/Snakefile +++ b/benchmarks/roman_pots/Snakefile @@ -1,7 +1,7 @@ from itertools import product DETECTOR_PATH = os.environ["DETECTOR_PATH"] -DETECTOR_VERSION = os.environ["DETECTOR_VERSION"] +DETECTOR_VERSION = re.search(r"epic-.*.-", DETECTOR_PATH).group().removeprefix("epic-").removesuffix("-") SUBSYSTEM = "roman_pots" BENCHMARK = "dense_neural_network" DETECTOR_CONFIG = "epic_ip6" From a695c51f45bd697af926fa0d6eea50de582be2ad Mon Sep 17 00:00:00 2001 From: Sakib Rahman Date: Tue, 4 Jun 2024 09:19:29 -0400 Subject: [PATCH 31/31] The default position of the planes updated to reflect current locations in epic --- benchmarks/roman_pots/preprocess_model_training_data.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/benchmarks/roman_pots/preprocess_model_training_data.cxx b/benchmarks/roman_pots/preprocess_model_training_data.cxx index 47d7e4c6..5d327ac7 100644 --- a/benchmarks/roman_pots/preprocess_model_training_data.cxx +++ b/benchmarks/roman_pots/preprocess_model_training_data.cxx @@ -100,10 +100,10 @@ void preprocess_model_training_data(TString inputFile, TString outputFile, TStri } } - double hit1minZ = 25099.0; - double hit1maxZ = 26022.0; - double hit2minZ = 27099.0; - double hit2maxZ = 28022.0; + double hit1minZ = 32547.3-923/2.0; + double hit1maxZ = 32547.3+923/2.0; + double hit2minZ = 34245.5-923/2.0; + double hit2maxZ = 34245.5+923/2.0; double rpHitLayerOne[3]; double rpHitLayerTwo[3];