【python debug】python常见编译问题解决方法_2

14 篇文章 1 订阅

 

序言

1. PermissionError: [Errno 13] Permission denied: ‘/lost+found’

  • 修改前:
  • 修改后(解决):
  • 此外,可能文件夹已被打开,也可能是无权限打开,或者打开一个文件而不是文件夹

2. No module named ‘lark’

  • 报错:ModuleNotFoundError: No module named ‘lark’

  • 解决方法:

    python3 -m pip install lark-parser
    

3. RuntimeError: Tensors must have same number of dimensions: got 3 and 2

  • 报错:有A[2, 16]、B[2, 4]两个tensor变量, 使用torch.cat((A, B), 1)拼接却报错
  • 解决方法:注意到模型输出带梯度,如下截图。如果不需要保留梯度信息,可以在变量变换之前加.detach()或.data调用,分离梯度信息

4. RuntimeError: Can’t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead

  • 同上,在tensor转换为list的过程中碰到

5. RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

  • 报错:需要梯度但是没有梯度信息
  • 报错原因1:这里的loss默认的requires_grad是False,因此在backward()处不会计算梯度,导致出错
  • 报错原因2:就是loss本身没梯度,所以调用loss.backward()后向传播时报错,没梯度的原因可能是loss_function的output没有梯度,注意检查

6. RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already beed freed. Specify retain_grad=True when calling .backward() or autograd.grad() the first time.

  • 解决:需要更新的中间结果加.data分离梯度信息

7. RuntimeError: Input and hidden tensors are not at the same device, found input tensor at cuda:0 and hidden tensor at cpu

8. AttributeError: ‘torch.dtype’ object has no attribute ‘type’

  • 报错:使用np.mean(xx)去计算张量xx的均值

  • 解决:np.mean可以操作list和array,但是此处的loss.data类型是torch.tensor, 需要使用torch.mean进行运算,torch.mean得到的结果也是tensor

  • 补充:获取张量的值 tensor.item()

9. ImportError: cannot import name ‘TypeAlias’ from ‘typing_extensions’

  • 或报错:TypeError: Plain typing_extensions.Self is not valid as type argument
  • 原因:以上两个报错都是typing_extensions版本过旧,使用了3.7.4

  • 解决方法:直接升级typing_extensions版本

    conda install typing_extensions=4.10.0	# 或其他版本
    

10. Torch.cuda.is_available()显示GPU Driver过老

  • 报错:The NVIDIA driver on your system is too old, torch.cuda.is_available()=False
  • 分析:GPU Driver版本11.1,并不老,我之前是通过如下命令在conda环境安装torch:

    pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
    
  • 在网上查了这种情况需要重新装一下cuda配套的pytorch版本

  • 原因:不是GPU驱动版本老,而是pytorch版本和cuda版本不匹配

  • 解决方法:重新安装pytorch,如下

    • 在该网站 https://pytorch.org/get-started/previous-versions/ 搜索cuda 11.1

    • 按照该命令重装后torch.cuda.is_available()查询正常

      # CUDA 11.1
      pip install torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html
      

 


【参考文章】
[1]. Permission denied解决方案1
[2]. Permission denied解决方案2
[3]. 不保留梯度信息
[4]. 中间变量添加.data分离梯度信息
[5]. .data和.detach()的区别
[6]. pytorch和cuda版本不匹配

created by shuaixio, 2024.03.03

  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值