ValueError:only one element tensors can be converted to . . . 这次又遇到问题:ValueError:only one element tensors can be converted to Python scalars。 原因:要转换的list里面的元素包含多维的tensor。 但是要转换的list里面的元素包含多维的tensor,应该使用 这是由于 gpu上的 tensor 不能直接转为 numpy; 须要先在 cpu 上完成操做,再回到 gpu 上。 若是是在 gpu,命令以下 这是由于 gpu上的 tensor 不能直接转为 numpy 先转 numpy,后转 list 参考链接: https: www shangmayuan com a fc6aaa7ff67443c68dbf3966 html
ValueError: only one element tensors can be converted to Python scalars . . . How can I fix this problem? $ python Python 3 8 10 (default, Nov 26 2021, 20:14:08) [GCC 9 3 0] on linux Type "help", "copyright", "credits" or "license" for more information >>> import torch >>> torch __version__ '1 10 1+cu113' X here is a list of torch tensors
ValueError: only one element tensors can be converted to Python scalars . . . ValueError: only one element tensors can be converted to Python scalars You try and convert the output of your model to a python scalar with item () but it complains that it has more than one element, so it cannot do that What is the size of your model’s output? What do you try to achieve with this item () call?