Pytorch NN for classification problem

I have a problem with loss_function; my code can not run

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

Note, my dataset from Human Activity Recognition with Smartphones | Kaggle

Your current model returns logits for 6 classes, which also defines the valid target class indices as values in the range [0, 5]. 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.