PyTorch使用中出现问题记录

问题一:当出现urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)>

出现证书认证失败,证书已经过期问题

解决方式如下:取消全局验证即可

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

问题二:GeForce RTX 3060 Laptop GPU with CUDA capability sm_86 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_61 sm_70 sm_75 compute_37.
If you want to use the GeForce RTX 3060 Laptop GPU GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

原因:当前pytorch的版本不匹配显卡,需要最新版本的pytorch和cudatoolkit11.1,原先安装的是10.2

解决办法:先执行卸载命令后重新安装最新版本pytorch和cudatoolkit

conda uninstall pytorch

conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge

使用的命令:(base) > conda info --envs 查看环境

 import torch

  • print(torch.version.cuda)  显示cuda版本
  • print(torch.__version__)   查看torch版本
  • device = torch.device('cuda')

使用conda卸载Pytorch

  • conda uninstall pytorch
  • conda uninstall libtorch

pip卸载Pytorch重新安装

  • pip uninstall torch 

使用pip更新Pytorch和torchvision

  • pip list --outdated --format=legacy

更新Pytorch和torchvision安装包

  • pip install --upgrade torch torchvision

使用Conda更新Pytorch和torchvision

  • conda config --add channels soumith  将其添加soumith为你的Anaconda(或Miniconda)源服务器
  • conda update pytorch torchvision

问题三:RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)

问题原因:输入类型是CPU(torch.FloatTensor),而参数类型是GPU(torch.cuda.FloatTensor)

解决方案:首先检查是否正确使用了CUDA

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

inputs = inputs.to(device)

这样就把input这个tensor转换成了CUDA 类型

tensor.to()  这个函数功能是产生一个新的tensor,并不会改变原数据。

测试时,torch.load("训练文件名", map_location="cpu")  即使用cpu作为输入类型

问题四:import torch 时出现问题,key already registered with the same priority

问题原因:之前有安装cpu版本的torch,最近又安装了gpu cuda版本的,两个版本的torch folder名字重合了

解决方案:

1. 将两个版本的torch全部卸载,否则可以成功Import torch,但实际上无法运行

conda uninstall pytorch

pip uninstall torch

pip uninstall torch # 需要跑两次pip uninstall

2. 重新安装所需要的版本

conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge

 问题五:安装其他版本pytorch时出现 torch.cuda.is_available() = False的情况

根本原因:CUDA环境与Torch版本不匹配

解决方案:使用官方推荐的版本进行适配

 要安装pytorch 1.8.0版本,使用官方安装版本

conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=11.1 -c pytorch -c conda-forge

问题6:anaconda3出现CondaHTTPError: HTTP None None for url

解决:检查网络

1、更换官方镜像,换为清华镜像源。并且删掉channels下面的 -defaults一行。

方法1:直接命令行输入以下代码
# 前三条添加清华源路径,第4条移除默认路径,第5条表示从channel中安装包时显示channel的url

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --remove channels defaults
conda config --set show_channel_urls yes

方法2:打开.condarc文件

ssl_verify: true
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
show_channel_urls: true

方法3:直接使用清华源.condarc文件,放在相应目录下。或者复制内容到.condarc中去

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

若不行,尝试在镜像地址后面,加上“/liunx-64” 或者“/win-64”(依照自己系统环境而定)

ssl_verify: true
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
show_channel_urls: true

依旧不行,尝试将地址的https修改成http

ssl_verify: true
channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
show_channel_urls: true

问题7:RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling cublasCreate(handle)

出现原因:代码问题

CPU运行时会报出错误的具体问题,例如数组越界等

GPU运行错误会报出上方错误,但无法确定是什么具体问题

一.在CPU上运行上面代码时,报错是IndexError: index out of range in self

二.在GPU上运行上面代码时,报错是RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling cublasCreate(handle)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值