当前位置: 代码迷 >> 综合 >> AttributeError: ‘torch.return_types.max‘ object has no attribute ‘shape‘
  详细解决方案

AttributeError: ‘torch.return_types.max‘ object has no attribute ‘shape‘

热度:38   发布时间:2023-11-11 09:55:58.0

测试程序有一句出现了bug,以前没见过,就是正常使用,不觉得会有错。

import torchb = torch.range(1,12,1).reshape([3,2,2])
print(b,b.shape)d = torch.max(b,dim=0,keepdim=True)###bug所在print(d,d.shape)

网上查了一下,torch.max()返回是个turtle,包含了 value,index,所以错误原因也找到了,没有定义index的变量。修改后如下:

import torchb = torch.range(1,12,1).reshape([3,2,2])
print(b,b.shape)d,e = torch.max(b,dim=0,keepdim=True)print(d,d.shape)
print(e)
#######################
E:\Anconda\python.exe "C:/Users/MR-LI/Desktop/program practice/1.py"
tensor([[[ 1.,  2.],
C:/Users/MR-LI/Desktop/program practice/1.py:3: UserWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [start; end].[ 3.,  4.]],b = torch.range(1,12,1).reshape([3,2,2])[[ 5.,  6.],[ 7.,  8.]],[[ 9., 10.],[11., 12.]]]) torch.Size([3, 2, 2])
tensor([[[ 9., 10.],[11., 12.]]]) torch.Size([1, 2, 2])
tensor([[[2, 2],[2, 2]]])

OK了!!!!

  相关解决方案