Pytorch is Throwing out of bounds Error? Expects a scalar
This is the code below, Not sure what the error being thrown is. Please can someone explain what is wrong and the fix. I'm new to pytorch and decided to try learn it using the house prices data set but ran into this error. 

It is apprently something to do with scalar value or somethings but not sure that the problem is because the y value given is a scalar not a vector. 

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader
import torch.optim as optim
import pandas as pd
import numpy as np
df = pd.read_csv('housepricedata.csv')
dataset = df.values
X = dataset[:,0:10]
y = dataset[:, 10]
from sklearn import preprocessing
min_max = preprocessing.MinMaxScaler()
x_scale = min_max.fit_transform(X)
y_scale = min_max.fit_transform(y_scale)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x_scale, y, test_size=0.3)

X_train = torch.FloatTensor(X_train)
X_test = torch.FloatTensor(X_test)
y_train = torch.LongTensor(y_train)
y_test = torch.LongTensor(y_test)

trainD = TensorDataset(X_train, y_train)
testD = TensorDataset(X_test, y_test)
class Model(nn.Module):
    def __init__(self, inp1=10, out=1):
        super().__init__()
        self.Dense1 = nn.Linear(inp1, 32)
        self.Dense2 = nn.Linear(32, 32)
        self.out = nn.Linear(32, out)

    def forward(self, x):
        x = F.relu(self.Dense1(x))
        x = F.relu(self.Dense2(x))
        x = self.out(x)
        return x
model = Model()
trainloader = DataLoader(trainD, batch_size=64, shuffle=False)
testloader  = DataLoader(testloader, batch_size=64, shuffle=False)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
epochs1 = 500
losses = []

epochs1 = 500
losses = []
for i in range(epochs1):
    for data in trainloader:
        X, y = data
        optimizer.zero_grad()
        output = model(X)
        loss = criterion(output, y)
        losses.append(loss)
        loss.backward()
        optimizer.step()


Error thrown:

IndexError                                Traceback (most recent call last)
 in 
      5     i =+1
      6     y_pred = model.forward(X_train)
----&gt; 7     loss = criterion(y_pred, y_train)
      8     losses.append(loss)
      9 

~\Anaconda3\envs\ml1\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--&gt; 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~\Anaconda3\envs\ml1\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
    914     def forward(self, input, target):
    915         return F.cross_entropy(input, target, weight=self.weight,
--&gt; 916                                ignore_index=self.ignore_index, reduction=self.reduction)
    917 
    918 

~\Anaconda3\envs\ml1\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2019     if size_average is not None or reduce is not None:
   2020         reduction = _Reduction.legacy_get_string(size_average, reduce)
-&gt; 2021     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
   2022 
   2023 

~\Anaconda3\envs\ml1\lib\site-packages\torch\nn\functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   1836                          .format(input.size(0), target.size(0)))
   1837     if dim == 2:
-&gt; 1838         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   1839     elif dim == 4:
   1840         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

IndexError: Target 1 is out of bounds.


You are trying to predict a discrete class with a regression network.

When trying to predict discrete classes, one usually outputs a vector of class-probabilities - the probability for each class given the input. On the other hand, there are regression tasks in which one wants to compute a continuous function of the given input. In the regression case, the new usually outputs only one scalar value per input.

In your code you are mixing the two: on the one hand your network has a single scalar output (self.out of your model has out_features=1). On the other hand, you are using nn.CrossEntropyLoss() which is a loss for classification that expects a vector of class probabilities.
