Attribut Error: 'torch.dtype' object has no attribute 'type'

Yellow-Champagne: <p>Traceback (most recent call last):</p>
<p>File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 204, in &lt;module&gt;</p>
<p>main()</p>
<p>File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 72, in main</p>
<p>i_tb = train(train_loader, net, optimizer, epoch, i_tb)</p>
<p>File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 105, in train</p>
<p>writer.add_scalar(‘train_loss’, loss.data[0], i_tb)</p>
<p>IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number</p>
<p>AttributeError: ‘torch.dtype’ object has no attribute ‘type’</p>

Fasc: <p>It’s pretty hard to help you here since I can’t reproduce your error. I would recommend you to use a debugger and look at your loss variable and figure out how to access the loss from what is likely some sort of tensor. The error message indicates that you might want to use a .item() somewhere.</p>

Yellow-Champagne: <aside class="quote no-group" data-username="Fasc" data-post="2" data-topic="42597" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/c2a13f/48.png" class="avatar"> Fasc:</div>
<blockquote>
<p>It’s pretty hard to help you here since I can’t reproduce your error. I would recommend you to use a debugger and look at your loss variable and figure out how to access the loss from what is likely some sort of tensor. The error message indicates that you might want to use a .item() somewhere.</p>
</blockquote>
</aside>
<p>Thank you for your prompt reply, I will try your method.</p>

Yellow-Champagne: <p>Sorry, I sent the error message wrong. The correct information is like this.</p>
<p>Traceback (most recent call last):</p>
<p>File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 204, in &lt;module&gt;</p>
<p>main()</p>
<p>File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 81, in main</p>
<p>validate(val_loader, net, epoch, restore_transform)</p>
<p>File "D:/zgy/GCC/GCC-SFCN-master/train.py", line 163, in validate</p>
<p>loss = np.mean(np.array(val_loss))</p>
<p>File "D:\Program Files\Anaconda3\envs\lei\lib\site-packages\numpy\core\fromnumeric.py", line 2957, in mean</p>
<p>out=out, **kwargs)</p>
<p>File "D:\Program Files\Anaconda3\envs\lei\lib\site-packages\numpy\core_methods.py", line 80, in _mean</p>
<p>ret = ret.dtype.type(ret / rcount)</p>
<p>AttributeError: ‘torch.dtype’ object has no attribute ‘type’</p>

Fasc: <aside class="quote no-group" data-username="Yellow-Champagne" data-post="4" data-topic="42597">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/919ad9/48.png" class="avatar"> Yellow-Champagne:</div>
<blockquote>
<p>loss = np.mean(np.array(val_loss))</p>
</blockquote>
</aside>
<p>looks like you didn’t cast your val_loss properly. I assume val_loss is a torch tensor - you can try <code>loss = torch.mean(val_loss).numpy()</code> if you get errors related to using a gpu try <code>loss = torch.mean(val_loss).detach().cpu().numpy()</code></p>

Yellow-Champagne: <p>Traceback (most recent call last):</p>
<p>File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 204, in &lt;module&gt;</p>
<p>main()</p>
<p>File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 81, in main</p>
<p>validate(val_loader, net, epoch, restore_transform)</p>
<p>File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 163, in validate</p>
<p>loss = torch.mean(val_loss).numpy()</p>
<p>TypeError: mean(): argument ‘input’ (position 1) must be Tensor, not list</p>
<p>I have tried your method, but the above error.<br>
And in my program “val_loss = []”.How can I modify?</p>

Fasc: <aside class="quote no-group" data-username="Yellow-Champagne" data-post="7" data-topic="42597">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/919ad9/48.png" class="avatar"> Yellow-Champagne:</div>
<blockquote>
<p>loss = torch.mean(val_loss).numpy()</p>
<p>TypeError: mean(): argument ‘input’ (position 1) must be Tensor, not list</p>
</blockquote>
</aside>
<p>Ok so val_loss is a list so it is not the loss you got directly from PyTorch since it would be a tensor then.</p>
<p>It is impossible to give you a good answer without you sharing minimal code necessary to reproduce this error. If you don’t do that we’re stuck with guessing and that’s quite frankly not satisfying for either of us.</p>

Yellow-Champagne: <p><a href="https://pan.baidu.com/s/1W6HwRpecqyJXGUY7NUlJCQ" class="onebox" target="_blank" rel="nofollow noopener">https://pan.baidu.com/s/1W6HwRpecqyJXGUY7NUlJCQ</a><br>
Thank you, my program is in this link.</p>

