Pytorch NN for classification problem

SutthidaMint: <p>I have a problem with loss_function; my code can not run</p>
<pre><code class="lang-auto">trainX = pd.read_csv("/content/drive/MyDrive/UCI/X_train.csv",header=None)
trainY = pd.read_csv("/content/drive/MyDrive/UCI/y_train.csv",header=None)
testX = pd.read_csv("/content/drive/MyDrive/UCI/X_test.csv",header=None)
testY = pd.read_csv("/content/drive/MyDrive/UCI/y_test.csv",header=None)

#convert data to numpy array
numpyX = np.array(trainX.values)
numpyY = np.array(trainY)
numpyY2 = np.ravel(numpyY)
print(numpyX.shape,numpyY.shape, numpyY2.shape)

#convert data to numpy array
numpytestX = np.array(testX.values)
numpytestY = np.array(testY)
numpytestY2 = np.ravel(numpytestY)
print(numpytestX.shape,numpytestY.shape, numpytestY2.shape)

MyNet = nn.Sequential(
    nn.Linear(561, 20),
    nn.Tanh(),
    nn.Dropout(p=0.5),
    nn.Linear(20, 30),
    nn.Tanh(),
    nn.Dropout(p=0.4),
    nn.Linear(30, 6),
)
MyNet.to(device)

BATCH_SIZE = 500
summary(MyNet, input_size=(BATCH_SIZE, 561))

train_lossLst = []
train_lossLstavg = []
validation_lossLst = []
test_accuracyLst = []

#train NN
for i in range(1, 3):
  print("\nTrain-test ", i, end= " ")
  #split validation data
  trainX, valX, trainY, valY = train_test_split(numpyX, numpyY2, test_size=0.10, random_state = 0)
  #load data
  tensorX = torch.FloatTensor(trainX).to(device)
  tensorY = torch.LongTensor(trainY).to(device)
  torch_dataset = Data.TensorDataset(tensorX, tensorY)
  loader = Data.DataLoader(
      dataset=torch_dataset,  
      batch_size=BATCH_SIZE,      
      shuffle=True,       
      num_workers=0,
      )

  TESTX = numpytestX
  TESTY = numpytestY2

  # initialize NN weights
  for name, param in MyNet.named_parameters():
    if(param.requires_grad):
      torch.nn.init.normal_(param, mean=0.0, std=0.02)
  loss_func = torch.nn.CrossEntropyLoss()
  optimizer = torch.optim.Adam(MyNet.parameters(), lr=0.0003)

# train NN 
  print("epoch", end=": ")
  epoch_lossLst=[]
  for epoch in range(1, 50):
    if(epoch%100 == 0):
      print(epoch, end=",")
    for (batchX, batchY) in loader:
      batchY_hat = MyNet(batchX)
      loss = loss_func(batchY_hat,batchY) #loss = loss_fn(outputs, targets) #ERROR LINE
      epoch_lossLst.append(float(loss))
      optimizer.zero_grad()
      loss.backward()
      optimizer.step()
  train_lossLst.append(epoch_lossLst) 

  tensor_valX = torch.FloatTensor(numpytestX).to(device)
  tensor_valY = torch.LongTensor(numpytestY2).to(device)
  tensor_valY_hat = MyNet(tensor_valX)
  validation_loss=loss.func(tensor_valY,validation_loss)
  validation_lossLst.append(float(validation_loss))
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/8/a81b2ce4919e26814a9111a671663c88ddafb268.png" data-download-href="https://discuss.pytorch.org/uploads/default/a81b2ce4919e26814a9111a671663c88ddafb268" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/8/a81b2ce4919e26814a9111a671663c88ddafb268_2_690x184.png" alt="image" data-base62-sha1="nZ8r9BfliWzHFPOZRxFxEu9UPkQ" width="690" height="184" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/8/a81b2ce4919e26814a9111a671663c88ddafb268_2_690x184.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/8/a81b2ce4919e26814a9111a671663c88ddafb268_2_1035x276.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/8/a81b2ce4919e26814a9111a671663c88ddafb268_2_1380x368.png 2x" data-dominant-color="F7F7F9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1977×528 52.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Note, my dataset from <a href="https://www.kaggle.com/datasets/uciml/human-activity-recognition-with-smartphones" class="inline-onebox" rel="noopener nofollow ugc">Human Activity Recognition with Smartphones | Kaggle</a></p>

ptrblck: <p>Your current model returns logits for 6 classes, which also defines the valid target class indices as values in the range <code>[0, 5]</code>. The error message explains that your current target contains an invalid class index as 6 so you would need to either check why the target is out of bounds or would need to increase the output size of your model in case you are dealing with more classes.</p>