pytorch Crossentropy error in simple example of NN
H1, I am try to make NN model that satisfy simple formula.
y = X1^2 + X2^2 

But when i use CrossEntropyLoss for loss function, i get two different error message. 
First, when i set code like this

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()


i get this message

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


assNLLCriterion.c:93

Second, I change code like this

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()


then i get message like

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


How can i solve this problem? Thanks.(sorry for my English)
This is my code

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 &lt; 0.001:
        break


Can i make those dataset using dataloader in pytorch? Thanks for your help.

You are using the wrong loss function. CrossEntropyLoss is used for classification problems generally wheread your problem is that of regression. So you should use losses which are meant for regression like tasks like Mean Squared Error Loss, L1 Loss etc. Take a look at this, this, this and this.
