How to use criterion with torch.max return value,thank you!

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(rnn.parameters(), lr=learning_rate)

for i, (images, labels) in enumerate(train_loader):
    images = Variable(images.view(-1, sequence_length, input_size)).cuda()
    # Forward + Backward + Optimize
    outputs = rnn(images)
    _, predicted = torch.max(, 1)
    loss = criterion(outputs, predicted)

error happen with line loss = criterion(outputs, predicted)
AttributeError: ‘torch.cuda.LongTensor’ object has no attribute 'requires_grad’
how can I use criterion with predicted? thank you

u need to make labels a Variable

labels = Variable(labels).cuda()

thanks for your answer, before I have used :labels = Variable(labels).cuda(),
error also exist as:AttributeError: ‘torch.cuda.LongTensor’ object has no attribute 'requires_grad’
this error location:
loss = criterion(outputs, predicted)
I also used : = predicted, occur other errors!

u can’t use predicted to get loss, u should use labels, because u need to get loss by comparing ground truth and predicting label, the predict is outputs, and the ground truth is labels

loss = criterion(outputs, labels)

thanks for you!Well,you are right, but I attempt to do it with a new way.Now, I know how to modify:

, predicted = torch.max(, 1)
loss = criterion(outputs, labels)

thinks, SherlockLiaoSherlock