AttributeError: 'int' object has no attribute 'cuda'

In the below code,

model_ft.eval()
test_data, test_target = image_datasets['train'][idx]
test_data = test_data.cuda()
test_target = test_target.cuda()

I get the error that test_target can’t use the .cuda():

Traceback (most recent call last):

File "test_loocv.py", line 239, in <module>:

test_target = test_target.cuda()

AttributeError: 'int' object has no attribute 'cuda'

How should I fix this?

1 Like

Whatever is coming out of image_datasets['train'][idx] is not a tensor. You can try wrapping test_data and test_target like this: test_data = torch.tensor(test_data) and see if that works.

1 Like

Thank you. I did not need to do it for test_data but only for test_target.

    test_data, test_target = image_datasets['train'][idx]
    test_data = test_data.cuda()
    #test_target = test_target.cuda()
    test_target = torch.tensor(test_target)
    test_target = test_target.cuda()
    test_data.unsqueeze_(1)
    test_target.unsqueeze_(0)

The code above works.

3 Likes