Yellow-Champagne: <p>Or use this link<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="32" height="32">
      <a href="https://github.com/gjy3035/GCC-SFCN" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars2.githubusercontent.com/u/7986462?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="359" height="359">

<h3><a href="https://github.com/gjy3035/GCC-SFCN" target="_blank" rel="nofollow noopener">gjy3035/GCC-SFCN</a></h3>

<p>This is the official code of spatial FCN in the paper Learning from Synthetic Data for Crowd Counting in the Wild [CVPR2019]. - gjy3035/GCC-SFCN</p>


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

Fasc: <p>Thank you!</p>
<p>As I see it your code should work if you replace <code>val_loss.append(net.loss.data)</code> with <code>val_loss.append(net.loss.data.numpy())</code> that way you should get a list of floats instead of a list of tensors which seems to be your issue.</p>

Yellow-Champagne: <p>First of all, thank you, but still gave an error.</p>
<aside class="quote no-group quote-modified" data-username="Yellow-Champagne" data-post="7" data-topic="42597">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/919ad9/48.png" class="avatar"> Yellow-Champagne:</div>
<blockquote>
<p>Traceback (most recent call last):</p>
<p>File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 204, in &lt;module&gt;</p>
<p>main()</p>
<p>File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 81, in main</p>
<p>validate(val_loader, net, epoch, restore_transform)</p>
<p>File “D:/zgy/GCC/GCC-SFCN-master/train.py”, line 131, in validate<br>
val_loss.append(net.loss.data.numpy())</p>
<p>TypeError:cannot convert CUDA tensor to numpy .Use Tensor.cpu() to copy the tensor to host memory first.</p>
</blockquote>
</aside>

Fasc: <p>Alrighty - so you still need to get the tensor back from your GPU - Try <code>net.loss.data.cpu().numpy()</code> and if that doesn’t work <code>net.loss.data.detach().cpu().numpy()</code></p>

Yellow-Champagne: <p>Still reported the error in the same place.When I try <strong><code>net.loss.data.cpu().numpy()</code></strong> ,it reports <strong>AttributeError:‘builtin_function_or_method’ object has no attribute ‘numpy’</strong> .When I try <strong><code>net.loss.data.detach().cpu().numpy()</code></strong> ,it reports <strong>AttributeError:‘builtin_function_or_method’ object has no attribute ‘cpu’</strong> .</p>
<p>Thank you.</p>

Fasc: <p>I’m only on my laptop right now so I can’t reproduce your error. What is the type of <code>net.loss.data.cpu()</code>?</p>

Yellow-Champagne: <p>Thank you, I finally solved this problem. My method is <strong>loss = np.mean(val_loss)</strong> .Now I will share this joy with you, thank you again for your constant help.</p>

Fasc: <p>I’m glad you figured it out. Sorry that I wasn’t that helpful after all.</p>

111267: <p>Hello, I also encountered the same problem as you, but I used the way that <strong>loss = np.mean(val_loss)</strong>，What went wrong<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/1/01b0b43ee28434885d3a79669535d6b21b5b0348.png" data-download-href="https://discuss.pytorch.org/uploads/default/01b0b43ee28434885d3a79669535d6b21b5b0348" title="QQ截图20200314213203"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/1/01b0b43ee28434885d3a79669535d6b21b5b0348_2_690x147.png" alt="QQ截图20200314213203" data-base62-sha1="eX3YSKxSO3OXAwHD2YZ7UCs8Tm" width="690" height="147" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/1/01b0b43ee28434885d3a79669535d6b21b5b0348_2_690x147.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/1/01b0b43ee28434885d3a79669535d6b21b5b0348_2_1035x220.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/1/01b0b43ee28434885d3a79669535d6b21b5b0348_2_1380x294.png 2x" data-dominant-color="111111"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">QQ截图20200314213203</span><span class="informations">1703×364 33.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>What type does <code>val_loss</code> have?<br>
If it’s a tensor, I would recommend to use <code>torch.mean</code> or if you need to use numpy, use <code>np.mean(val_loss.numpy())</code>.</p>

111267: <p>thx very much! I used val_loss.item() to solve this problem</p>
<p>ptrblck via PyTorch Forums <a href="mailto:noreply@discuss.pytorch.org">noreply@discuss.pytorch.org</a> 于2020年3月15日周日 下午6:09写道：</p>

kysunami: <p>Even this didn’t work</p>