Converting an old pytorch code (3 years ago) to 1.9

Mona_Jalal: <p>could someone please confirm if this change  (## refers to old code)</p>
<pre><code>    # measure accuracy and record loss
    ##losses.update(loss_triplet.data[0], num_items)
    ##accs.update(acc.data[0], num_items)
    ##emb_norms.update(loss_embed.data[0])
    ##mask_norms.update(loss_mask.data[0])
    
    losses.update(loss_triplet.data, num_items)
    accs.update(acc.data, num_items)
    emb_norms.update(loss_embed.data)
    mask_norms.update(loss_mask.data)
</code></pre>
<p>based on this suggestion <a href="https://github.com/NVIDIA/flownet2-pytorch/issues/113#issuecomment-450802359" class="inline-onebox" rel="noopener nofollow ugc">invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number · Issue #113 · NVIDIA/flownet2-pytorch · GitHub</a> makes any sense if the error i was getting is non-existent?<br>
Error was:</p>
<blockquote>
<p>invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number</p>
</blockquote>

ptrblck: <p>Remove the <code>.data</code> usage, as it could break your code and use <code>.item()</code> instead:</p>
<pre><code class="lang-python">    losses.update(loss_triplet.item(), num_items)
    accs.update(acc.item(), num_items)
    emb_norms.update(loss_embed.item())
    mask_norms.update(loss_mask.item())
</code></pre>

Mona_Jalal: <p>Do you know why this fix for format didn’t work for all tensors?</p>
<pre><code class="lang-auto">        if batch_idx % args.log_interval == 0:
            print('Train Epoch: {} [{}/{}]\t'
                  'Loss: {:.4f} ({:.4f}) \t'
                  'Acc: {:.2f}% ({:.2f}%) \t'
                  'Emb_Norm: {:.2f} ({:.2f})'.format(
                epoch, batch_idx * num_items, len(train_loader.dataset),
                losses.val.item(), losses.avg.item(), 
                100. * accs.val.item(), 100. * accs.avg.item(), emb_norms.val.item(), emb_norms.avg.item()))
</code></pre>
<p>error is</p>
<blockquote>
<p>File “main.py”, line 167, in main     train(train_loader, tnet, criterion, optimizer, epoch)   File “main.py”, line 251, in train     losses.val.item(), losses.avg.item(),  AttributeError: ‘float’ object has no attribute ‘item’</p>
</blockquote>

ptrblck: <p>My best guess is that <code>losses</code> might be an object tracking some internal stats as <code>float</code>s and <code>losses.val</code> as well as <code>losses.avg</code> would already be <code>float</code> values (not tensors), so you could remove the <code>.item()</code> call.</p>

Mona_Jalal: <p>Sorry I was about to share the full log here:</p>
<pre><code class="lang-auto">(fashcomp) [jalal@goku fashion-compatibility]$ python main.py --name test_baseline --learned --l2_embed --datadir ../../../data/fashion/ --epochs 1
/scratch3/venv/fashcomp/lib/python3.8/site-packages/torchvision/transforms/transforms.py:310: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
  warnings.warn("The use of the transforms.Scale transform is deprecated, " +
  + Number of params: 3191808
&lt;class 'torch.utils.data.dataloader.DataLoader'&gt;
/scratch3/venv/fashcomp/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
Traceback (most recent call last):
  File "main.py", line 335, in &lt;module&gt;
    main()    
  File "main.py", line 167, in main
    train(train_loader, tnet, criterion, optimizer, epoch)
  File "main.py", line 251, in train
    losses.val.item(), losses.avg.item(), 
AttributeError: 'float' object has no attribute 'item'
</code></pre>

Mona_Jalal: <p>so I removed it from the losses methods but still have the error. Do you which items from this .format requires .item()?</p>
<pre><code class="lang-auto">            print('Train Epoch: {} [{}/{}]\t'
                  'Loss: {:.4f} ({:.4f}) \t'
                  'Acc: {:.2f}% ({:.2f}%) \t'
                  'Emb_Norm: {:.2f} ({:.2f})'.format(
                epoch, batch_idx * num_items, len(train_loader.dataset),
                losses.val, losses.avg, 
                100. * accs.val.item(), 100. * accs.avg.item(), emb_norms.val.item(), emb_norms.avg.item()))
</code></pre>
<p>now error is:</p>
<pre><code class="lang-auto">(fashcomp) [jalal@goku fashion-compatibility]$ python main.py --name test_baseline --learned --l2_embed --datadir ../../../data/fashion/ --epochs 1
/scratch3/venv/fashcomp/lib/python3.8/site-packages/torchvision/transforms/transforms.py:310: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
  warnings.warn("The use of the transforms.Scale transform is deprecated, " +
  + Number of params: 3191808
&lt;class 'torch.utils.data.dataloader.DataLoader'&gt;
/scratch3/venv/fashcomp/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
Traceback (most recent call last):
  File "main.py", line 335, in &lt;module&gt;
    main()    
  File "main.py", line 167, in main
    train(train_loader, tnet, criterion, optimizer, epoch)
  File "main.py", line 252, in train
    100. * accs.val.item(), 100. * accs.avg.item(), emb_norms.val.item(), emb_norms.avg.item()))
AttributeError: 'float' object has no attribute 'item'
</code></pre>
<p>my personal guess is that acc should be float too.</p>

ptrblck: <aside class="quote no-group" data-username="Mona_Jalal" data-post="6" data-topic="130893">
<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/mona_jalal/48/25715_2.png" class="avatar"> Mona_Jalal:</div>
<blockquote>
<p>my personal guess is that acc should be float too.</p>
</blockquote>
</aside>
<p>Yes, this would also be my guess as note how the error changed from:</p>
<pre data-code-wrap="python"><code class="lang-python">  File "main.py", line 251, in train
    losses.val.item(), losses.avg.item(), 
AttributeError: 'float' object has no attribute 'item'
</code></pre>
<p>to</p>
<pre data-code-wrap="python"><code class="lang-python">  File "main.py", line 252, in train
    100. * accs.val.item(), 100. * accs.avg.item(), emb_norms.val.item(), emb_norms.avg.item()))
