pytorch mnist分类实验中调用其他loss function

在mnist的分类实验中,默认target为数字类别,torch.nn.functional.nll_loss(output, target)可以直接计算,但是其他损失函数并不可以直接计算.
我们找到mnist简易分类网络中对应的代码模块

def train(model, device, train_loader, optimizer, epoch):
    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)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()#根据梯度更新网络参数
        if(batch_idx+1)%30 == 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()))#Use torch.Tensor.item() to get a Python number from a tensor containing a single value:

假如我们想用其他loss function 如F.l1_loss,这里就需要我们对target进行处理,因为output是一个512维度的向量,而target则是一个标量,我们需要把target转换为one-hot representation

        target = target.view(BATCH_SIZE,1)
        target2=torch.zeros(BATCH_SIZE, 10).to(device)
        target2=target2.scatter_(1, target, 1)
        loss = F.l1_loss(output, target2)

这里可以顺利调用到epoch的最后一个iter,会出现问题,因为在epoch最后一个iter中长度是不足batch_size,做一个简单修正即可

        target = target.view(target.size()[0],1)
        target2=torch.zeros(target.size()[0], 10).to(device)
        target2=target2.scatter_(1, target, 1)
        loss = F.l1_loss(output, target2)

你可能感兴趣的:(pytorch mnist分类实验中调用其他loss function)