Hey, I’m having the same error, but I don’t think the problem is quite the same.
I’m doing the Pytorch’s Classifier Tutorial which uses CIFAR10, so it’s a multi-class problem. However, I want to use my own dataset, so I’m not using DataLoader.
My X_train got shape (100, 3, 64, 64), being a tensor full of 64x64 images and my y_train has been one-hot encoded using torch.nn.functional.one_hot, so it got shape (100, 3).
I’ve modified the training loop accordingly, but I keep getting the same IndexError.
Here’s the code for the Neural Network and the training loop:
class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(2704, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 3)
        self.ReLU = nn.ReLU()
        self.softmax = nn.Softmax(dim=-1)

    def forward(self, x):
        x = self.conv1(x)
        x = self.ReLU(x)
        x = self.pool(x)
        x = self.conv2(x)
        x = self.ReLU(x)
        x = self.pool(x)
        x = torch.flatten(x)
        x = self.fc1(x)
        x = self.ReLU(x)
        x = self.fc2(x)
        x = self.ReLU(x)
        x = self.fc3(x)
        x = self.softmax(x)
        return x

net = Net().to(device)

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

num_epochs = 10

for epoch in range(num_epochs):
    for i in range(len(X_train)):
        inputs = X_train[i]
        inputs = torch.unsqueeze(inputs, 0) # One sample at time.
        labels = y_train[i]

    optimizer.zero_grad()

    outputs = net(inputs)

    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()
    print(f"Epoch {i} concluded")```