i have no proplem with following code:
import torch
import torch.nn as nn

model = nn.Sequential(nn.Linear(28*28,10))
optimizer = torch.optim.SGD(model.parameters(), 0.03)
criterion = nn.CrossEntropyLoss()

sample_img, sample_label = torch.randn(1,28,28) , 0

sample_img.requires_grad = True
prediction = model(sample_img.view(-1).unsqueeze(dim=0))
cost = criterion(prediction, torch.tensor([sample_label]))
optimizer.zero_grad()
cost.backward()
print(sample_label)
#print(sample_img.shape)

print(sample_img.grad)

maybe it’s your model that cuasing problem??