Anaconda搭建深度学习环境py 3.7:tensorflow-gpu2.3.0、pytorch1.12.1_gpu版本;(使用conda下载cuda和cudnn);配置环境经验总结

2024-07-30 08:15:50 浏览数 (2)

前言:

本文使用conda下载cuda和cudnn,直接安装到虚拟环境,免去配置环境变量等操作且节省C盘空间。若想单独下载CUDA Toolkit及cudnn,可参照该文章:

【2022超详细版】Win10安装cuda(10.1、11.7) cuDNN(7.6.5、8.5.0) tensorflow(gpu版) pytorch(gpu版)_QomolangmaH的博客-CSDN博客

https://blog.csdn.net/m0_63834988/article/details/128781572?spm=1001.2014.3001.5501

此外,配置环境会遇到n种报错,故本文下载版本及操作顺序不能乱。

若想安装其它版本,亦可参照本文提供的一些常见报错(我遇到的……)解决方案,或许大概可能有些参考价值吧……

纯个人经验分享,仅供参考。若有不当之处,欢迎评论(虽然理论上,即使留言了我也不知道如何解决)

0. 前提条件

(a). 安装Anaconda pycharm

win11 安装 Anaconda(2022.10) pycharm(2022.3/2023.1.4) 配置虚拟环境_QomolangmaH的博客-CSDN博客

https://blog.csdn.net/m0_63834988/article/details/128693741?spm=1001.2014.3001.5501

(b). Anaconda更改虚拟环境安装路径、创建虚拟环境

【2023】Anaconda更改虚拟环境安装路径 创建虚拟环境_anaconda修改虚拟环境安装位置_QomolangmaH的博客-CSDN博客

https://blog.csdn.net/m0_63834988/article/details/128691854?spm=1001.2014.3001.5501

1. 创建虚拟环境(Python 3.7)

  • 创建名为"MLgpu"的新环境,并指定使用Python 3.7版本
代码语言:javascript复制
conda create -n MLgpu python=3.7
  • 激活虚拟环境
代码语言:javascript复制
conda activate MLgpu

2. 安装tensorflow-gpu

(a). 常见版本匹配报错

代码语言:javascript复制
ensorflow 2.3.0 requires scipy==1.4.1, but you have scipy 1.7.3 which is incompatible.
tensorflow 2.3.0 requires tensorflow-estimator<2.4.0,>=2.3.0, but you have tensorflow-estimator 2.5.0 which is incompatible.
tensorflow-gpu 2.3.0 requires scipy==1.4.1, but you have scipy 1.7.3 which is incompatible.

scipy 和 tensorflow-estimator 版本与 TensorFlow 2.3.0 和 TensorFlow GPU 2.3.0 不兼容,可以尝试如下两种方法:

  • 升级 tensorFlow 和 tensorflow-gpu 版本
  • 降级安装 sciPy 和 tensorflow-estimator

若选择第1种方法,可能会导致其它库的版本兼容性问题:如果有其他库依赖于 tensorflow-estimator 2.3.0 或 scipy 1.4.1 的特定版本,可能继续报错……然后继续改……

(b). 下载tensorflow-gpu2.3.0版本

须使用pip下载,实践表明conda下载后,安装pytorch会报错

代码语言:javascript复制
pip install tensorflow-gpu==2.3.0
代码语言:javascript复制
Successfully installed MarkupSafe-2.1.2 absl-py-1.4.0 astunparse-1.6.3 
cachetools-5.3.0 certifi-2022.12.7 charset-normalizer-3.0.1 gast-0.3.3 
google-auth-2.16.0 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.51.1 
h5py-2.10.0 idna-3.4 importlib-metadata-6.0.0 keras-preprocessing-1.1.2 
markdown-3.4.1 numpy-1.18.5 oauthlib-3.2.2 opt-einsum-3.3.0 protobuf-3.20.3 
pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-2.28.2 requests-oauthlib-1.3.1 
rsa-4.9 scipy-1.4.1 six-1.16.0 tensorboard-2.11.2 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.1 tensorflow-gpu-2.3.0 tensorflow-gpu-estimator-2.3.0 
termcolor-2.2.0 typing-extensions-4.4.0 urllib3-1.26.14 werkzeug-2.2.2 wrapt-1.14.1 
zipp-3.12.0

