当前位置: 首页>>代码示例>>Python>>正文


Python functional.nll_loss函数代码示例

本文整理汇总了Python中torch.nn.functional.nll_loss函数的典型用法代码示例。如果您正苦于以下问题:Python nll_loss函数的具体用法?Python nll_loss怎么用?Python nll_loss使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了nll_loss函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: forward

    def forward(self, y_pred, y_true):
        torch.nn.modules.loss._assert_no_grad(y_true)

        y_pred_log = torch.log(y_pred)
        start_loss = F.nll_loss(y_pred_log[:, 0, :], y_true[:, 0])
        end_loss = F.nll_loss(y_pred_log[:, 1, :], y_true[:, 1])
        return start_loss + end_loss
开发者ID:SerenaKhoo,项目名称:Match-LSTM,代码行数:7,代码来源:loss.py

示例2: train

def train(epoch):
    t = time.time()
    model.train()
    optimizer.zero_grad()
    output = model(features, adj)
    loss_train = F.nll_loss(output[idx_train], labels[idx_train])
    acc_train = accuracy(output[idx_train], labels[idx_train])
    loss_train.backward()
    optimizer.step()

    if not args.fastmode:
        # Evaluate validation set performance separately,
        # deactivates dropout during validation run.
        model.eval()
        output = model(features, adj)

    loss_val = F.nll_loss(output[idx_val], labels[idx_val])
    acc_val = accuracy(output[idx_val], labels[idx_val])
    print('Epoch: {:04d}'.format(epoch+1),
          'loss_train: {:.4f}'.format(loss_train.data[0]),
          'acc_train: {:.4f}'.format(acc_train.data[0]),
          'loss_val: {:.4f}'.format(loss_val.data[0]),
          'acc_val: {:.4f}'.format(acc_val.data[0]),
          'time: {:.4f}s'.format(time.time() - t))

    return loss_val.data[0]
开发者ID:aimeng100,项目名称:pyGAT,代码行数:26,代码来源:train.py

示例3: train

def train(epoch, model):
    #最后的全连接层学习率为前面的10倍
    LEARNING_RATE = lr / math.pow((1 + 10 * (epoch - 1) / epochs), 0.75)
    print("learning rate:", LEARNING_RATE)
    optimizer_fea = torch.optim.SGD([
        {'params': model.sharedNet.parameters()},
        {'params': model.cls_fc.parameters(), 'lr': LEARNING_RATE},
    ], lr=LEARNING_RATE / 10, momentum=momentum, weight_decay=l2_decay)
    optimizer_critic = torch.optim.SGD([
        {'params': model.domain_fc.parameters(), 'lr': LEARNING_RATE}
    ], lr=LEARNING_RATE, momentum=momentum, weight_decay=l2_decay)

    data_source_iter = iter(source_loader)
    data_target_iter = iter(target_train_loader)
    dlabel_src = Variable(torch.ones(batch_size).long().cuda())
    dlabel_tgt = Variable(torch.zeros(batch_size).long().cuda())
    i = 1
    while i <= len_source_loader:
        model.train()

        source_data, source_label = data_source_iter.next()
        if cuda:
            source_data, source_label = source_data.cuda(), source_label.cuda()
        source_data, source_label = Variable(source_data), Variable(source_label)
        clabel_src, dlabel_pred_src = model(source_data)
        label_loss = F.nll_loss(F.log_softmax(clabel_src, dim=1), source_label)
        critic_loss_src = F.nll_loss(F.log_softmax(dlabel_pred_src, dim=1), dlabel_src)
        confusion_loss_src = 0.5 * ( F.nll_loss(F.log_softmax(dlabel_pred_src, dim=1), dlabel_src) + F.nll_loss(F.log_softmax(dlabel_pred_src, dim=1), dlabel_tgt) )

        target_data, target_label = data_target_iter.next()
        if i % len_target_loader == 0:
            data_target_iter = iter(target_train_loader)
        if cuda:
            target_data, target_label = target_data.cuda(), target_label.cuda()
        target_data = Variable(target_data)
        clabel_tgt, dlabel_pred_tgt = model(target_data)
        critic_loss_tgt = F.nll_loss(F.log_softmax(dlabel_pred_tgt, dim=1), dlabel_tgt)
        confusion_loss_tgt = 0.5 * (F.nll_loss(F.log_softmax(dlabel_pred_tgt, dim=1), dlabel_src) + F.nll_loss(
            F.log_softmax(dlabel_pred_tgt, dim=1), dlabel_tgt))

        confusion_loss_total = (confusion_loss_src + confusion_loss_tgt) / 2
        fea_loss_total = confusion_loss_total + label_loss
        critic_loss_total = (critic_loss_src + critic_loss_tgt) / 2

        optimizer_fea.zero_grad()
        fea_loss_total.backward(retain_graph=True)
        optimizer_fea.step()
        optimizer_fea.zero_grad()
        optimizer_critic.zero_grad()
        critic_loss_total.backward()
        optimizer_critic.step()

        if i % log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tconfusion_Loss: {:.6f}\tlabel_Loss: {:.6f}\tdomain_Loss: {:.6f}'.format(
                epoch, i * len(source_data),len_source_dataset,
                100. * i / len_source_loader, confusion_loss_total.data[0], label_loss.data[0], critic_loss_total.data[0]))
        i = i + 1
