Running_corrects tensor(0, device='cuda:0') to int?

I am going through the ants bees transfer learning tutorial:

https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

Instead of each epoch, I wanted to get results every x amount of batches, so I made a couple of tweaks (which probably doesn’t affect the problem I have):

if num % 100 == 0:
print(‘running_corrects’, running_corrects/( len(inputs) * num + 1)

running corrects should not be using auto-grad,
and I thought it would be printable.

When I print I get the tensor value:
“running_corrects tensor(0, device=‘cuda:0’)”
however if I try to .numpy or .cpu
I get the error:

AttributeError: 'int' object has no attribute 'numpy'
AttributeError: ‘int’ object has no attribute ‘cpu’

I found this:

but don’t want to mess around with the version (unless I have to) as it took me a while to get Pytorch working (Windows 10).

Thank you.

running_corrects is already a int, why do you want to do .numpy() or .cpu()?

Because I was getting back this message:
Running_corrects tensor(0, device='cuda:0')

if I just try to print as follows:
print(‘running_corrects’, running_corrects/( len(inputs) * num + 1)

So I thought It was a tensor on GPU and I need to bring it back to the CPU.

I don’t why I am not getting back an int value and that is the problem I am trying to solve.

I think running_corrects will be an integer and the denominator will also be an integer. First the number should be converted to float and perform division. And the tensor should be printed with cpu().numpy() only. Which version of torch are you using ? Can you provide more inputs if this does’nt work ?

1 Like

Thank you. Your method works. First convert it to double, and then cpu().numpy().