MCPcopy Create free account
hub / github.com/InternScience/InternAgent / uncertainty_loss_fct

Function uncertainty_loss_fct

tasks/AutoTPPR/code/experiment.py:691–737  ·  view source on GitHub ↗

Uncertainty loss function Args: pred (torch.tensor): predicted values logvar (torch.tensor): log variance y (torch.tensor): true values perts (list): list of perturbations reg (float): regularization parameter ctrl (str): control perturbation

(pred, logvar, y, perts, reg = 0.1, ctrl = None,
                         direction_lambda = 1e-3, dict_filter = None)

Source from the content-addressed store, hash-verified

689 return df_co_expression
690
691def uncertainty_loss_fct(pred, logvar, y, perts, reg = 0.1, ctrl = None,
692 direction_lambda = 1e-3, dict_filter = None):
693 """
694 Uncertainty loss function
695
696 Args:
697 pred (torch.tensor): predicted values
698 logvar (torch.tensor): log variance
699 y (torch.tensor): true values
700 perts (list): list of perturbations
701 reg (float): regularization parameter
702 ctrl (str): control perturbation
703 direction_lambda (float): direction loss weight hyperparameter
704 dict_filter (dict): dictionary of perturbations to conditions
705
706 """
707 gamma = 2
708 perts = np.array(perts)
709 losses = torch.tensor(0.0, requires_grad=True).to(pred.device)
710 for p in set(perts):
711 if p!= 'ctrl':
712 retain_idx = dict_filter[p]
713 pred_p = pred[np.where(perts==p)[0]][:, retain_idx]
714 y_p = y[np.where(perts==p)[0]][:, retain_idx]
715 logvar_p = logvar[np.where(perts==p)[0]][:, retain_idx]
716 else:
717 pred_p = pred[np.where(perts==p)[0]]
718 y_p = y[np.where(perts==p)[0]]
719 logvar_p = logvar[np.where(perts==p)[0]]
720
721 # uncertainty based loss
722 losses += torch.sum((pred_p - y_p)**(2 + gamma) + reg * torch.exp(
723 -logvar_p) * (pred_p - y_p)**(2 + gamma))/pred_p.shape[0]/pred_p.shape[1]
724
725 # direction loss
726 if p!= 'ctrl':
727 losses += torch.sum(direction_lambda *
728 (torch.sign(y_p - ctrl[retain_idx]) -
729 torch.sign(pred_p - ctrl[retain_idx]))**2)/\
730 pred_p.shape[0]/pred_p.shape[1]
731 else:
732 losses += torch.sum(direction_lambda *
733 (torch.sign(y_p - ctrl) -
734 torch.sign(pred_p - ctrl))**2)/\
735 pred_p.shape[0]/pred_p.shape[1]
736
737 return losses/(len(set(perts)))
738
739
740def loss_fct(pred, y, perts, ctrl = None, direction_lambda = 1e-3, dict_filter = None):

Callers 1

trainMethod · 0.70

Calls 1

toMethod · 0.45

Tested by

no test coverage detected