开发者ID:Silflame,项目名称:transferlearning,代码行数:57,代码来源:RevGrad.py

示例4: get_loss

    def get_loss(cls, start_log_probs, end_log_probs, starts, ends):
        """
        Get the loss, $-\log P(s|p,q)P(e|p,q)$.
        The start and end labels are expected to be in span format,
        so that text[start:end] is the answer.
        """

        # Subtracts 1 from the end points, to get the exact indices, not 1
        # after the end.
        loss = nll_loss(start_log_probs, starts) +\
            nll_loss(end_log_probs, ends-1)
        return loss
开发者ID:zhouyonglong,项目名称:MSMARCOV2,代码行数:12,代码来源:bidaf.py

示例5: train

def train(args, epoch, net, trainLoader, optimizer, trainF):
    net.train()
    nProcessed = 0
    nTrain = len(trainLoader.dataset)
    for batch_idx, (data, target) in enumerate(trainLoader):
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        optimizer.zero_grad()
        output = net(data)
        loss = F.nll_loss(output, target)
        # make_graph.save('/tmp/t.dot', loss.creator); assert(False)
        loss.backward()
        optimizer.step()
        nProcessed += len(data)
        pred = output.data.max(1)[1] # get the index of the max log-probability
        incorrect = pred.ne(target.data).cpu().sum()
        err = 100.*incorrect/len(data)
        partialEpoch = epoch + batch_idx / len(trainLoader) - 1
        print('Train Epoch: {:.2f} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tError: {:.6f}'.format(
            partialEpoch, nProcessed, nTrain, 100. * batch_idx / len(trainLoader),
            loss.data[0], err))

        trainF.write('{},{},{}\n'.format(partialEpoch, loss.data[0], err))
        trainF.flush()
开发者ID:shubhampachori12110095,项目名称:densenet.pytorch,代码行数:25,代码来源:train.py

示例6: _test_pytorch

  def _test_pytorch(self, model):
    """
    Test pre-trained pytorch model using MNIST Dataset
    :param model: Pre-trained PytorchMNIST model
    :return: tuple(loss, accuracy)
    """
    data_loader = torch.utils.data.DataLoader(
      datasets.MNIST(self.dataDir, train=False, download=True,
                     transform=transforms.Compose([
                       transforms.ToTensor(),
                       transforms.Normalize((0.1307,), (0.3081,))])),
      batch_size=BATCH_SIZE, shuffle=True)

    model.eval()
    loss = 0.0
    num_correct = 0.0
    with torch.no_grad():
      for data, target in data_loader:
        data = data.view(-1, 28 * 28)
        output = model(data)
        loss += F.nll_loss(output, target, reduction='sum').item()  # sum up batch loss
        pred = output.max(1, keepdim=True)[1]  # get the index of the max log-probability
        num_correct += pred.eq(target.view_as(pred)).sum().item()

    loss /= len(data_loader.dataset)
    accuracy = num_correct / len(data_loader.dataset)

    return (loss, accuracy)
开发者ID:rhyolight,项目名称:nupic.research,代码行数:28,代码来源:import_export_test.py

示例7: test

def test(model, device, test_loader, epoch):
    losses = AverageMeter()
    top1 = AverageMeter()

    # switch to evaluate mode
    model.eval()

    for batch_idx, (data, target) in enumerate(test_loader):
        data, target = data.to(device), target.to(device)

        # compute output
        with torch.no_grad():
            output = model(data)
        loss = F.nll_loss(output, target)

        # measure accuracy and record loss
        prec1 = accuracy(output, target, topk=(1,))[0]
        losses.update(loss.item(), data.size(0))
        top1.update(prec1.item(), data.size(0))

        if batch_idx % args.print_freq == 0:
            print('Test: [{0}/{1}]\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                  '[email protected] {top1.val:.3f} ({top1.avg:.3f})'.format(
                      batch_idx, len(test_loader), loss=losses,
                      top1=top1))

    print(' * [email protected] {top1.avg:.3f}'.format(top1=top1))
    return top1.avg
