python,怎么解决这个输入问题,应该怎么改

    sr_img = utils.tensor2np(pre.detach()[0])
    gt_img = utils.tensor2np(hr_tensor.detach()[0])
    

    
    
    
      File "/home/tt/train.py", line 197, in <module>
valid()

File "/home/tt/train.py", line 162, in valid
sr_img = utils.tensor2np(pre.detach()[0])
AttributeError: 'list' object has no attribute 'detach'

列表没有detach这个方法,这不是提示你了吗?

你的list列表对象里是不是存取的对象不能调用detach
题主,这有个例子:看下你调用detach函数的方式是否正确

import torch
 
a = torch.tensor([1, 2, 3.], requires_grad=True)
print(a)
out = a.tanh()
print(out)
c = out.detach()  # 需要走注意的是,通过.detach() “分离”得到的的变量会和原来的变量共用同样的数据,而且新分离得到的张量是不可求导的,c发生了变化,原来的张量也会发生变化
c.zero_()  # 改变c的值,原来的out也会改变
print(c.requires_grad)
print(c)
print(out.requires_grad)
print(out)

 
 
# 输出
tensor([1., 2., 3.], requires_grad=True)
tensor([0.7616, 0.9640, 0.9951], grad_fn=<TanhBackward>)
False
tensor([0., 0., 0.])
True
tensor([0., 0., 0.], grad_fn=<TanhBackward>)


你的pre是什么?报错显示它是列表类型,你希望它是什么类型?