gpt4 book ai didi

python - 类型错误 : unsupported format string passed to Tensor. __format__

转载 作者:行者123 更新时间:2023-12-02 18:34:10 26 4
gpt4 key购买 nike

当尝试将旧 PyTorch 编写的代码转换为 1.9 时,我收到此错误:

(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__

这是代码中有问题的部分:

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))

我从this bug report看到截至两年前,这个问题还没有得到解决。您对如何解决此问题有什么建议或任何替代方案吗?

代码来自here .

最佳答案

如果您尝试以特定方式格式化 torch.Tensor,则可以重现此错误:

>>> 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__

在没有任何格式规则的情况下执行'{}'.format(torch.tensor(1)) - - 会起作用。

这是因为 torch.Tensor 没有实现这些特定的格式操作。


一个简单的修复方法是使用 itemtorch.Tensor 转换为适当的 - 相应的 - 类型 :

>>> print('{:.2f}'.format(torch.rand(1).item()))
0.02

您应该将此修改应用于 print 字符串表达式中涉及的所有 torch.Tensor:losses.vallosses。 avgaccs.valaccs.avgemb_norms.valemb_norms.avg

关于python - 类型错误 : unsupported format string passed to Tensor. __format__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69021335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com