开发者ID:kevinzakka,项目名称:blog-code,代码行数:29,代码来源:cifar.py

示例8: test

def test(epoch, best_acc):
    slope = get_slope(epoch)

    model.eval()
    test_loss = 0.0
    correct = 0.0
    for data, target in test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model((data, slope))
        test_loss += F.nll_loss(output, target, size_average=False).data[0] # sum up batch loss
        pred = output.data.max(1, keepdim=True)[1] # get the index of the max log-probability
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()

    test_loss /= len(test_loader.dataset)
    test_acc = correct / len(test_loader.dataset)
    print 'Test set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
          test_loss, int(correct), len(test_loader.dataset),
          100. * test_acc)

    if test_acc >= best_acc:
        torch.save(model.state_dict(), os.path.join('models','{}.pth'.format(model_name)))

    return test_loss, test_acc
开发者ID:codealphago,项目名称:binary-stochastic-neurons,代码行数:25,代码来源:main.py

示例9: train

def train(epoch, model):
    LEARNING_RATE = lr / math.pow((1 + 10 * (epoch - 1) / epochs), 0.75)
    print('learning rate{: .4f}'.format(LEARNING_RATE) )
    optimizer = torch.optim.SGD([
        {'params': model.sharedNet.parameters()},
        {'params': model.cls_fc.parameters(), 'lr': LEARNING_RATE},
        ], lr=LEARNING_RATE / 10, momentum=momentum, weight_decay=l2_decay)

    model.train()

    iter_source = iter(source_loader)
    iter_target = iter(target_train_loader)
    num_iter = len_source_loader
    for i in range(1, num_iter):
        data_source, label_source = iter_source.next()
        data_target, _ = iter_target.next()
        if i % len_target_loader == 0:
            iter_target = iter(target_train_loader)
        if cuda:
            data_source, label_source = data_source.cuda(), label_source.cuda()
            data_target = data_target.cuda()
        data_source, label_source = Variable(data_source), Variable(label_source)
        data_target = Variable(data_target)

        optimizer.zero_grad()
        label_source_pred, loss_mmd = model(data_source, data_target)
        loss_cls = F.nll_loss(F.log_softmax(label_source_pred, dim=1), label_source)
        gamma = 2 / (1 + math.exp(-10 * (epoch) / epochs)) - 1
        loss = loss_cls + gamma * loss_mmd
        loss.backward()
        optimizer.step()
        if i % log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tsoft_Loss: {:.6f}\tmmd_Loss: {:.6f}'.format(
                epoch, i * len(data_source), len_source_dataset,
                100. * i / len_source_loader, loss.data[0], loss_cls.data[0], loss_mmd.data[0]))
开发者ID:Silflame,项目名称:transferlearning,代码行数:35,代码来源:DAN.py

示例10: train

  def train(self, epoch):
    """
    Train one epoch of this model by iterating through mini batches. An epoch
    ends after one pass through the training set, or if the number of mini
    batches exceeds the parameter "batches_in_epoch".
    """

    self.logger.info("epoch: %s", epoch)

    t0 = time.time()
    self.preEpoch()

    self.logger.info("Learning rate: %s",
                     self.learningRate if self.lr_scheduler is None
                     else self.lr_scheduler.get_lr())

    self.model.train()
    for batch_idx, (batch, target) in enumerate(self.train_loader):
      data = batch["input"]
      if self.model_type in ["resnet9", "cnn"]:
        data = torch.unsqueeze(data, 1)
      data, target = data.to(self.device), target.to(self.device)
      self.optimizer.zero_grad()
      output = self.model(data)
      loss = F.nll_loss(output, target)
      loss.backward()
      self.optimizer.step()

      if batch_idx >= self.batches_in_epoch:
        break

    self.postEpoch()

    self.logger.info("training duration: %s", time.time() - t0)
开发者ID:numenta,项目名称:nupic.research,代码行数:34,代码来源:sparse_speech_experiment.py

示例11: train