(c). 根据tensorflow-gpu下载相应的cudnn7.6.5版本

使用如下conda会同时下载cudnn cudatoolkit!!!

代码语言:javascript复制
conda install cudnn==7.6.5

(d). 检验

代码语言:javascript复制
import tensorflow as tf


print('GPU', tf.config.list_physical_devices('GPU'))
a = tf.constant(3.)
print(a * a)

(e). 报错及解决方案

下面的报错不知道是哪个版本遇到的了,最好不要遇见……

报错1

Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found

代码语言:javascript复制
2023-02-03 20:23:07.260374: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found

且检测不出gpu,输出如下

代码语言:javascript复制
GPU []
tf.Tensor(9.0, shape=(), dtype=float32)

原因:深度学习框架 TensorFlow 在尝试使用 CUDA 模块时出现版本匹配问题

解决方法:找到如下文件,复制粘贴并重命名为cudart64_101.dll

输出

代码语言:javascript复制
GPU [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
报错2

AttributeError: module 'tensorflow.compat.v2' has no attribute '__internal__'

keras与tensorflow版本不兼容

代码语言:javascript复制
 conda install keras==2.4.3
代码语言:javascript复制
(RL) PS C:UsersLenovo> pip install keras==2.4.3
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting keras==2.4.3
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/44/e1/dc0757b20b56c980b5553c1b5c4c32d378c7055ab7bfa92006801ad359ab/Keras-2.4.3-py2.py3-none-any.whl (36 kB)
Collecting pyyaml
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/d1/c0/4fe04181b0210ee2647cfbb89ecd10a36eef89f10d8aca6a192c201bbe58/PyYAML-6.0-cp37-cp37m-win_amd64.whl (153 kB)
Requirement already satisfied: h5py in e:softwareanaconda3envsrllibsite-packages (from keras==2.4.3) (2.10.0)
Requirement already satisfied: scipy>=0.14 in e:softwareanaconda3envsrllibsite-packages (from keras==2.4.3) (1.4.1)
Requirement already satisfied: numpy>=1.9.1 in e:softwareanaconda3envsrllibsite-packages (from keras==2.4.3) (1.18.5)
Requirement already satisfied: six in e:softwareanaconda3envsrllibsite-packages (from h5py->keras==2.4.3) (1.16.0)
Installing collected packages: pyyaml, keras
Successfully installed keras-2.4.3 pyyaml-6.0

3. 安装pytorch(gpu版本)

(a). 官方

代码语言:javascript复制
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=10.2 -c pytorch

(b). 清华源

(先前以添加清华源,详细步骤请参照文初链接)

代码语言:javascript复制
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1

(cudatoolkit=10.2在安装tensorflow-gpu时安装过了)

代码语言:javascript复制
 environment location: E:Softwareanaconda3envsRLgpu

  added / updated specs:
    - pytorch==1.12.1
    - torchaudio==0.12.1
    - torchvision==0.13.1


The following NEW packages will be INSTALLED:

  blas               anaconda/cloud/conda-forge/win-64::blas-2.116-mkl
  blas-devel         anaconda/cloud/conda-forge/win-64::blas-devel-3.9.0-16_win64_mkl
  brotlipy           anaconda/cloud/conda-forge/win-64::brotlipy-0.7.0-py37hcc03f2d_1004
  certifi            anaconda/cloud/conda-forge/noarch::certifi-2022.12.7-pyhd8ed1ab_0
  cffi               anaconda/cloud/conda-forge/win-64::cffi-1.15.1-py37ha95fbe2_1
  charset-normalizer anaconda/cloud/conda-forge/noarch::charset-normalizer-2.1.1-pyhd8ed1ab_0
  cryptography       anaconda/cloud/conda-forge/win-64::cryptography-38.0.2-py37h953a470_1
  freetype           anaconda/cloud/conda-forge/win-64::freetype-2.12.1-h546665d_1
  idna               anaconda/cloud/conda-forge/noarch::idna-3.4-pyhd8ed1ab_0
  intel-openmp       anaconda/cloud/conda-forge/win-64::intel-openmp-2023.0.0-h57928b3_25922
  jpeg               anaconda/cloud/conda-forge/win-64::jpeg-9e-h8ffe710_2
  lcms2              anaconda/cloud/conda-forge/win-64::lcms2-2.14-h90d422f_0
  lerc               anaconda/cloud/conda-forge/win-64::lerc-4.0.0-h63175ca_0
  libblas            anaconda/cloud/conda-forge/win-64::libblas-3.9.0-16_win64_mkl
  libcblas           anaconda/cloud/conda-forge/win-64::libcblas-3.9.0-16_win64_mkl
  libdeflate         anaconda/cloud/conda-forge/win-64::libdeflate-1.14-hcfcfb64_0
  libhwloc           anaconda/cloud/conda-forge/win-64::libhwloc-2.8.0-h039e092_1
  libiconv           anaconda/cloud/conda-forge/win-64::libiconv-1.17-h8ffe710_0
  liblapack          anaconda/cloud/conda-forge/win-64::liblapack-3.9.0-16_win64_mkl
  liblapacke         anaconda/cloud/conda-forge/win-64::liblapacke-3.9.0-16_win64_mkl
  libpng             anaconda/cloud/conda-forge/win-64::libpng-1.6.39-h19919ed_0
  libtiff            anaconda/cloud/conda-forge/win-64::libtiff-4.4.0-hc4f729c_5
  libuv              anaconda/cloud/conda-forge/win-64::libuv-1.44.2-h8ffe710_0
  libwebp-base       anaconda/cloud/conda-forge/win-64::libwebp-base-1.2.4-h8ffe710_0
  libxcb             anaconda/cloud/conda-forge/win-64::libxcb-1.13-hcd874cb_1004
  libxml2            anaconda/cloud/conda-forge/win-64::libxml2-2.10.3-hc3477c8_0
  libzlib            anaconda/cloud/conda-forge/win-64::libzlib-1.2.13-hcfcfb64_4
  m2w64-gcc-libgfor~ anaconda/cloud/conda-forge/win-64::m2w64-gcc-libgfortran-5.3.0-6
  m2w64-gcc-libs     anaconda/cloud/conda-forge/win-64::m2w64-gcc-libs-5.3.0-7
  m2w64-gcc-libs-co~ anaconda/cloud/conda-forge/win-64::m2w64-gcc-libs-core-5.3.0-7
  m2w64-gmp          anaconda/cloud/conda-forge/win-64::m2w64-gmp-6.1.0-2
  m2w64-libwinpthre~ anaconda/cloud/conda-forge/win-64::m2w64-libwinpthread-git-5.0.0.4634.697f757-2
  mkl                anaconda/cloud/conda-forge/win-64::mkl-2022.1.0-h6a75c08_874
  mkl-devel          anaconda/cloud/conda-forge/win-64::mkl-devel-2022.1.0-h57928b3_875
  mkl-include        anaconda/cloud/conda-forge/win-64::mkl-include-2022.1.0-h6a75c08_874
  msys2-conda-epoch  anaconda/cloud/conda-forge/win-64::msys2-conda-epoch-20160418-1
  numpy              anaconda/cloud/conda-forge/win-64::numpy-1.21.6-py37h2830a78_0
  openjpeg           anaconda/cloud/conda-forge/win-64::openjpeg-2.5.0-hc9384bd_1
  pillow             anaconda/cloud/conda-forge/win-64::pillow-9.2.0-py37h42a8222_2
  pthread-stubs      anaconda/cloud/conda-forge/win-64::pthread-stubs-0.4-hcd874cb_1001
  pthreads-win32     anaconda/cloud/conda-forge/win-64::pthreads-win32-2.9.1-hfa6e2cd_3
  pycparser          anaconda/cloud/conda-forge/noarch::pycparser-2.21-pyhd8ed1ab_0
  pyopenssl          anaconda/cloud/conda-forge/noarch::pyopenssl-23.0.0-pyhd8ed1ab_0
  pysocks            anaconda/cloud/conda-forge/win-64::pysocks-1.7.1-py37h03978a9_5
  python_abi         anaconda/cloud/conda-forge/win-64::python_abi-3.7-3_cp37m
  pytorch            anaconda/cloud/pytorch/win-64::pytorch-1.12.1-py3.7_cpu_0
  pytorch-mutex      anaconda/cloud/pytorch/noarch::pytorch-mutex-1.0-cpu
  requests           anaconda/cloud/conda-forge/noarch::requests-2.28.2-pyhd8ed1ab_0
  tbb                anaconda/cloud/conda-forge/win-64::tbb-2021.7.0-h91493d7_1
  tk                 anaconda/cloud/conda-forge/win-64::tk-8.6.12-h8ffe710_0
  torchaudio         anaconda/cloud/pytorch/win-64::torchaudio-0.12.1-py37_cpu
  torchvision        anaconda/cloud/pytorch/win-64::torchvision-0.13.1-py37_cpu
  typing_extensions  anaconda/cloud/conda-forge/noarch::typing_extensions-4.4.0-pyha770c72_0
  urllib3            anaconda/cloud/conda-forge/noarch::urllib3-1.26.14-pyhd8ed1ab_0
  win_inet_pton      anaconda/cloud/conda-forge/noarch::win_inet_pton-1.1.0-pyhd8ed1ab_6
  xorg-libxau        anaconda/cloud/conda-forge/win-64::xorg-libxau-1.0.9-hcd874cb_0
  xorg-libxdmcp      anaconda/cloud/conda-forge/win-64::xorg-libxdmcp-1.1.3-hcd874cb_0
  xz                 anaconda/cloud/conda-forge/win-64::xz-5.2.6-h8d14728_0
  zstd               anaconda/cloud/conda-forge/win-64::zstd-1.5.2-h12be248_6

4. 安装其它库

sklearn

matplotlib

代码语言:javascript复制
 conda install matplotlib
代码语言:javascript复制
The following NEW packages will be INSTALLED:

  brotli             anaconda/cloud/conda-forge/win-64::brotli-1.0.9-hcfcfb64_8
  brotli-bin         anaconda/cloud/conda-forge/win-64::brotli-bin-1.0.9-hcfcfb64_8
  bzip2              anaconda/cloud/conda-forge/win-64::bzip2-1.0.8-h8ffe710_4
  cycler             anaconda/cloud/conda-forge/noarch::cycler-0.11.0-pyhd8ed1ab_0
  fonttools          anaconda/pkgs/main/noarch::fonttools-4.25.0-pyhd3eb1b0_0
  freetype           anaconda/cloud/conda-forge/win-64::freetype-2.12.1-h546665d_1
  gettext            anaconda/cloud/conda-forge/win-64::gettext-0.21.1-h5728263_0
  glib               anaconda/cloud/conda-forge/win-64::glib-2.74.1-h12be248_1
  glib-tools         anaconda/cloud/conda-forge/win-64::glib-tools-2.74.1-h12be248_1
  gst-plugins-base   anaconda/cloud/conda-forge/win-64::gst-plugins-base-1.21.3-h001b923_1
  gstreamer          anaconda/cloud/conda-forge/win-64::gstreamer-1.21.3-h6b5321d_1
  icu                anaconda/cloud/conda-forge/win-64::icu-70.1-h0e60522_0
  jpeg               anaconda/cloud/conda-forge/win-64::jpeg-9e-h8ffe710_2
  kiwisolver         anaconda/cloud/conda-forge/win-64::kiwisolver-1.4.4-py37h8c56517_0
  krb5               anaconda/cloud/conda-forge/win-64::krb5-1.20.1-heb0366b_0
  lerc               anaconda/cloud/conda-forge/win-64::lerc-4.0.0-h63175ca_0
  libbrotlicommon    anaconda/cloud/conda-forge/win-64::libbrotlicommon-1.0.9-hcfcfb64_8
  libbrotlidec       anaconda/cloud/conda-forge/win-64::libbrotlidec-1.0.9-hcfcfb64_8
  libbrotlienc       anaconda/cloud/conda-forge/win-64::libbrotlienc-1.0.9-hcfcfb64_8
  libclang           anaconda/cloud/conda-forge/win-64::libclang-15.0.7-default_h77d9078_0
  libclang13         anaconda/cloud/conda-forge/win-64::libclang13-15.0.7-default_h77d9078_0
  libdeflate         anaconda/cloud/conda-forge/win-64::libdeflate-1.17-hcfcfb64_0
  libffi             anaconda/cloud/conda-forge/win-64::libffi-3.4.2-h8ffe710_5
  libglib            anaconda/cloud/conda-forge/win-64::libglib-2.74.1-he8f3873_1
  libogg             anaconda/cloud/conda-forge/win-64::libogg-1.3.4-h8ffe710_1
  libpng             anaconda/cloud/conda-forge/win-64::libpng-1.6.39-h19919ed_0
  libtiff            anaconda/cloud/conda-forge/win-64::libtiff-4.5.0-hf8721a0_2
  libvorbis          anaconda/cloud/conda-forge/win-64::libvorbis-1.3.7-h0e60522_0
  libwebp            anaconda/cloud/conda-forge/win-64::libwebp-1.2.4-hcfcfb64_1
  libwebp-base       anaconda/cloud/conda-forge/win-64::libwebp-base-1.2.4-h8ffe710_0
  matplotlib         anaconda/cloud/conda-forge/win-64::matplotlib-3.5.3-py37h03978a9_2
  matplotlib-base    anaconda/cloud/conda-forge/win-64::matplotlib-base-3.5.3-py37hbaab90a_2
  munkres            anaconda/cloud/conda-forge/noarch::munkres-1.1.4-pyh9f0ad1d_0
  packaging          anaconda/cloud/conda-forge/noarch::packaging-23.0-pyhd8ed1ab_0
  pcre2              anaconda/cloud/conda-forge/win-64::pcre2-10.40-h17e33f8_0
  pillow             anaconda/pkgs/main/win-64::pillow-9.3.0-py37hd77b12b_2
  ply                anaconda/cloud/conda-forge/noarch::ply-3.11-py_1
  pyparsing          anaconda/cloud/conda-forge/noarch::pyparsing-3.0.9-pyhd8ed1ab_0
  pyqt               anaconda/cloud/conda-forge/win-64::pyqt-5.15.7-py37h35e25fb_0
  pyqt5-sip          anaconda/cloud/conda-forge/win-64::pyqt5-sip-12.11.0-py37hf2a7229_0
  python-dateutil    anaconda/cloud/conda-forge/noarch::python-dateutil-2.8.2-pyhd8ed1ab_0
  qt-main            anaconda/cloud/conda-forge/win-64::qt-main-5.15.6-h9580fe5_6
  sip                anaconda/cloud/conda-forge/win-64::sip-6.6.2-py37hf2a7229_0
  tk                 anaconda/cloud/conda-forge/win-64::tk-8.6.12-h8ffe710_0
  toml               anaconda/cloud/conda-forge/noarch::toml-0.10.2-pyhd8ed1ab_0
  tornado            anaconda/cloud/conda-forge/win-64::tornado-6.2-py37hcc03f2d_0
  xz                 anaconda/cloud/conda-forge/win-64::xz-5.2.6-h8d14728_0
  zstd               anaconda/cloud/conda-forge/win-64::zstd-1.5.2-h12be248_6

keras-metrics

代码语言:javascript复制
pip install keras-metrics

Successfully installed Keras-2.11.0 keras_metrics-1.1.0

5. 配置环境总结

配置环境的痛苦……以下言论没有任何科学依据,仅供娱乐

  1. 我习惯先用conda配置
  2. pip不行就conda,conda报错就pip,核心思想:“瞎配”
  3. 版本不匹配一言不合就降版本
  4. 降版本:conda、pip一起来回删改会有奇迹发生(比如一个环境中同时存在好几个版本的numpy,但最后代码顺利运行)
  5. 自己配:一天配不完就配两天,两天配不完就配三天……整个十天八天没结果就放弃吧……建议寻找能人异士
  6. 本文谨针对requirement.txt完全不好用的情况

0 人点赞