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

username: MasayoMusic
cooked: <p>I am going through the ants bees transfer learning tutorial:</p>
<p><a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html</a></p>
<p>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):</p>
<p>if num % 100 == 0:<br>
print(‘running_corrects’, running_corrects/( len(inputs) *  num + 1)</p>
<p>running corrects should not be using auto-grad,<br>
and I thought it would be printable.</p>
<p>When I print I get the tensor value:<br>
“running_corrects tensor(0, device=‘cuda:0’)”<br>
however if I try to <code>.numpy</code> or <code>.cpu</code><br>
I get the error:</p>
<p><code>AttributeError: 'int' object has no attribute 'numpy'</code><br>
AttributeError: ‘int’ object has no attribute ‘cpu’</p>
<p>I found this:</p>
<aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/prlz77/ResNeXt.pytorch/issues/7" target="_blank" rel="nofollow noopener">github.com/prlz77/ResNeXt.pytorch</a>
  </header>
  <article class="onebox-body">
    <a href="https://github.com/zorrocai" rel="nofollow noopener">
<img src="https://avatars1.githubusercontent.com/u/24424749?v=2&amp;s=96" class="thumbnail onebox-avatar" width="60" height="60">
</a>

<h4><a href="https://github.com/prlz77/ResNeXt.pytorch/issues/7" target="_blank" rel="nofollow noopener">Issue: TypeError: tensor(0, device='cuda:0') is not JSON serializable</a></h4>

<div class="date" style="margin-top:10px;">
	<div class="user" style="margin-top:10px;">
	opened by <a href="https://github.com/zorrocai" target="_blank" rel="nofollow noopener">zorrocai</a>
	on <a href="https://github.com/prlz77/ResNeXt.pytorch/issues/7" target="_blank" rel="nofollow noopener">2018-05-10</a>
	</div>
	<div class="user">
	closed by <a href="https://github.com/prlz77" target="_blank" rel="nofollow noopener">prlz77</a>
	on <a href="https://github.com/prlz77/ResNeXt.pytorch/issues/7" target="_blank" rel="nofollow noopener">2018-05-10</a>
	</div>
</div>

<pre class="content" style="white-space: pre-wrap;">when I run the codes following your instructions, "TypeError: tensor(0, device='cuda:0') is not JSON serializable" occurs at the line 167 of...</pre>

<div class="labels">
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>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).</p>
<p>Thank you.</p>

username: peterjc123
cooked: <p><code>running_corrects</code> is already a <code>int</code>, why do you want to do <code>.numpy()</code> or <code>.cpu()</code>?</p>

username: MasayoMusic
cooked: <p>Because I was getting back this message:<br>
<code>Running_corrects tensor(0, device='cuda:0')</code></p>
<p>if I just try to print as follows:<br>
print(‘running_corrects’, running_corrects/( len(inputs) * num + 1)</p>
<p>So I thought It was a tensor on GPU and I need to bring it back to the CPU.</p>
<p>I don’t why I am not getting back an int value and that is the problem I am trying to solve.</p>

username: Balamurali_M
cooked: <aside class="quote no-group" data-username="MasayoMusic" data-post="1" data-topic="47486">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/masayomusic/48/12934_2.png" class="avatar"> MasayoMusic:</div>
<blockquote>
<p>print(‘running_corrects’, running_corrects/( len(inputs) * num + 1)</p>
</blockquote>
</aside>
<p>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 ?</p>

username: MasayoMusic
cooked: <p>Thank you. Your method works. First convert it to double, and then cpu().numpy().</p>