I want to feed a single label to the model and update the model

This is my code,

correct_class=Variable(output_class[get_ans]).long()
loss=criterion(output,correct_class)
loss.backward()
optimizer.step()

It shows.
RuntimeError: Variable data has to be a tensor, but got int
How do I feed one image and one label to the model and update the model?

Hi,

You should upgrade your pytorch version as no Variables are needed anymore.
If your labels is a single python number, you can simply wrap it in a long (or whatever type you criterion expects) tensor as torch.LongTensor([correct_class]).

Thanks. I did this in the same version,
torch.LongTensor(output_class[get_ans])

Iam getting Variable containing:[torch.LongTensor with no dimension]

output of

output_class[get_ans] is 0
When I convert 0 to longtensor it shows no dimension

It should have 1 dimension of size 1:

a = torch.LongTensor([0])
a.size() # Gives torch.Size([1])

Also there should be no mention of Variable if you use the latest pytorch version.

Thanks a lot. I got another error
RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.

Iam actually manually upgrading the weights by getting inputs from user. I read your answer to provide retain_graph=True. Still it does not work. Here is my code.

correct_class=torch.LongTensor([output_class[get_ans]])
optimizer.zero_grad()
loss=criterion(output,correct_class)
loss.backward(retain_graph=True)
optimizer.step()