AttributeError: 'float' object has no attribute 'item'
</code></pre>

Mona_Jalal: <p>I am confused since I followed this answer here <a href="https://stackoverflow.com/a/69021370/2414957" class="inline-onebox" rel="noopener nofollow ugc">python - TypeError: unsupported format string passed to Tensor.__format__ - Stack Overflow</a></p>
<p>I removed all .item() (s) and bug is gone.</p>
<pre><code class="lang-auto">(fashcomp) [jalal@goku fashion-compatibility]$ python main.py --name test_baseline --learned --l2_embed --datadir ../../../data/fashion/ --epochs 1
/scratch3/venv/fashcomp/lib/python3.8/site-packages/torchvision/transforms/transforms.py:310: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
  warnings.warn("The use of the transforms.Scale transform is deprecated, " +
  + Number of params: 3191808
&lt;class 'torch.utils.data.dataloader.DataLoader'&gt;
/scratch3/venv/fashcomp/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
Train Epoch: 1 [0/686851]	Loss: 0.2760 (0.2760) 	Acc: 61.72% (61.72%) 	Emb_Norm: 0.76 (0.76)
Train Epoch: 1 [64000/686851]	Loss: 0.2212 (0.2369) 	Acc: 70.31% (64.27%) 	Emb_Norm: 0.70 (0.69)
</code></pre>
<p>and code is:</p>
<pre><code class="lang-auto">        if batch_idx % args.log_interval == 0:
            print('Train Epoch: {} [{}/{}]\t'
                  'Loss: {:.4f} ({:.4f}) \t'
                  'Acc: {:.2f}% ({:.2f}%) \t'
                  'Emb_Norm: {:.2f} ({:.2f})'.format(
                epoch, batch_idx * num_items, len(train_loader.dataset),
                losses.val, losses.avg, 
                100. * accs.val, 100. * accs.avg, emb_norms.val, emb_norms.avg))
</code></pre>
<p>initially asked that question because of this error:</p><aside class="onebox stackexchange" data-onebox-src="https://stackoverflow.com/questions/69021335/typeerror-unsupported-format-string-passed-to-tensor-format">
  <header class="source">

      <a href="https://stackoverflow.com/questions/69021335/typeerror-unsupported-format-string-passed-to-tensor-format" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/2414957/mona-jalal" target="_blank" rel="noopener nofollow ugc">
    <img alt="Mona Jalal" src="https://i.stack.imgur.com/0MuAW.jpg?s=128&amp;g=1" class="thumbnail onebox-avatar" width="128" height="128">
  </a>

<h4>
  <a href="https://stackoverflow.com/questions/69021335/typeerror-unsupported-format-string-passed-to-tensor-format" target="_blank" rel="noopener nofollow ugc">TypeError: unsupported format string passed to Tensor.__format__</a>
</h4>

<div class="tags">
  <strong>python, deep-learning, pytorch, computer-vision</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/2414957/mona-jalal" target="_blank" rel="noopener nofollow ugc">
    Mona Jalal
  </a>
  on <a href="https://stackoverflow.com/questions/69021335/typeerror-unsupported-format-string-passed-to-tensor-format" target="_blank" rel="noopener nofollow ugc">10:52PM - 01 Sep 21 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
I am honestly confused but glad the error is gone.</p>