Module bioiain.machine.losses
Classes
class CustomHalfHalf (weights=None)-
Expand source code
class CustomHalfHalf(CustomLoss): def __init__(self, weights=None): log(1, "Using label weights:") if type(weights) is dict: weights = weights.values() w = np.array(list(weights)) w = w w = w / w.sum() w = torch.Tensor(w) log(2, w) self.CEL = nn.MSELoss() self.weight = w def __call__(self, o, item): true_index = item.li true_tensor = item.lt pred = torch.max(o, dim=0)[1] if len(item.lt) == 10: if item.li < 5: true_tensor[:5] = 0.5 else: true_tensor[5:] = 0.5 true_tensor[item.li:item.li+1] = 1. #print(true_tensor) weighted_out = o# * self.weight #print("Weighted out:", weighted_out) loss = self.CEL(o, true_tensor) #if true_index < 5 == torch.max(o, dim=0)[1] < 5: # loss *= 0.5 loss *= self.weight[pred] return lossAncestors
class CustomLoss-
Expand source code
class CustomLoss(object): passSubclasses
class CustomWeighted (weights=None, addto1=False)-
Expand source code
class CustomWeighted(CustomLoss): def __init__(self, weights=None, addto1=False): log(1, "Using label weights:") if weights is not None: if type(weights) is dict: weights = weights.values() w = np.array(list(weights)) print(w) if addto1: w = w / w.sum() else: w = w / w.max() self.weight = torch.Tensor(w) self.CEL = nn.MSELoss() log(2, self.weight) def __call__(self, o, item): true_index = item.li true_tensor = item.lt loss = self.CEL(o, true_tensor) pred_index = torch.max(o, dim=0)[1] if self.weight is not None: pred_weight = self.weight[pred_index] loss *= pred_weight return lossAncestors
class VQLoss (weights=None)-
Expand source code
class VQLoss(CustomLoss): def __init__(self, weights=None): self.MSE = nn.MSELoss() self.CSL = nn.CosineEmbeddingLoss(margin=0.) def encoder_loss_vq_vae(self, o, tensor, commitment=0.25): loss = self.MSE(o, tensor) loss = loss * (1 + commitment) return loss def encoder_loss(self, l, token, origin, commitment=0.25): token = token.to(DEVICE) origin = origin.to(DEVICE) #print() #print("L", l) #print("T", token) #print("O", origin) t_l = torch_distance(l, token) o_t = torch_distance(token, origin) o_l = torch_distance(l, origin) y = torch.Tensor([-1])[0] z = torch.zeros(1).to(DEVICE) #print("TL", t_l) #print("OT", o_t) #print("OL", o_l) #print("Y", y) #loss = self.CSL(t_l, o_l, y) #print(loss) #print("LOSS", l_t.item(), "-", (o_l-o_t).item()) #loss = t_l-(o_l-o_t) loss = (t_l-o_l+o_t) / t_l loss = torch.max(z, loss * (1 + commitment)) #print(loss) return loss def decoder_loss(self, o, tensor): loss = self.MSE(o, tensor) return loss def __call__(self, e_loss, d_loss): loss = e_loss + d_loss return lossAncestors
Methods
def decoder_loss(self, o, tensor)-
Expand source code
def decoder_loss(self, o, tensor): loss = self.MSE(o, tensor) return loss def encoder_loss(self, l, token, origin, commitment=0.25)-
Expand source code
def encoder_loss(self, l, token, origin, commitment=0.25): token = token.to(DEVICE) origin = origin.to(DEVICE) #print() #print("L", l) #print("T", token) #print("O", origin) t_l = torch_distance(l, token) o_t = torch_distance(token, origin) o_l = torch_distance(l, origin) y = torch.Tensor([-1])[0] z = torch.zeros(1).to(DEVICE) #print("TL", t_l) #print("OT", o_t) #print("OL", o_l) #print("Y", y) #loss = self.CSL(t_l, o_l, y) #print(loss) #print("LOSS", l_t.item(), "-", (o_l-o_t).item()) #loss = t_l-(o_l-o_t) loss = (t_l-o_l+o_t) / t_l loss = torch.max(z, loss * (1 + commitment)) #print(loss) return loss def encoder_loss_vq_vae(self, o, tensor, commitment=0.25)-
Expand source code
def encoder_loss_vq_vae(self, o, tensor, commitment=0.25): loss = self.MSE(o, tensor) loss = loss * (1 + commitment) return loss
class customLRS (*args, optimiser, use_original=True, range=4, **kwargs)-
Expand source code
class customLRS(torch.optim.lr_scheduler.LRScheduler): def __init__(self, *args, optimiser, use_original=True, range=4, **kwargs): self.o_lrs = [p["lr"] for p in optimiser.param_groups] self.use_original = use_original self.loss_list = [] super().__init__(optimiser, *args, **kwargs) def get_lr(self): print("LRS: getting lrs") print(self.lrs) return torch.Tensor(np.array(self.lrs)) def step(self, running_loss=None): print("LRS: stepping...") print("Current loss:", running_loss) if running_loss is None: return self.o_lrs if len(self.loss_list) == 0: self.loss_list.append(running_loss) print("Previous loss", self.loss_list[-1]) self.lrs = [] for p, olr in zip(self.optimizer.param_groups, self.o_lrs): if self.use_original: old_log = math.log(olr, 10) new_log = old_log - ((1-running_loss)*4) + 2 else: old_log = math.log(p["lr"], 10) delta = self.loss_list[-1] / running_loss new_log = old_log - delta + 1 # -0.5 print("OLD_LOG", old_log) print("NEW_LOG", new_log) new_lr = 10 ** new_log print("NEW_LR", new_lr) self.lrs.append(new_lr) p["lr"] = torch.Tensor(np.array([new_lr])) self.loss_list.append(running_loss) print(self.lrs) return self.lrsBase class for all learning rate schedulers.
Subclasses implement :meth:
get_lrand optionally override :meth:stepto define scheduling behavior.Args
optimizer:Optimizer- The optimizer this scheduler will adjust the learning rates of.
last_epoch:int- Index of the last epoch seen by the scheduler. Use
-1(default) to initialize the scheduler. Only use a non-default value when restoring this scheduler from a saved checkpoint.
Warning
Initializing a scheduler overwrites its optimizer's
param_group["lr"]\s. When restoring a checkpoint, initialize the scheduler before calling your optimizer's :meth:~torch.optim.Optimizer.load_state_dictto avoid overwriting the loaded learning rates.Ancestors
- torch.optim.lr_scheduler.LRScheduler
Methods
def get_lr(self)-
Expand source code
def get_lr(self): print("LRS: getting lrs") print(self.lrs) return torch.Tensor(np.array(self.lrs))Compute the next learning rate for each of the optimizer's :attr:
~torch.optim.Optimizer.param_groups.Returns
list[float | Tensor]- A :class:
listof learning rates for each of
the optimizer's :attr:
~torch.optim.Optimizer.param_groupswith the same types as their currentgroup["lr"]\s.Note
If you're trying to inspect the most recent learning rate, use :meth:
get_last_lr()instead.Note
The returned :class:
~torch.Tensor\s are copies, and never alias the optimizer'sgroup["lr"]\s. def step(self, running_loss=None)-
Expand source code
def step(self, running_loss=None): print("LRS: stepping...") print("Current loss:", running_loss) if running_loss is None: return self.o_lrs if len(self.loss_list) == 0: self.loss_list.append(running_loss) print("Previous loss", self.loss_list[-1]) self.lrs = [] for p, olr in zip(self.optimizer.param_groups, self.o_lrs): if self.use_original: old_log = math.log(olr, 10) new_log = old_log - ((1-running_loss)*4) + 2 else: old_log = math.log(p["lr"], 10) delta = self.loss_list[-1] / running_loss new_log = old_log - delta + 1 # -0.5 print("OLD_LOG", old_log) print("NEW_LOG", new_log) new_lr = 10 ** new_log print("NEW_LR", new_lr) self.lrs.append(new_lr) p["lr"] = torch.Tensor(np.array([new_lr])) self.loss_list.append(running_loss) print(self.lrs) return self.lrsStep the scheduler.
Args
epoch (int, optional): !!! deprecated "Deprecated since version: 1.4" If provided, sets :attr:
last_epochtoepochand uses :meth:_get_closed_form_lrif it is available. This is not universally supported. Use :meth:stepwithout arguments instead.Note
Call this method after calling the optimizer's :meth:
~torch.optim.Optimizer.step.