I’m not sure why you get this error.
Your code runs fine for this dummy input and target:
def train(epoch):
    model.train()
    for i in range(len(x)):
        optimizer.zero_grad()
        output = model(x[i])
        print(output, target[i])
        loss = criterion(output, target[i])
        loss.backward()
        optimizer.step()
        losses.append(loss.item())
        print("loss: {}".format(loss.item()))

x = torch.randn(1, 4909)
target = torch.tensor([[1.]])

losses = []
train(0)