module ‘torch’ has no attribute ‘xxx’ 问题

    技术2022-07-14  84

    import torch出现错误:module ‘torch’ has no attribute 'from_numpy’ 刚接触torch,好不容易安装成功(pip install torch1.5.1+cpu torchvision0.6.1+cpu -f https://download.pytorch.org/whl/torch_stable.html),cpu版本,打开发现这样问题:module ‘torch’ has no attribute ‘from_numpy’ 心情很是糟糕,求助了淘宝卖家,他说可能是版本的问题,我的torch版本是“1.5.1+cpu”,numpy的版本是“1.18.1”,准备降级试一下,后来在网上搜了很多相关错误,发现python文件中有个文件名是torch.py,重命名就ok了,心情豁然开朗了哈哈哈,表白淘宝小哥哥,和我同时找到错误原因嘻嘻嘻

    新手第一次使用pytorch千万不要把文件命名为“torch.py”!!! 解决方法:定义xxx.py文件名时,xxx不要是python保留字,不能使用Python系统库的名字

    Processed: 0.015, SQL: 9