Reputation: 38145
When trying to convert a code written for old PyTorch to 1.9 I get this error:
(fashcomp) [jalal@goku fashion-compatibility]$ python main.py --name test_baseline --learned --l2_embed --datadir ../../../data/fashion/
/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
<class 'torch.utils.data.dataloader.DataLoader'>
/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 329, in <module>
main()
File "main.py", line 167, in main
train(train_loader, tnet, criterion, optimizer, epoch)
File "main.py", line 240, in train
print('Train Epoch: {} [{}/{}]\t'
File "/scratch3/venv/fashcomp/lib/python3.8/site-packages/torch/_tensor.py", line 561, in __format__
return object.__format__(self, format_spec)
TypeError: unsupported format string passed to Tensor.__format__
Here's the problematic part of the code:
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))
I see from this bug report that, as of two years ago, there was no solution provided to this problem. Do you have any suggestions on how to fix this, or an alternative of any sort?
Code is from here.
Upvotes: 2
Views: 9001
Reputation: 40618
This error is reproducible if you try to format a torch.Tensor
in a specific way:
>>> print('{:.2f}'.format(torch.rand(1)))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-ece663be5b5c> in <module>()
----> 1 print('{:.2f}'.format(torch.tensor([1])))
/usr/local/lib/python3.7/dist-packages/torch/_tensor.py in __format__(self, format_spec)
559 if self.dim() == 0:
560 return self.item().__format__(format_spec)
--> 561 return object.__format__(self, format_spec)
562
563 def __ipow__(self, other): # type: ignore[misc]
TypeError: unsupported format string passed to Tensor.__format__
Doing '{}'.format(torch.tensor(1))
- i.e. without any formating rule - will work.
This is because torch.Tensor
doesn't implement those specific format operations.
An easy fix would be to convert the torch.Tensor
to the appropriate - corresponding - type using item
:
>>> print('{:.2f}'.format(torch.rand(1).item()))
0.02
You should apply this modification to all torch.Tensor
involved in your print
string expression: losses.val
, losses.avg
, accs.val
, accs.avg
, emb_norms.val
, and emb_norms.avg
?
Upvotes: 5