bugroup/Eye_Tracking_Drift_Correction
3
1import torch as t2 3 4def macro_soft_f1(real_vals, predictions, reduction):5 """from https://towardsdatascience.com/the-unknown-benefits-of-using-a-soft-f1-loss-in-classification-systems-753902c0105d"""6 true_positive = (real_vals * predictions).sum(dim=0)7 false_positive = (predictions * (1 - real_vals)).sum(dim=0)8 false_negative = ((1 - predictions) * real_vals).sum(dim=0)9 soft_f1 = 2 * true_positive / (2 * true_positive + false_negative + false_positive + 1e-16)10 if reduction == "mean":11 loss = t.mean(1 - soft_f1)12 else:13 loss = 1 - soft_f114 return loss15 16 17def coral_loss(logits, levels, importance_weights=None, reduction="mean"):18 """Computes the CORAL loss described in19 Cao, Mirjalili, and Raschka (2020)20 *Rank Consistent Ordinal Regression for Neural Networks21 with Application to Age Estimation*22 Pattern Recognition Letters, https://doi.org/10.1016/j.patrec.2020.11.00823 Parameters24 ----------25 logits : torch.tensor, shape(num_examples, num_classes-1)26 Outputs of the CORAL layer.27 levels : torch.tensor, shape(num_examples, num_classes-1)28 True labels represented as extended binary vectors29 (via `coral_pytorch.dataset.levels_from_labelbatch`).30 importance_weights : torch.tensor, shape=(num_classes-1,) (default=None)31 Optional weights for the different labels in levels.32 A tensor of ones, i.e.,33 `torch.ones(num_classes-1, dtype=torch.float32)`34 will result in uniform weights that have the same effect as None.35 reduction : str or None (default='mean')36 If 'mean' or 'sum', returns the averaged or summed loss value across37 all data points (rows) in logits. If None, returns a vector of38 shape (num_examples,)39 Returns40 ----------41 loss : torch.tensor42 A torch.tensor containing a single loss value (if `reduction='mean'` or '`sum'`)43 or a loss value for each data record (if `reduction=None`).44 Examples45 ----------46 >>> import torch47 >>> from coral_pytorch.losses import coral_loss48 >>> levels = torch.tensor(49 ... [[1., 1., 0., 0.],50 ... [1., 0., 0., 0.],51 ... [1., 1., 1., 1.]])52 >>> logits = torch.tensor(53 ... [[2.1, 1.8, -2.1, -1.8],54 ... [1.9, -1., -1.5, -1.3],55 ... [1.9, 1.8, 1.7, 1.6]])56 >>> coral_loss(logits, levels)57 tensor(0.6920)58 https://github.com/Raschka-research-group/coral-pytorch/blob/c6ab93afd555a6eac708c95ae1feafa15f91c5aa/coral_pytorch/losses.py59 """60 61 if not logits.shape == levels.shape:62 raise ValueError(63 "Please ensure that logits (%s) has the same shape as levels (%s). " % (logits.shape, levels.shape)64 )65 66 term1 = t.nn.functional.logsigmoid(logits) * levels + (t.nn.functional.logsigmoid(logits) - logits) * (1 - levels)67 68 if importance_weights is not None:69 term1 *= importance_weights70 71 val = -t.sum(term1, dim=1)72 73 if reduction == "mean":74 loss = t.mean(val)75 elif reduction == "sum":76 loss = t.sum(val)77 elif reduction is None:78 loss = val79 else:80 s = 'Invalid value for `reduction`. Should be "mean", ' '"sum", or None. Got %s' % reduction81 raise ValueError(s)82 83 return loss84 85 86def corn_loss(logits, y_train, num_classes):87 """Computes the CORN loss described in our forthcoming88 'Deep Neural Networks for Rank Consistent Ordinal89 Regression based on Conditional Probabilities'90 manuscript.91 Parameters92 ----------93 logits : torch.tensor, shape=(num_examples, num_classes-1)94 Outputs of the CORN layer.95 y_train : torch.tensor, shape=(num_examples)96 Torch tensor containing the class labels.97 num_classes : int98 Number of unique class labels (class labels should start at 0).99 Returns100 ----------101 loss : torch.tensor102 A torch.tensor containing a single loss value.103 Examples104 ----------105 >>> import torch106 >>> from coral_pytorch.losses import corn_loss107 >>> # Consider 8 training examples108 >>> _ = torch.manual_seed(123)109 >>> X_train = torch.rand(8, 99)110 >>> y_train = torch.tensor([0, 1, 2, 2, 2, 3, 4, 4])111 >>> NUM_CLASSES = 5112 >>> #113 >>> #114 >>> # def __init__(self):115 >>> corn_net = torch.nn.Linear(99, NUM_CLASSES-1)116 >>> #117 >>> #118 >>> # def forward(self, X_train):119 >>> logits = corn_net(X_train)120 >>> logits.shape121 torch.Size([8, 4])122 >>> corn_loss(logits, y_train, NUM_CLASSES)123 tensor(0.7127, grad_fn=<DivBackward0>)124 https://github.com/Raschka-research-group/coral-pytorch/blob/c6ab93afd555a6eac708c95ae1feafa15f91c5aa/coral_pytorch/losses.py125 """126 sets = []127 for i in range(num_classes - 1):128 label_mask = y_train > i - 1129 label_tensor = (y_train[label_mask] > i).to(t.int64)130 sets.append((label_mask, label_tensor))131 132 num_examples = 0133 losses = 0.0134 for task_index, s in enumerate(sets):135 train_examples = s[0]136 train_labels = s[1]137 138 if len(train_labels) < 1:139 continue140 141 num_examples += len(train_labels)142 pred = logits[train_examples, task_index]143 144 loss = -t.sum(145 t.nn.functional.logsigmoid(pred) * train_labels146 + (t.nn.functional.logsigmoid(pred) - pred) * (1 - train_labels)147 )148 losses += loss149 150 return losses / num_examples151 152 153def corn_label_from_logits(logits):154 """155 Returns the predicted rank label from logits for a156 network trained via the CORN loss.157 Parameters158 ----------159 logits : torch.tensor, shape=(n_examples, n_classes)160 Torch tensor consisting of logits returned by the161 neural net.162 Returns163 ----------164 labels : torch.tensor, shape=(n_examples)165 Integer tensor containing the predicted rank (class) labels166 Examples167 ----------168 >>> # 2 training examples, 5 classes169 >>> logits = torch.tensor([[14.152, -6.1942, 0.47710, 0.96850],170 ... [65.667, 0.303, 11.500, -4.524]])171 >>> corn_label_from_logits(logits)172 tensor([1, 3])173 https://github.com/Raschka-research-group/coral-pytorch/blob/c6ab93afd555a6eac708c95ae1feafa15f91c5aa/coral_pytorch/dataset.py174 """175 probas = t.sigmoid(logits)176 probas = t.cumprod(probas, dim=1)177 predict_levels = probas > 0.5178 predicted_labels = t.sum(predict_levels, dim=1)179 return predicted_labels180 