def train(epoch):

    slope = get_slope(epoch)

    print '# Epoch : {} - Slope : {}'.format(epoch, slope)

    model.train()
    train_loss = 0
    for batch_idx, (data, target) in enumerate(train_loader):
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)
        optimizer.zero_grad()
        output = model((data, slope))
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()

        train_loss += loss.data

    train_loss /= len(train_loader)
    train_loss = train_loss[0]

    print 'Training Loss : {}'.format(train_loss)

    return train_loss
开发者ID:codealphago,项目名称:binary-stochastic-neurons,代码行数:26,代码来源:main.py

示例12: train

def train(model, device, train_loader, optimizer, epoch):
    """Train for one epoch on the training set"""
    losses = AverageMeter()
    top1 = AverageMeter()

    # switch to train mode
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)

        # compute output
        output = model(data)
        loss = F.nll_loss(output, target)

        # measure accuracy and record loss
        prec1 = accuracy(output, target, topk=(1,))[0]
        losses.update(loss.item(), data.size(0))
        top1.update(prec1.item(), data.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if batch_idx % args.print_freq == 0:
            print('Epoch: [{0}][{1}/{2}]\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                  '[email protected] {top1.val:.3f} ({top1.avg:.3f})'.format(
                      epoch, batch_idx, len(train_loader), loss=losses, top1=top1))
开发者ID:kevinzakka,项目名称:blog-code,代码行数:29,代码来源:cifar_reg.py

示例13: evaluate

def evaluate():
    should_stop = False
    model.eval()

    for name, loader in [('train', train_loader), ('test', test_loader)]:
        loss = 0
        correct = 0
        for data, target in loader:
            if args.cuda:
                data, target = data.cuda(), target.cuda()
            if isinstance(model, MLP):
                data = data.view(-1, 784)
            data, target = Variable(data, volatile=True), Variable(target)
            output = model(data)
            loss += F.nll_loss(output, target, size_average=False).data[0]
            # get the index of the max log-probability
            pred = output.data.max(1, keepdim=True)[1]
            correct += pred.eq(target.data.view_as(pred)).cpu().sum()

        loss /= len(loader.dataset)
        print('{} -- Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)'
              .format(name.ljust(5), loss, correct, len(loader.dataset),
                      100. * correct / len(loader.dataset)))
        if name == 'test':
            scheduler.step(loss)
            should_stop = should_stop or correct == len(loader.dataset)
    return should_stop or optimizer.param_groups[0]['lr'] < args.lr / 1e2
开发者ID:ogrisel,项目名称:notebooks,代码行数:27,代码来源:run_mnist.py

示例14: m_testxxx

def m_testxxx(epoch):
    # checkpoint = torch.load('checkpoint-1.pth.tar')
    # model.load_state_dict(checkpoint['state_dict'])
    # optimizer.load_state_dict(checkpoint['optimizer'])
    #
    model.eval()
    test_loss = 0
    correct = 0
    for data, target in test_loader:
        # x_data = data[0].numpy()
        # x_data = np.reshape(x_data, [28, 28])
        # np.savetxt('./data.csv', x_data)
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model(data)
        test_loss += F.nll_loss(output, target).data[0]
        pred = output.data.max(1)[1]  # get the index of the max log-probability
        #result = pred.numpy()
        #np.reshape(result, [-1, 1])
        #print(result.shape)
        # print(pred)
        correct += pred.eq(target.data).cpu().sum()

    test_loss = test_loss
    test_loss /= len(test_loader)  # loss function already averages over batch size
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))
开发者ID:BossKwei,项目名称:temp,代码行数:29,代码来源:buf_fucker_train_2.py

示例15: test

  def test(self, test_loader=None):
    """
    Test the model using the given loader and return test metrics
    """
    if test_loader is None:
      test_loader = self.test_loader

    self.model.eval()
    test_loss = 0
    correct = 0

    with torch.no_grad():
      for batch, target in test_loader:
        data = batch["input"]
        if self.model_type in ["resnet9", "cnn"]:
          data = torch.unsqueeze(data, 1)
        data, target = data.to(self.device), target.to(self.device)
        output = self.model(data)
        test_loss += F.nll_loss(output, target, reduction='sum').item()
        pred = output.max(1, keepdim=True)[1]
        correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.sampler)
    test_error = 100. * correct / len(test_loader.sampler)

    entropy = self.entropy()
    ret = {
      "total_correct": correct,
      "mean_loss": test_loss,
      "mean_accuracy": test_error,
      "entropy": float(entropy)}

    return ret
开发者ID:numenta,项目名称:nupic.research,代码行数:33,代码来源:sparse_speech_experiment.py


注:本文中的torch.nn.functional.nll_loss函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。