pytorch NN的简单示例中的交叉熵误差

时间:2018-12-01 14:09:57

标签: python pytorch loss-function

H1,我试图制作满足简单公式的NN模型。
y = X1 ^ 2 + X2 ^ 2

但是当我使用CrossEntropyLoss作为损失函数时,我得到两个不同的错误消息。
首先,当我设置这样的代码

x = torch.randn(batch_size, 2)
y_hat = model(x)
y = answer(x).long()

optimizer.zero_grad()
loss = loss_func(y_hat, y)
loss.backward()
optimizer.step()

我收到此消息

RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed.  at 
c:\programdata\miniconda3\conda-bld\pytorch_1533090623466\work\aten\src\thnn\generic/Cl 

assNLLCriterion.c:93

第二,我更改这样的代码

x = torch.randn(batch_size, 2)
y_hat = model(x)
y = answer(x).long().view(batch_size,1,1)

optimizer.zero_grad()
loss = loss_func(y_hat, y)
loss.backward()
optimizer.step()

然后我收到类似

的消息
RuntimeError: multi-target not supported at c:\programdata\miniconda3\conda-bld\pytorch_1533090623466\work\aten\src\thnn\generic/ClassNLLCriterion.c:21

我该如何解决这个问题?谢谢。(对不起,我的英语)
这是我的代码

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F

def answer(x):

    y = x[:,0].pow(2) + x[:,1].pow(2)

    return y

class Model(nn.Module):

    def __init__(self, input_size, output_size):
        super(Model, self).__init__()

        self.linear1 = nn.Linear(input_size, 10)
        self.linear2 = nn.Linear(10, 1)

    def forward(self, x):

        y = F.relu(self.linear1(x))
        y = F.relu(self.linear2(y))

        return y

model = Model(2,1)
print(model, '\n')

loss_func = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr = 0.001)

batch_size = 3
epoch_n = 100
iter_n = 100

for epoch in range(epoch_n):
    loss_avg = 0

    for i in range(iter_n):

        x = torch.randn(batch_size, 2)
        y_hat = model(x)
        y = answer(x).long().view(batch_size,1,1)

        optimizer.zero_grad()
        loss = loss_func(y_hat, y)
        loss.backward()
        optimizer.step()

        loss_avg += loss

    loss_avg = loss_avg / iter_n

    if epoch % 10 == 0:
        print(loss_avg)

    if loss_avg < 0.001:
        break

我可以使用pytorch中的dataloader制作那些数据集吗?感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您使用了错误的丢失功能。 CrossEntropyLoss通常用于分类问题,而您的问题是回归问题。因此,您应该使用诸如Mean Squared Error LossL1 Loss等任务之类的用于回归的损耗。看看thisthisthis和{{ 3}}。