PyTorch自定义损失功能

时间:2018-12-30 17:52:12

标签: python deep-learning pytorch

应如何实现自定义损失功能?使用以下代码会导致错误:

import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import torch.utils.data as data_utils
import torch.nn as nn
import torch.nn.functional as F

num_epochs = 20

x1 = np.array([0,0])
x2 = np.array([0,1])
x3 = np.array([1,0])
x4 = np.array([1,1])

num_epochs = 200

class cus2(torch.nn.Module):

    def __init__(self):
        super(cus2,self).__init__()

    def forward(self, outputs, labels):
        # reshape labels to give a flat vector of length batch_size*seq_len
        labels = labels.view(-1)  

        # mask out 'PAD' tokens
        mask = (labels >= 0).float()

        # the number of tokens is the sum of elements in mask
        num_tokens = int(torch.sum(mask).data[0])

        # pick the values corresponding to labels and multiply by mask
        outputs = outputs[range(outputs.shape[0]), labels]*mask

        # cross entropy loss for all non 'PAD' tokens
        return -torch.sum(outputs)/num_tokens


x = torch.tensor([x1,x2,x3,x4]).float()

y = torch.tensor([0,1,1,0]).long()

train = data_utils.TensorDataset(x,y)
train_loader = data_utils.DataLoader(train , batch_size=2 , shuffle=True)

device = 'cpu'

input_size = 2
hidden_size = 100 
num_classes = 2

learning_rate = .0001

class NeuralNet(nn.Module) : 
    def __init__(self, input_size, hidden_size, num_classes) : 
        super(NeuralNet, self).__init__()
        self.fc1 = nn.Linear(input_size , hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size , num_classes)

    def forward(self, x) : 
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

for i in range(0 , 1) :

        model = NeuralNet(input_size, hidden_size, num_classes).to(device)

        criterion = nn.CrossEntropyLoss()
#         criterion = Regress_Loss()
#         criterion = cus2()
        optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

        total_step = len(train_loader)
        for epoch in range(num_epochs) : 
            for i,(images , labels) in enumerate(train_loader) : 
                images = images.reshape(-1 , 2).to(device)
                labels = labels.to(device)

                outputs = model(images)
                loss = criterion(outputs , labels)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
#                 print(loss)

        outputs = model(x)

        print(outputs.data.max(1)[1])

对训练数据做出完美的预测:

tensor([0, 1, 1, 0])

使用https://cs230-stanford.github.io/pytorch-nlp.html#writing-a-custom-loss-function中的自定义损失函数:

enter image description here

在上面的代码中以cus2

的形式实现

使用此损失函数的未注释代码# criterion = cus2()返回:

tensor([0, 0, 0, 0])

还会返回警告:

  

UserWarning:0维张量的无效索引。这将是一个错误   PyTorch 0.5。使用tensor.item()将0维张量转换为Python   数字

我没有正确实现自定义损失功能?

3 个答案:

答案 0 :(得分:2)

如果您使用割炬功能,就可以了

import torch 

def my_custom_loss(output, target):
    loss = torch.mean((output-target*2)**3)
    return loss

# Forward pass to the Network
# then, 
loss.backward()

答案 1 :(得分:2)

解决方案

以下是一些我在this Kaggle Notebook中遇到的自定义损失函数的示例。它在PyTorchTensorFlow中提供了以下自定义损失函数的实现。

Loss Function Reference for Keras & PyTorch

我希望这对希望了解如何制作自己的自定义损失函数的人有所帮助。

答案 2 :(得分:1)

您的损失函数在编程上是正确的,但以下情况除外:

    # the number of tokens is the sum of elements in mask
    num_tokens = int(torch.sum(mask).data[0])

当您执行torch.sum时,它返回0维张量,因此警告无法对其进行索引。要解决此问题,请按照建议int(torch.sum(mask).item())int(torch.sum(mask))也可以使用。

现在,您是否要使用自定义损失来模拟CE损失?如果是,则您缺少log_softmax

要修复在语句4之前添加outputs = torch.nn.functional.log_softmax(outputs, dim=1)的问题。请注意,如果您已附加教程,则log_softmax已在前向调用中完成。你也可以那样做。

此外,我注意到学习速度很慢,即使出现CE丢失,结果也不一致。在习俗和CE丢失的情况下,将学习率提高到1e-3对我来说效果很好。