Skip to content
Snippets Groups Projects
Commit d804a782 authored by Krishna Krishna Nikhil's avatar Krishna Krishna Nikhil
Browse files

Update optimizer.py

parent 33560926
Branches
No related tags found
No related merge requests found
......@@ -39,16 +39,17 @@ class Optimizer(object):
self.surrogate_model.to(device)
self.cu_box.to(device)
def other_constraints(self, dataset):
def other_constraints(self, dataset, ToP, scale_update):
# keep the size of the detector within 3m
raw_detector_parameters = self.detector_parameters
# this creates a loss that increases starting from 3 meters using relu
detector_length = torch.sum(raw_detector_parameters[:6])# changed this here so as to not include material choice in this
thickness_parameters = self.detector_parameters[0:6]
material_parameters = self.detector_parameters[6:12]
abs_parameters = self.detector_parameters[6:9]
scint_parameters = self.detector_parameters[9:12]
number_parameters = len(self.detector_parameters)
divider = int(((number_parameters//ToP)*3)/2)
thickness_parameters = self.detector_parameters[0:(number_parameters//ToP)]
material_parameters = self.detector_parameters[(number_parameters//ToP):(number_parameters//ToP)*2]
abs_parameters = self.detector_parameters[(number_parameters//ToP): divider ]
scint_parameters = self.detector_parameters[divider : (number_parameters//ToP)*2]
# cost_dic = {"G4_POLYSTYRENE": 0.001
# ,"G4_PLASTIC_SC_VINYLTOLUENE": 0.001
......@@ -64,18 +65,24 @@ class Optimizer(object):
# ,"G4_Si": 8330000
# ,"G4_PbWO4": 2500 }
scale_factor = 1 + scale_update * 0.02
if self.constraints is not None:
zero_length = (torch.zeros(number_parameters//2)).to(self.device)
negative_length_loss = torch.mean(0.5*torch.nn.ReLU()(zero_length- thickness_parameters)**2)
if 'length' in self.constraints:
detector_length = torch.sum(raw_detector_parameters[:6])# changed this here so as to not include material choice in this
total_length_loss = torch.mean(100.*torch.nn.ReLU()(detector_length - self.constraints['length'])**2)#constrain it to 25cm
detector_length = torch.sum(raw_detector_parameters[:(number_parameters//ToP)])# changed this here so as to not include material choice in this
total_length_loss = torch.mean(10.*torch.nn.ReLU()(detector_length - self.constraints['length'])**2)#constrain it to 25cm
if 'lower' in self.constraints:
lbound = (torch.ones(12) * self.constraints['lower']).to(self.device)
lower_loss = torch.mean(100.*torch.nn.ReLU()(lbound- raw_detector_parameters)**2)
lbound = (torch.ones(number_parameters//2) * self.constraints['lower']).to(self.device)
lower_loss = torch.mean(0.5*torch.nn.ReLU()(lbound- material_parameters)**2)
if 'upper' in self.constraints:
ubound = (torch.ones(6) * self.constraints['upper']).to(self.device)
upper_loss = torch.mean(100.*torch.nn.ReLU()(material_parameters - ubound)**2)
ubound = (torch.ones(number_parameters//2) * self.constraints['upper']).to(self.device)
upper_loss = torch.mean(0.5*torch.nn.ReLU()(material_parameters - ubound)**2)
if 'diff' in self.constraints:
abs_maxdiff = max(abs_parameters) - min(abs_parameters)
scint_maxdiff = max(scint_parameters)- min(scint_parameters)
......@@ -83,26 +90,23 @@ class Optimizer(object):
diff_loss = torch.mean(100.*torch.nn.ReLU()(abs_maxdiff - self.constraints['diff'])**2)
diff_loss += torch.mean(100.*torch.nn.ReLU()(scint_maxdiff - self.constraints['diff'])**2)
if 'cost' in self.constraints:
# cost = w * cost_material_a + (1-w) * cost_material_b
# then you can scale w in a way that makes it more 'strict', so e.g. with a sigmoid(10*(w-0.5)) (please plot that and check if that makes sense before using it)
def sigmoid(number):
return 1/ (1 + torch.exp(-number))
abs_sigm = sigmoid(10*(abs_parameters-0.5))
abs_sigm = sigmoid(scale_factor*(abs_parameters))
cost_abs = abs_sigm * 25 + (1 - abs_sigm) * 4.166
scint_sigm = sigmoid(10*(scint_parameters-0.5))
cost_scint = scint_sigm * 2500 + (1 - scint_sigm) * 0.001
scint_sigm = sigmoid(scale_factor*(scint_parameters))
cost_scint = scint_sigm * 2500 + (1 - scint_sigm) * 0.01
combined = torch.cat((cost_abs, cost_scint), dim=0)
combined_cost = combined.to(self.device)
cost = torch.sum(combined_cost * thickness_parameters)
cost_loss = torch.mean(50.* torch.nn.ReLU()(cost - self.constraints['cost'])**2)
cost_loss = torch.mean((2/self.constraints['cost'])* torch.nn.ReLU()(cost - self.constraints['cost'])**2)
# now keep parameters such that within the box size of the generator, there are always some positive values even if the
# central parameters are negative. Both box size and raw_detector_parameters are in non-normalised space, so this is straight forward
......@@ -114,7 +118,7 @@ class Optimizer(object):
# bloss = torch.mean(100.*torch.nn.ReLU()(lower_para_bound - raw_detector_parameters)**2)
# + bloss
return total_length_loss +lower_loss + upper_loss + diff_loss + cost_loss
return total_length_loss +lower_loss + upper_loss + cost_loss + negative_length_loss
def clamp_parameters(self):
return
......@@ -134,10 +138,10 @@ class Optimizer(object):
# Adjust the original covariance matrix
self.generator.box_covariance = np.diag(self.generator.box_size**2) + M_scaled
print('new box_covariance', self.generator.box_covariance)
# print('new box_covariance', self.generator.box_covariance)
def optimize(self, dataset, batch_size, n_epochs, lr, add_constraints = False):
def optimize(self, dataset, ToP , scale_update ,batch_size, n_epochs, lr, add_constraints = False):
'''
keep both models fixed, train only the detector parameters (self.detector_start_parameters)
using the reconstruction model loss
......@@ -152,12 +156,16 @@ class Optimizer(object):
data_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
# save the initial parameters
initial_parameters = self.detector_parameters.detach().cpu().numpy()
initial_parameters = np.copy(self.detector_parameters.detach().cpu().numpy())
# loop over the batches
mean_loss = 0
reco_surrogate_loss = 0
constraint_loss = 0
for epoch in range(n_epochs):
mean_loss = 0 #only use last epoch
mean_loss = 0
reco_surrogate_loss = 0
constraint_loss = 0
stop_epoch = False
for batch_idx, (_, true_inputs, true_context, reco_result) in enumerate(data_loader):
......@@ -172,10 +180,13 @@ class Optimizer(object):
true_inputs,
true_context)
# calculate the loss
loss = 5 * self.reconstruction_model.loss(dataset.unnormalise_target(reco_surrogate), dataset.unnormalise_target(true_inputs))
loss = self.reconstruction_model.loss(dataset.unnormalise_target(reco_surrogate), dataset.unnormalise_target(true_inputs))
reco_surrogate_loss += loss.item()
if add_constraints:
loss += self.other_constraints(dataset)
#
constraint_loss_value = self.other_constraints(dataset, ToP, scale_update)
constraint_loss += constraint_loss_value.item()
loss += constraint_loss_value
self.optimizer.zero_grad()
loss.backward()
#print('gradient',self.detector_parameters.grad, 'should have', self.detector_parameters.requires_grad)
......@@ -198,6 +209,7 @@ class Optimizer(object):
#check if the parameters are still local otherwise stop
if not self.generator.is_local(self.detector_parameters.detach().cpu().numpy(),0.8):#a bit smaller box size to be safe
stop_epoch = True
print("parameter not local")
break
if batch_idx % 20 == 0:
......@@ -211,11 +223,16 @@ class Optimizer(object):
epoch, loss.item()))
if stop_epoch:
break
# if not stop_epoch:
# scale_update += 1
# print('Scale updated to : ', scale_update)
scale_update += 1
self.clamp_parameters()
mean_loss /= batch_idx+1
reco_surrogate_loss/= batch_idx+1
constraint_loss/= batch_idx + 1
self.adjust_generator_covariance( self.detector_parameters.detach().cpu().numpy() - initial_parameters )
return self.detector_parameters.detach().cpu().numpy(), True, mean_loss
return self.detector_parameters.detach().cpu().numpy(), True, mean_loss, reco_surrogate_loss, constraint_loss ,scale_update
def get_optimum(self):
return self.detector_parameters.detach().cpu().numpy()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment