def stepTrain(self, batched_data, inference=False): # Turn on training mode which enables dropout. self.model.eval() if inference else self.model.train() input_variable, tensor_label, sen_len = self.get_batch_data(batched_data) hidden = self.model.init_hidden(len(batched_data['sentence_length'])) if inference == False: # zero the parameter gradients self.optimizer.zero_grad() if self.model_name == 'Attention': loss_sim, prob = self.model(input_variable, hidden, sen_len, tensor_label) else: loss_sim, prob, logits = self.model(input_variable, hidden, sen_len, tensor_label) loss_hinge_classify = F.multi_margin_loss(prob, tensor_label) loss_hinge = F.multi_margin_loss(loss_sim, tensor_label) loss = loss_hinge_classify + loss_hinge # loss = loss_hinge_classify if inference == False: loss.backward() self.optimizer.step() return np.array([loss.data.cpu().numpy(), loss_hinge_classify.data.cpu().numpy(), loss_hinge.data.cpu().numpy()]).reshape(3), prob.data.cpu().numpy() else: if self.model_name == 'Attention': return np.array([loss.data.cpu().numpy(), loss_hinge_classify.data.cpu().numpy(), loss_hinge.data.cpu().numpy()]).reshape(3), prob.data.cpu().numpy(), None else: return np.array([loss.data.cpu().numpy(), loss_hinge_classify.data.cpu().numpy(), loss_hinge.data.cpu().numpy()]).reshape(3), prob.data.cpu().numpy(), logits
def loss(self, scores, true_pos, method="SL", lamb=1e-7, isLocal=False): #loss = None # print("----MulRelRanker Loss----") # if method == "SL": if isLocal: loss = F.multi_margin_loss(scores, true_pos, margin=self.margin) else: loss = F.multi_margin_loss(scores, true_pos, margin=self.margin_global) # elif method == "RL": # loss = self.finish_episode(self.rewards, self.saved_log_probs) return loss
def fast_rcnn_losses(cls_score, bbox_pred, label_int32, bbox_targets, bbox_inside_weights, bbox_outside_weights): device_id = cls_score.get_device() rois_label = Variable(torch.from_numpy(label_int32.astype('int64'))).cuda(device_id) if cfg.FAST_RCNN.LOSS_TYPE in ['cross_entropy', 'triplet_softmax']: if cfg.FAST_RCNN.LOSS_TYPE == 'triplet_softmax': cls_score = cls_score * 3 # This method is borrowed from ji zhang's large scale relationship detection if not cfg.MODEL.TAGGING: loss_cls = F.cross_entropy(cls_score, rois_label) else: loss_cls = F.cross_entropy(cls_score, rois_label, ignore_index=0) if cfg.FAST_RCNN.LOSS_TYPE == 'triplet_softmax': cls_score = cls_score / 3 else: if cfg.FAST_RCNN.LOSS_TYPE == 'multi_margin': loss_cls = F.multi_margin_loss(cls_score, rois_label, margin=cfg.FAST_RCNN.MARGIN, reduction='none') elif cfg.FAST_RCNN.LOSS_TYPE == 'max_margin': cls_score_with_high_target = cls_score.clone() cls_score_with_high_target.scatter_(1, rois_label.view(-1, 1), 1e10) # This make sure the following variable always has the target in the first column target_and_offender_index = cls_score_with_high_target.sort(1, True)[1][:, :2] # Target and the largest score excpet target loss_cls = F.multi_margin_loss(cls_score.gather(1, target_and_offender_index), rois_label.data * 0, margin=cfg.FAST_RCNN.MARGIN, reduction='none') loss_cls = loss_cls[rois_label > 0] loss_cls = loss_cls.mean() if loss_cls.numel() > 0 else loss_cls.new_tensor(0) # Secretly log the mean similarity! if cfg.FAST_RCNN.LOSS_TYPE in ['triplet_softmax', 'max_margin', 'multi_margin']: loss_cls.mean_similarity = cls_score[rois_label>0].gather(1, rois_label[rois_label>0].unsqueeze(1)).mean().detach() / 3 bbox_targets = Variable(torch.from_numpy(bbox_targets)).cuda(device_id) bbox_inside_weights = Variable(torch.from_numpy(bbox_inside_weights)).cuda(device_id) bbox_outside_weights = Variable(torch.from_numpy(bbox_outside_weights)).cuda(device_id) loss_bbox = net_utils.smooth_l1_loss( bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights) if cfg.MODEL.TAGGING: loss_bbox = torch.zeros_like(loss_bbox) # class accuracy cls_preds = cls_score.max(dim=1)[1].type_as(rois_label) if not cfg.MODEL.TAGGING: accuracy_cls = cls_preds.eq(rois_label).float().mean(dim=0) else: accuracy_cls = cls_preds[rois_label > 0].eq(rois_label[rois_label > 0]).float().mean(dim=0) # Ignore index 0 return loss_cls, loss_bbox, accuracy_cls
def forward(self, x, target): index = torch.zeros_like(x, dtype=torch.uint8) index.scatter_(1, target.data.view(-1, 1), 1) # output = x - index * self.m_list index_float = index.type(torch.cuda.FloatTensor) batch_m = torch.matmul(self.m_list[None, :], index_float.transpose(0, 1)) batch_m = batch_m.view((-1, 1)) x_m = x - batch_m output = torch.where(index, x_m, x) if self.mode == 'CrossEntropy': return F.cross_entropy(self.s * output, target, weight=self.weight) elif self.mode == 'Hinge': # output 조작 aux = x - index_float * np.inf max_idx = aux.max(1)[1] logit_index = torch.zeros_like(x, dtype=torch.uint8) logit_index.scatter_(1, max_idx.data.view(-1, 1), 1) logit_index_float = logit_index.type(torch.cuda.FloatTensor) all_index = index_float + logit_index_float output = all_index * output - (1 - all_index) * np.inf return F.multi_margin_loss(self.s * output, target, p=1, margin=0, weight=self.weight)
def forward(self, predictions, target): loss = Variable(torch.zeros(1)) target_index_var = Variable(torch.LongTensor([0])) if torch.cuda.is_available(): loss = loss.cuda() target_index_var = target_index_var.cuda() target_sorted, target_indices = torch.sort(target, dim=-1, descending=True) predictions = predictions.gather(1, target_indices) margins = DEFAULT_MARGIN * target_sorted.data # margins = margins.clamp(max=1.0, min=0.5) for sample_index in range(target_indices.size(0)): target_index = 0 while target_index < min(target_indices.size(1), 10) and ( target_sorted[sample_index, target_index].data[0] > 0.25): loss += F.multi_margin_loss(predictions[sample_index, target_index:], target_index_var, margin=margins[sample_index, target_index], size_average=False) target_index += 1 return loss
def forward(self, ents_emb, ents): h1 = F.leaky_relu(self.W1(ents_emb)) h2 = F.leaky_relu(self.W2(h1)) h3 = F.leaky_relu(self.W3(h2)) scores = self.W4(h3) if self.attribute == 'gender' or self.attribute == 'random': A_labels = Variable(torch.Tensor( self.users_sensitive[ents])).cuda() A_labels = A_labels.unsqueeze(1) if self.cross_entropy: fair_penalty = F.binary_cross_entropy_with_logits(scores,\ A_labels) else: probs = torch.sigmoid(scores) fair_penalty = F.l1_loss(probs, A_labels, reduction='elementwise_mean') else: A_labels = Variable(torch.LongTensor( self.users_sensitive[ents])).cuda() if self.cross_entropy: fair_penalty = F.cross_entropy(scores, A_labels) else: probs = torch.softmax(scores, dim=1) fair_penalty = F.multi_margin_loss( probs, A_labels, reduction='elementwise_mean') return fair_penalty
def get_loss(loss_function, output, label, use_gpu): ''' get objective loss of model and backprograte to compute gradients some loss function not impelement ''' if not isinstance(loss_function, str): raise TypeError('loss_function should be str object') label = np.asarray(label) if loss_function == 'binary_cross_entropy': loss = F.binary_cross_entropy(output, label) elif loss_function == 'poisson_nll_loss': loss = F.poisson_nll_loss(output, label) elif loss_function == 'cross_entropy': loss = F.cross_entropy(output, label) elif loss_function == 'hinge_embedding_loss': loss = F.hinge_embedding_loss(output, label) elif loss_function == 'margin_ranking_loss': loss = F.margin_ranking_loss(output, label) elif loss_function == 'multilabel_soft_margin_loss': loss = F.multilabel_soft_margin_loss(output, label) elif loss_function == 'multi_margin_loss': loss = F.multi_margin_loss(output, label) elif loss_function == 'nll_loss': if use_gpu: label = Variable(torch.LongTensor(label).cuda()) label = Variable(torch.LongTensor(label)) loss = F.nll_loss(output, label) elif loss_function == 'binary_cross_entropy_with_logits': loss = F.binary_cross_entropy_with_logits(output, label) return loss
def training_step(self, batch, batch_idx): x, y = batch output = self.forward(x) loss = F.multi_margin_loss(output, y) self.log('Loss', loss) self.log('Accuracy Step', self.accuracy(output, y)) return loss
def forward(self): a = torch.randn(3, 2) b = torch.rand(3, 2) c = torch.rand(3) log_probs = torch.randn(50, 16, 20).log_softmax(2).detach() targets = torch.randint(1, 20, (16, 30), dtype=torch.long) input_lengths = torch.full((16, ), 50, dtype=torch.long) target_lengths = torch.randint(10, 30, (16, ), dtype=torch.long) return len( F.binary_cross_entropy(torch.sigmoid(a), b), F.binary_cross_entropy_with_logits(torch.sigmoid(a), b), F.poisson_nll_loss(a, b), F.cosine_embedding_loss(a, b, c), F.cross_entropy(a, b), F.ctc_loss(log_probs, targets, input_lengths, target_lengths), # F.gaussian_nll_loss(a, b, torch.ones(5, 1)), # ENTER is not supported in mobile module F.hinge_embedding_loss(a, b), F.kl_div(a, b), F.l1_loss(a, b), F.mse_loss(a, b), F.margin_ranking_loss(c, c, c), F.multilabel_margin_loss(self.x, self.y), F.multilabel_soft_margin_loss(self.x, self.y), F.multi_margin_loss(self.x, torch.tensor([3])), F.nll_loss(a, torch.tensor([1, 0, 1])), F.huber_loss(a, b), F.smooth_l1_loss(a, b), F.soft_margin_loss(a, b), F.triplet_margin_loss(a, b, -b), # F.triplet_margin_with_distance_loss(a, b, -b), # can't take variable number of arguments )
def loss(self, scores, true_pos, lamb=1e-7): """ Computes given ranking loss (Equation 7) and adds a regularization term. :return: loss of given batch """ loss = F.multi_margin_loss(scores, true_pos, margin=self.config["margin"]) if self.config["use_local_only"]: return loss # regularization X = F.normalize(self.rel_embs) diff = ((X.view(self.config["n_rels"], 1, -1) - X.view(1, self.config["n_rels"], -1)).pow(2).sum( dim=2).add_(1e-5).sqrt()) diff = diff * (diff < 1).float() loss -= torch.sum(diff).mul(lamb) X = F.normalize(self.ew_embs) diff = ((X.view(self.config["n_rels"], 1, -1) - X.view(1, self.config["n_rels"], -1)).pow(2).sum( dim=2).add_(1e-5).sqrt()) diff = diff * (diff < 1).float() loss -= torch.sum(diff).mul(lamb) return loss
def _compute_label_loss(scores, target): if scores.shape[0] > 0 and scores.shape[1] > 1: # return hinge(scores[torch.arange(scores.shape[0]), target] - scores[:,0]) # needs to handle special case where target is 0 return F.multi_margin_loss(scores, target, margin=self.config["label_margin"]) else: return None
def loss(self, scores, true_pos, method="SL", lamb=1e-7): loss = None if method == "SL": loss = F.multi_margin_loss(scores, true_pos, margin=self.margin) elif method == "RL": loss = self.finish_episode(self.rewards, self.saved_log_probs) return loss
def __call__(self, logits, targets, reduce=True): return F.multi_margin_loss( logits, targets, margin=self.margin, weight=self.weight, reduction="mean" if reduce else "none", )
def forward(self, x, y): embed = x[0] out = x[1] margin_loss = super().forward(embed, y) out_loss = F.multi_margin_loss(out, y) return out_loss + self.lmbda * margin_loss
def loss(self, sample, test=False): inputs = Variable(sample[0], volatile=test) targets = Variable(sample[1], volatile=test) o = torch.squeeze(self.model.forward(inputs)) return F.multi_margin_loss(o, targets, margin=self.margin), { 'logits': o }
def spread_loss(cls, x, labels, m): """ Spread Loss: L_i = \max{(0, m-(a_t - a_i))}^2, L = \sum_{i\neq t} L_i defaut size_average is True """ L = F.multi_margin_loss(x, labels, p=2, margin=m, size_average=cls().size_average) return L
def get_loss(logit, y, args): if args.objective == 'cross_entropy': loss = F.cross_entropy(logit, y) elif args.objective == 'margin': loss = F.multi_margin_loss(logit, y) elif args.objective == 'mse': loss = F.mse_loss(logit, y.float()) else: raise Exception("Objective {} not supported!".format(args.objective)) return loss
def forward(self, input, target): """forward Local Rank Bilinear Loss Original Paper used Hinge Loss For single-label single-classification SVM Choose CrossEntropy For single-lable multi-classification Args: output (Tensor): shape [N, classes] target (Tensor): shape [N] """ if self.type == 'CE': return F.cross_entropy(input, target) else: return F.multi_margin_loss(input, target)
def forward(self, mention_contexts, mention_position, entity_contexts, entities, target=None): """ mention_contexts : batch, m_seq_len mention_position: batch, 2 (start, end) entity_contexts : batch, candidates, e_seq_len entities : batch, candidates target : batch """ mention_seq_len = torch.sum(mention_contexts > 0, dim=-1) # mention_seq_len = mention_seq_len[mention_seq_len > 0] mention_contexts_emb = self.mention_embedding(mention_contexts) # filter 0 rows # select_tensor = mention_seq_len > 0 # mention_seq_len = mention_seq_len[select_tensor] # mention_contexts = mention_contexts[select_tensor] mention_contexts_emb, _ = self.lstm_forward(mention_contexts_emb, mention_seq_len) mention_emb = self.generate_representation(mention_contexts_emb, mention_position) entity_embs = [] cands_num = entity_contexts.size(1) for entity_context, entity in zip(entity_contexts.chunk(dim=1, chunks=cands_num), entities.chunk(dim=1, chunks=cands_num)): entity_context = entity_context.squeeze(1) entity = entity.squeeze(1) entity_seq_len = torch.sum(entity_context > 0, dim=1) # filter 0 rows # select_tensor = entity_seq_len > 0 # entity_seq_len = entity_seq_len[select_tensor] # entity_context = entity_context[select_tensor] # entity = entity[select_tensor] entity_contexts_emb = self.entity_embedding(entity_context) # batch, e_seq_len, emb_dim entity_emb = self.embedding(entity) # batch, emb_dim entity_contexts_emb, _ = self.lstm_forward(entity_contexts_emb, entity_seq_len) entity_contexts_emb = F.relu(self.reduce_w(entity_contexts_emb)) # batch, e_seq_len, emb_dim # add attention entity_emb = entity_emb.unsqueeze(1).expand_as(entity_contexts_emb) # torch.sum( * entity_contexts_emb, dim=-1) attention = self.v( torch.tanh(self.w_c(entity_contexts_emb) + self.w_e(entity_emb) + self.b)) # batch, e_seq_len, 1 attention = F.softmax(attention, dim=1) entity_contexts_emb = torch.sum(attention * entity_contexts_emb, dim=1) # batch, emb_dim entity_embs.append(entity_contexts_emb) entity_embs = torch.stack(entity_embs, dim=1) # batch, cands, emb_dim mention_emb = F.relu(self.reduce_m(mention_emb)) # batch, emb_dim scores = torch.sum(mention_emb.unsqueeze(1) * entity_embs, dim=-1) # batch, cands scores = F.softmax(scores, dim=-1) if target is not None: loss = F.multi_margin_loss(scores, target, margin=0.5) return scores, loss else: return scores
def loss(self, scores, true_pos, lamb=1e-7): loss = F.multi_margin_loss(scores, true_pos, margin=self.margin) if self.use_local_only: return loss # regularization X = F.normalize(self.rel_embs) diff = (X.view(self.n_rels, 1, -1) - X.view(1, self.n_rels, -1)).pow(2).sum(dim=2).add_(1e-5).sqrt() diff = diff * (diff < 1).float() loss -= torch.sum(diff).mul(lamb) X = F.normalize(self.ew_embs) diff = (X.view(self.n_rels, 1, -1) - X.view(1, self.n_rels, -1)).pow(2).sum(dim=2).add_(1e-5).sqrt() diff = diff * (diff < 1).float() loss -= torch.sum(diff).mul(lamb) return loss
def train(args, model, device, train_loader, optimizer, epoch, penalty='cross_entropy'): model.train() for batch_idx, (data, target) in enumerate(train_loader): data, target = data.to(device), target.to(device) optimizer.zero_grad() output = model(data) if penalty == 'cross_entropy': loss = F.cross_entropy(output, target, reduction='mean') elif penalty == 'multi_margin': loss = F.multi_margin_loss(output, target, p=1, reduction='mean') loss.backward() optimizer.step() if batch_idx % args.log_interval == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item()))
def inner_update(self, x, fast_weights, y, bn_training): logits = self.net(x, fast_weights, bn_training=bn_training) loss = F.multi_margin_loss(logits, y) if fast_weights is None: fast_weights = self.net.parameters() # grad = torch.autograd.grad(loss, fast_weights) fast_weights = list( map(lambda p: p[1] - self.update_lr * p[0] if p[1].learn else p[1], zip(grad, fast_weights))) for params_old, params_new in zip(self.net.parameters(), fast_weights): params_new.learn = params_old.learn return fast_weights
def on_batch_end(self, session, lossMeter, output, label): label = Variable(util.to_gpu(label)) batch_accuracy = self.train_accuracy_meter.update(output, label) self.batch_train_accuracies.append(batch_accuracy) self.batch_train_losses.append(lossMeter.loss.data.cpu().item()) self.train_raw_loss_meter.update( F.multi_margin_loss(output[-1][0], label).data.cpu(), label.shape[0]) for layer, loss_meter in zip(output[:-1], self.train_embedding_loss_meters): if layer[1] in self.select: loss_meter.update( batch_all_triplet_loss(layer[0].view(layer[0].size(0), -1), label, 1).data.cpu().item()) self.num_batches += 1
def run(self, session, lossMeter=None): self.val_accuracy_meter.reset() val_loss = LossMeter() val_raw_loss = LossMeter() embedding_losses = [LossMeter() for x in range(len(self.select) - 1)] with EvalModel(session.model): for input, label, *_ in tqdm(self.val_data, desc="Validating", leave=False): label = Variable(util.to_gpu(label)) output = session.forward(input) step_loss = session.criterion(output, label).data.cpu() val_loss.update(step_loss, input.shape[0]) val_raw_loss.update( F.multi_margin_loss(output[-1][0], label).data.cpu(), input.shape[0]) self.val_accuracy_meter.update(output, label) for idx, (layer, embedding_loss) in enumerate( zip(output[:-1], embedding_losses)): if layer[1] in self.select: self.names[idx] = layer[1] embedding_loss.update( batch_all_triplet_loss( layer[0].view(layer[0].size(0), -1), label, 1).data.cpu()) self.val_losses.append(val_loss.raw_avg.item()) self.val_raw_losses.append(val_raw_loss.raw_avg.item()) accuracy = self.val_accuracy_meter.accuracy() if self.model_file != None and accuracy > self.best_accuracy: session.save(self.model_file) self.best_accuracy = accuracy self.val_accuracies.append(accuracy) for meter, loss in zip(embedding_losses, self.val_embedding_losses): loss.append(meter.raw_avg)
def __init__(self, n_way, active_losses): super().__init__() self.n_way = n_way self.active_losses = [0 for _ in range(4)] for idx in active_losses: self.active_losses[idx] = 1 self.n_losses = npsum(self.active_losses) self.losses = [ lambda x, y: F.multi_margin_loss(x, y, margin=0.9, p=2), F.mse_loss, # one-hot, softmax, double F.cross_entropy, F.multilabel_margin_loss # one-hot, softmax, long ] self.onehot = [False, True, False, True] self.softmax = [False, True, False, True] # Weights are inputs to softplus self.weights = np.ones(len(self.losses)) * invsp(1. / self.n_losses)
def loss(self, scores, true_pos, method="SL", lamb=1e-7): loss = None # print("----MulRelRanker Loss----") if method == "SL": loss = F.multi_margin_loss(scores, true_pos, margin=self.margin) elif method == "RL": loss = self.finish_episode(self.rewards, self.saved_log_probs) # if self.use_local_only: # return loss # regularization # X = F.normalize(self.rel_embs) # diff = (X.view(self.n_rels, 1, -1) - X.view(1, self.n_rels, -1)).pow(2).sum(dim=2).add_(1e-5).sqrt() # diff = diff * (diff < 1).float() # loss -= torch.sum(diff).mul(lamb) # # X = F.normalize(self.ew_embs) # diff = (X.view(self.n_rels, 1, -1) - X.view(1, self.n_rels, -1)).pow(2).sum(dim=2).add_(1e-5).sqrt() # diff = diff * (diff < 1).float() # loss -= torch.sum(diff).mul(lamb) return loss
def test(args, model, device, test_loader, penalty='cross_entropy'): model.eval() test_loss = 0 correct_top1 = 0 correct_top5 = 0 with torch.no_grad(): for data, target in test_loader: data, target = data.to(device), target.to(device) output = model(data) if penalty == 'cross_entropy': test_loss += F.cross_entropy(output, target, reduction='sum').item() elif penalty == 'multi_margin': test_loss += F.multi_margin_loss(output, target, p=1, reduction='sum').item() # sum up batch loss correct += count_correct_preds(output, target, topk=(1, 5)) correct_top1 += correct[0] correct_top5 += correct[1] test_loss /= len(test_loader.dataset) print('Average loss: {:.4f}, Accuracy top-1\%: {}/{} ({:.1f}%), Accuracy top-5\%: {}/{} ({:.1f}%)'.format( test_loss, correct_top1, len(test_loader.dataset), 100. * correct_top1 / len(test_loader.dataset), correct_top5, len(test_loader.dataset), 100. * correct_top5 / len(test_loader.dataset)))
def sequence_forward(self, net_output, model, sample): """Compute the sequence-level loss for the given hypotheses. Returns a tuple with three elements: 1) the loss, as a Variable 2) the sample size, which is used as the denominator for the gradient 3) logging outputs to display while training """ scores = self.get_hypothesis_scores(net_output, sample) lengths = self.get_hypothesis_lengths(net_output, sample) avg_scores = scores.sum(2) / lengths bleu = sample['norm_bleu'].type_as(avg_scores) avg_scores = avg_scores - bleu scores_with_high_target = avg_scores.clone() scores_with_high_target.scatter_(1, bleu.max(1)[1].view(-1, 1), 1e10) target_and_offender_index = scores_with_high_target.sort(1, True)[1][:, 0:2] avg_scores = avg_scores.gather(1, target_and_offender_index) target_index = sample['target_hypo_idx'].data.fill_(0) # Multi max margin loss makes sure that x_i <= x_t # While we want to make sure that score_i - bleu_i <= score_t - bleu_t loss = F.multi_margin_loss(avg_scores, sample['target_hypo_idx'], size_average=False, margin=0) sample_size = net_output.size(0) # bsz logging_output = { 'loss': loss.data[0], 'sample_size': sample_size, } return loss, sample_size, logging_output
def configure_criterion(self, y, t): criterion = F.cross_entropy(y, t) if self.hparams.criterion == "cross_entropy": criterion = F.cross_entropy(y, t) elif self.hparams.criterion == "binary_cross_entropy": criterion = F.binary_cross_entropy(y, t) elif self.hparams.criterion == "binary_cross_entropy_with_logits": criterion = F.binary_cross_entropy_with_logits(y, t) elif self.hparams.criterion == "poisson_nll_loss": criterion = F.poisson_nll_loss(y, t) elif self.hparams.criterion == "hinge_embedding_loss": criterion = F.hinge_embedding_loss(y, t) elif self.hparams.criterion == "kl_div": criterion = F.kl_div(y, t) elif self.hparams.criterion == "l1_loss": criterion = F.l1_loss(y, t) elif self.hparams.criterion == "mse_loss": criterion = F.mse_loss(y, t) elif self.hparams.criterion == "margin_ranking_loss": criterion = F.margin_ranking_loss(y, t) elif self.hparams.criterion == "multilabel_margin_loss": criterion = F.multilabel_margin_loss(y, t) elif self.hparams.criterion == "multilabel_soft_margin_loss": criterion = F.multilabel_soft_margin_loss(y, t) elif self.hparams.criterion == "multi_margin_loss": criterion = F.multi_margin_loss(y, t) elif self.hparams.criterion == "nll_loss": criterion = F.nll_loss(y, t) elif self.hparams.criterion == "smooth_l1_loss": criterion = F.smooth_l1_loss(y, t) elif self.hparams.criterion == "soft_margin_loss": criterion = F.soft_margin_loss(y, t) return criterion
def multi_margin(y_pred, y_true): return F.multi_margin_loss(y_pred, y_true)