深度学习基础(环境篇)

本文介绍了深度学习的基础环境配置,包括虚拟环境的创建与管理,CUDA和Cudnn的安装,以及Torch的安装步骤。在虚拟环境中遇到的网络访问问题和Ubuntu终端创建问题提供了解决方案。还分享了conda和pip的换源操作,以及如何动态查看GPU显存和处理版本兼容性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

深度学习基础(环境篇)

虚拟环境
  1. 新建虚拟环境

    conda create -n nlp python=3.6
    
    • 报错1:(果真用的久什么问题都能碰到)

      Solving environment: failed
      
      CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/free/noarch/repodata.json.bz2>
      Elapsed: -
      
      An HTTP error occurred when trying to retrieve this URL.
      HTTP errors are often intermittent, and a simple retry will get you on your way.
      
      If your current network has https://www.anaconda.com blocked, please file
      a support request with your network engineering team.
      
      SSLError(MaxRetryError('HTTPSConnectionPool(host=\'repo.anaconda.com\', port=443): Max retries exceeded with url: /pkgs/free/noarch/repodata.json.bz2 (Caused by SSLError(SSLError("bad handshake: Error([(\'SSL routines\', \'ssl3_get_server_certificate\', \'certificate verify failed\')])")))'))
      

      描述:未开代理,使用公司网络/热点均出现以上错误,直接访问(已解决)https://repo.anaconda.com/pkgs/free/noarch/repodata.json.bz2没问题。

      解决:参考链接 本次使用是windows,实测有效。

      1. CMD进入%HOMEPATH%(即C:/USER/),找到.condrac文件,以记事本等任意可以修改的编辑器打开。
      2. - https://… 改成 - http://…ssl_verify: false,保存退出。
    • 报错2:

      Collecting package metadata(current repodata.json): failed
      COndaHTTPErrOr: HTTP 429 TO0 MANY REQUESTS for url <https://mirrors.ustc.edu.cn/anaconda/pkgs/main/linux-64/current repodata.json>Elapsed:00:46.251752
      An HTTP error occurred when trying to retrieve this URL.HTTP errors are often intermittent, and a simple retry will get you on your way
      https://mirrors.ustc.edu.cn/anaconda/pkgs/main/linux-64
      

      描述:ubuntu 20.04终端创建出现问题(已解决)

      解决:参考链接 推测是conda源加入了不知名的URL,现在不能使用了(或者废弃)。于是进入conda镜像源文件将相关废弃源删除。

      1. vim ~/.condarc
      2. 移动光标ddhttps://mirrors.ustc.edu.cn相关源全部删除
  2. 虚拟环境的常见操作

    # 创建python虚拟环境
    conda create -n wkkk python=3.9
    
    # 激活虚拟环境
    activate wkkk
    
    # 查看当前存在哪些虚拟环境
    conda env list
    conda info -e
    
    # 查看安装了哪些包
    conda list
    # 检查更新当前conda
    conda update conda
    # 删除某个虚拟环境
    conda remove --name wkkk --all
    
  3. anaconda镜像源添加

    # 清华源
    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/cloud/msys2
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
    
    # 中科大源
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
    
    # 删除源
    conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
    
    # 设置搜索时显示通道地址
    conda config --set show_channel_urls yes
    
    # 查看已安装的源
    conda config --show-sources
    
  4. 陆续碰到的conda报错

    (nlp) C:\Users\15520>conda install -c huggingface transformers
    Solving environment: failed
    
    InvalidVersionSpecError: Invalid version spec: =2.7
    

    anaconda的版本太低,conda的版本也低==,仍然采用pip,下次一定

  5. pip换源

    • 临时换源

      # 清华源
      pip install xxxx -i https://pypi.tuna.tsinghua.edu.cn/simple
      
      # 阿里源
      pip install xxxx -i https://mirrors.aliyun.com/pypi/simple/
      
      # 腾讯源
      pip install xxxx -i http://mirrors.cloud.tencent.com/pypi/simple
      
      # 豆瓣源
      pip install xxxx -i http://pypi.douban.com/simple/
      
    • 永久换源

      # 清华源
      pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
      
      # 阿里源
      pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
      
      # 腾讯源
      pip config set global.index-url http://mirrors.cloud.tencent.com/pypi/simple
      
      # 豆瓣源
      pip config set global.index-url http://pypi.douban.com/simple/
      
    • 换回原有的源

      pip config unset global.index-url
      
  6. pip常见的操作

    • 清楚缓存

      pip cache purge
      
    • 更新pip和setuptools

      pip install --upgrade pip setuptools
      
CUDA和Cudnn安装

参考之前写的链接

之前写的内容还挺详细OTZ

Torch安装
  1. 查看CUDA版本

    • nvcc-V :显示当前安装的CUDA版本

      外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

    • nvidia-smi:显示当前系统支持的最大CUDA版本

      外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  2. 去torch官网找到对应CUDA一致的Torch版本

    Previous PyTorch Versions | PyTorch

    conda install pytorch==1.10.1 torchvision==0.11.2 torchaudio==0.10.1 -c pytorch
    

    Torch与python好像有对应,python3.6支持到torch10(没记错的话)

  3. 检查torch是否安装成功

    import torch
    
    print(torch.__version__)
    print(torch.version.cuda)
    print(torch.backends.cudnn.version())
    print('gpu',torch.cuda.is_available())
    

使用时的一些Tips

  1. 动态刷新查看当前显存

    watch -n 0.5 nvidia-smi
    
  2. 安装pip install transformers时,python3.6失败,python3,7成功。

  3. 安装torchtext ,需要十分注意与torch的版本,否则会存在不兼容的现象。

    计算公式: pytorch1.a.b,则torchtext版本应该是0.a+1.b

    eg:pytorch版本是1.12.1,对应的torchtext版本应该是0.13.1

    参考链接

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值