TensorFlow2.3.0 开发环境安装

硬件

 i7-10700K+RTX2080S

软件

Win10

Miniconda3-py37_4.8.2-Windows-x86_64

cuda10.1

cudnn7.6.5

tensorflow2.3.0

安装过程

网上看到很多教程都是先把CUDA、cuDNN安装下来再一步步安装。流程没毛病,不过,英伟达的官网就有点恶心,奇慢无比,还时不时的打不开,好不容易打开了网页,下载又下载不下来,要么就一动不动,要么一开始好几M的速度,等你正激动的时候,突然提示下载完成。。。正当你一脸蒙在思考百度网盘的秒传功能怎么在下行端实现的时候,打开下载好的文件一看,X,只有几个字节。。。总之是各种状况,即使“科学上网”也不行。

后来看到有一种方法,不用下载软件,直接用命令行操作就能OK,现分享如下:

1. 安装Miniconda,版本如上面所示。

2. 安装完Miniconda,打开CMD,输入:

conda install cudatoolkit=10.1

以上命令安装CUDA,结果如下:

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: D:\ProgramFiles\miniconda3\envs\tf2

  added / updated specs:
    - cudatoolkit=10.1


The following NEW packages will be INSTALLED:

  cudatoolkit        conda-forge/win-64::cudatoolkit-10.1.243-h3826478_8


Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: -
"By downloading and using the CUDA Toolkit conda packages, you accept the terms and conditions of the CUDA End User License Agreement (EULA): https://docs.nvidia.com/cuda/eula/index.html"

done

 有时,因为网络问题,如果安装不成功,会出现如下提示:

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: D:\ProgramFiles\miniconda3\envs\tf2

  added / updated specs:
    - cudatoolkit=10.1


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    cudatoolkit-10.1.243       |       h3826478_8       378.0 MB  conda-forge
    ------------------------------------------------------------
                                           Total:       378.0 MB

The following NEW packages will be INSTALLED:

  cudatoolkit        conda-forge/win-64::cudatoolkit-10.1.243-h3826478_8


Proceed ([y]/n)? y


Downloading and Extracting Packages
cudatoolkit-10.1.243 | 378.0 MB  | ###########################################################################################################################################1                                                                                                                                                                        |  45%

CondaHTTPError: HTTP 000 CONNECTION FAILED for url 
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.

 以上提示中有一个安装包的链接,可以把这个链接粘贴到迅雷中,让迅雷下载会快很多。下完后将下载好的安装包拷贝到anaconda安装路径下的pkgs路径下,将该安装包的下载路径(也就是上面提到的链接)添加到pkgs路径下的“urls.txt”文件中,再重新安装。

3. 在CMD中继续输入:

conda install cudnn=7.6.5

以上命令安装cuDNN,结果如下:

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: D:\ProgramFiles\miniconda3

  added / updated specs:
    - cudnn=7.6.5


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    cudnn-7.6.5                |       cuda10.1_0       179.1 MB  defaults
    ------------------------------------------------------------
                                           Total:       179.1 MB

The following NEW packages will be INSTALLED:

  cudnn              anaconda/pkgs/main/win-64::cudnn-7.6.5-cuda10.1_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
cudnn-7.6.5          | 179.1 MB  | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

4. 完成上一步后在CMD中继续输入:

pip install tensorflow-gpu==2.3.0 -i https://pypi.douban.com/simple/

以上命令安装tensorflow。只要出现以下信息就是成功了:

Looking in indexes: https://pypi.douban.com/simple/
Collecting tensorflow-gpu==2.3.0
  Downloading https://pypi.doubanio.com/packages/5d/0a/a35e045b095d01340b5495efc4671b82ed00fd582d0633f5251fb1edc0ba/tensorflow_gpu-2.3.0-cp37-cp37m-win_amd64.whl (344.1 MB)
     |████████████████████████████████| 344.1 MB 142 kB/s
Collecting numpy<1.19.0,>=1.16.0
  Downloading https://pypi.doubanio.com/packages/e4/01/7a26148f7de9eb6c27f95b29eba16b7e820827cb9ebaae182d7483e44711/numpy-1.18.5-cp37-cp37m-win_amd64.whl (12.7 MB)
     |████████████████████████████████| 12.7 MB 1.3 MB/s
Collecting opt-einsum>=2.3.2
  Downloading https://pypi.doubanio.com/packages/bc/19/404708a7e54ad2798907210462fd950c3442ea51acc8790f3da48d2bee8b/opt_einsum-3.3.0-py3-none-any.whl (65 kB)
     |████████████████████████████████| 65 kB 907 kB/s
Collecting termcolor>=1.1.0
  Downloading https://pypi.doubanio.com/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz (3.9 kB)
Requirement already satisfied: wheel>=0.26 in d:\programfiles\miniconda3\lib\site-packages (from tensorflow-gpu==2.3.0) (0.34.2)
Collecting wrapt>=1.11.1
  Downloading https://pypi.doubanio.com/packages/82/f7/e43cefbe88c5fd371f4cf0cf5eb3feccd07515af9fd6cf7dbf1d1793a797/wrapt-1.12.1.tar.gz (27 kB)
Collecting tensorboard<3,>=2.3.0
  Downloading https://pypi.doubanio.com/packages/02/83/179c8f76e5716030cc3ee9433721161cfcc1d854e9ba20c9205180bb100a/tensorboard-2.4.0-py3-none-any.whl (10.6 MB)
     |████████████████████████████████| 10.6 MB 1.6 MB/s
Requirement already satisfied: six>=1.12.0 in d:\programfiles\miniconda3\lib\site-packages (from tensorflow-gpu==2.3.0) (1.14.0)
Collecting protobuf>=3.9.2
  Downloading https://pypi.doubanio.com/packages/6b/2e/28425c709c26525998be0b7a91c4090c87c38a1a9644fd43fefaea2e16c0/protobuf-3.13.0-cp37-cp37m-win_amd64.whl (1.0 MB)
     |████████████████████████████████| 1.0 MB 726 kB/s
Collecting scipy==1.4.1
  Downloading https://pypi.doubanio.com/packages/61/51/046cbc61c7607e5ecead6ff1a9453fba5e7e47a5ea8d608cc7036586a5ef/scipy-1.4.1-cp37-cp37m-win_amd64.whl (30.9 MB)
     |████████████████████████████████| 30.9 MB 1.1 MB/s
Collecting astunparse==1.6.3
  Downloading https://pypi.doubanio.com/packages/2b/03/13dde6512ad7b4557eb792fbcf0c653af6076b81e5941d36ec61f7ce6028/astunparse-1.6.3-py2.py3-none-any.whl (12 kB)
Collecting google-pasta>=0.1.8
  Downloading https://pypi.doubanio.com/packages/a3/de/c648ef6835192e6e2cc03f40b19eeda4382c49b5bafb43d88b931c4c74ac/google_pasta-0.2.0-py3-none-any.whl (57 kB)
     |████████████████████████████████| 57 kB 859 kB/s
Collecting gast==0.3.3
  Downloading https://pypi.doubanio.com/packages/d6/84/759f5dd23fec8ba71952d97bcc7e2c9d7d63bdc582421f3cd4be845f0c98/gast-0.3.3-py2.py3-none-any.whl (9.7 kB)
Collecting absl-py>=0.7.0
  Downloading https://pypi.doubanio.com/packages/bc/58/0aa6fb779dc69cfc811df3398fcbeaeefbf18561b6e36b185df0782781cc/absl_py-0.11.0-py3-none-any.whl (127 kB)
     |████████████████████████████████| 127 kB 819 kB/s
Collecting grpcio>=1.8.6
  Downloading https://pypi.doubanio.com/packages/ed/f4/36f90a2091ff9807fc5c85763ffdc717e81deb771acf30173f774711d5db/grpcio-1.33.2-cp37-cp37m-win_amd64.whl (2.5 MB)
     |████████████████████████████████| 2.5 MB 1.7 MB/s
Collecting h5py<2.11.0,>=2.10.0
  Downloading https://pypi.doubanio.com/packages/a1/6b/7f62017e3f0b32438dd90bdc1ff0b7b1448b6cb04a1ed84f37b6de95cd7b/h5py-2.10.0-cp37-cp37m-win_amd64.whl (2.5 MB)
     |████████████████████████████████| 2.5 MB 1.3 MB/s
Collecting tensorflow-gpu-estimator<2.4.0,>=2.3.0
  Downloading https://pypi.doubanio.com/packages/9d/6f/87e922b1dbfa9aa2e79bf150bf05d567eaa4f83bfd329c04834b26b0725e/tensorflow_gpu_estimator-2.3.0-py2.py3-none-any.whl (474 kB)
     |████████████████████████████████| 474 kB 819 kB/s
Collecting keras-preprocessing<1.2,>=1.1.1
  Downloading https://pypi.doubanio.com/packages/79/4c/7c3275a01e12ef9368a892926ab932b33bb13d55794881e3573482b378a7/Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 3.4 MB/s
Collecting google-auth<2,>=1.6.3
  Downloading https://pypi.doubanio.com/packages/1d/60/81e68e70eea91ef05bb00bcdac243d67b61f826c65aaca6961de622dffd7/google_auth-1.23.0-py2.py3-none-any.whl (114 kB)
     |████████████████████████████████| 114 kB 595 kB/s
Requirement already satisfied: requests<3,>=2.21.0 in d:\programfiles\miniconda3\lib\site-packages (from tensorboard<3,>=2.3.0->tensorflow-gpu==2.3.0) (2.22.0)
Collecting tensorboard-plugin-wit>=1.6.0
  Downloading https://pypi.doubanio.com/packages/b6/85/5c5ac0a8c5efdfab916e9c6bc18963f6a6996a8a1e19ec4ad8c9ac9c623c/tensorboard_plugin_wit-1.7.0-py3-none-any.whl (779 kB)
     |████████████████████████████████| 779 kB 469 kB/s
Collecting google-auth-oauthlib<0.5,>=0.4.1
  Downloading https://pypi.doubanio.com/packages/81/67/e2c34bb0628984c7ce71cce6ba6964cb29c418873847fc285f826e032e6e/google_auth_oauthlib-0.4.2-py2.py3-none-any.whl (18 kB)
Collecting werkzeug>=0.11.15
  Downloading https://pypi.doubanio.com/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
     |████████████████████████████████| 298 kB 547 kB/s
Collecting markdown>=2.6.8
  Downloading https://pypi.doubanio.com/packages/ac/ef/24a91ca96efa0d7802dffb83ccc7a3c677027bea19ec3c9ee80be740408e/Markdown-3.3.3-py3-none-any.whl (96 kB)
     |████████████████████████████████| 96 kB 779 kB/s
Requirement already satisfied: setuptools>=41.0.0 in d:\programfiles\miniconda3\lib\site-packages (from tensorboard<3,>=2.3.0->tensorflow-gpu==2.3.0) (45.2.0.post20200210)
Collecting rsa<5,>=3.1.4; python_version >= "3.5"
  Downloading https://pypi.doubanio.com/packages/1c/df/c3587a667d6b308fadc90b99e8bc8774788d033efcc70f4ecaae7fad144b/rsa-4.6-py3-none-any.whl (47 kB)
     |████████████████████████████████| 47 kB 1.1 MB/s
Collecting cachetools<5.0,>=2.0.0
  Downloading https://pypi.doubanio.com/packages/cd/5c/f3aa86b6d5482f3051b433c7616668a9b96fbe49a622210e2c9781938a5c/cachetools-4.1.1-py3-none-any.whl (10 kB)
Collecting pyasn1-modules>=0.2.1
  Downloading https://pypi.doubanio.com/packages/95/de/214830a981892a3e286c3794f41ae67a4495df1108c3da8a9f62159b9a9d/pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
     |████████████████████████████████| 155 kB 656 kB/s
Requirement already satisfied: idna<2.9,>=2.5 in d:\programfiles\miniconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<3,>=2.3.0->tensorflow-gpu==2.3.0) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in d:\programfiles\miniconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<3,>=2.3.0->tensorflow-gpu==2.3.0) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in d:\programfiles\miniconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<3,>=2.3.0->tensorflow-gpu==2.3.0) (1.25.8)
Requirement already satisfied: certifi>=2017.4.17 in d:\programfiles\miniconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<3,>=2.3.0->tensorflow-gpu==2.3.0) (2020.6.20)
Collecting requests-oauthlib>=0.7.0
  Downloading https://pypi.doubanio.com/packages/a3/12/b92740d845ab62ea4edf04d2f4164d82532b5a0b03836d4d4e71c6f3d379/requests_oauthlib-1.3.0-py2.py3-none-any.whl (23 kB)
Collecting importlib-metadata; python_version < "3.8"
  Downloading https://pypi.doubanio.com/packages/6d/6d/f4bb28424bc677bce1210bc19f69a43efe823e294325606ead595211f93e/importlib_metadata-2.0.0-py2.py3-none-any.whl (31 kB)
Collecting pyasn1>=0.1.3
  Downloading https://pypi.doubanio.com/packages/62/1e/a94a8d635fa3ce4cfc7f506003548d0a2447ae76fd5ca53932970fe3053f/pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
     |████████████████████████████████| 77 kB 871 kB/s
Collecting oauthlib>=3.0.0
  Downloading https://pypi.doubanio.com/packages/05/57/ce2e7a8fa7c0afb54a0581b14a65b56e62b5759dbc98e80627142b8a3704/oauthlib-3.1.0-py2.py3-none-any.whl (147 kB)
     |████████████████████████████████| 147 kB 595 kB/s
Collecting zipp>=0.5
  Downloading https://pypi.doubanio.com/packages/41/ad/6a4f1a124b325618a7fb758b885b68ff7b058eec47d9220a12ab38d90b1f/zipp-3.4.0-py3-none-any.whl (5.2 kB)
Building wheels for collected packages: termcolor, wrapt
  Building wheel for termcolor (setup.py) ... done
  Created wheel for termcolor: filename=termcolor-1.1.0-py3-none-any.whl size=4835 sha256=4b032826abf6ca65e8fe1b8b2edf3e13d123190d334c884c3171ac3909ecefb4
  Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\3e\22\6a\f8d6117d6e8d15726ab5fab06e328ecefb5d0dcce467f7c95b
  Building wheel for wrapt (setup.py) ... done
  Created wheel for wrapt: filename=wrapt-1.12.1-py3-none-any.whl size=19558 sha256=0925b80615249843bd7e5221ba884a1f361abbaf93f3c016169242e564e4e92a
  Stored in directory: c:\users\user\appdata\local\pip\cache\wheels\4b\d6\4a\8f624bbcf3a01720a9fe04df85631ff62b56ed6f19be2a90f9
Successfully built termcolor wrapt
Installing collected packages: numpy, opt-einsum, termcolor, wrapt, grpcio, pyasn1, rsa, cachetools, pyasn1-modules, google-auth, protobuf, tensorboard-plugin-wit, oauthlib, requests-oauthlib, google-auth-oauthlib, absl-py, werkzeug, zipp, importlib-metadata, markdown, tensorboard, scipy, astunparse, google-pasta, gast, h5py, tensorflow-gpu-estimator, keras-preprocessing, tensorflow-gpu
Successfully installed absl-py-0.11.0 astunparse-1.6.3 cachetools-4.1.1 gast-0.3.3 google-auth-1.23.0 google-auth-oauthlib-0.4.2 google-pasta-0.2.0 grpcio-1.33.2 h5py-2.10.0 importlib-metadata-2.0.0 keras-preprocessing-1.1.2 markdown-3.3.3 numpy-1.18.5 oauthlib-3.1.0 opt-einsum-3.3.0 protobuf-3.13.0 pyasn1-0.4.8 pyasn1-modules-0.2.8 requests-oauthlib-1.3.0 rsa-4.6 scipy-1.4.1 tensorboard-2.4.0 tensorboard-plugin-wit-1.7.0 tensorflow-gpu-2.3.0 tensorflow-gpu-estimator-2.3.0 termcolor-1.1.0 werkzeug-1.0.1 wrapt-1.12.1 zipp-3.4.0

信息比较多,不过看最后3行就行了,提示了Successfully 的,就是成功了。

5. 至此,已经安装成功了,我们来测试一下。

先在CMD里输入python,进入python的环境

Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

Type "help", "copyright", "credits" or "license" for more information.
>>>

再输入import tensorflow as tf

2020-11-13 20:53:51.187590: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
>>>

说明tensorflow 已经安装好了。然后输入tf.test.is_gpu_available()

WARNING:tensorflow:From :1: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2020-11-13 20:57:27.195594: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-11-13 20:57:27.206405: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2cf33638160 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-11-13 20:57:27.209233: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-11-13 20:57:27.211972: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-11-13 20:57:27.243232: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce RTX 2080 SUPER computeCapability: 7.5
coreClock: 1.845GHz coreCount: 48 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 462.00GiB/s
2020-11-13 20:57:27.247291: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudart64_101.dll
2020-11-13 20:57:27.255915: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cublas64_10.dll
2020-11-13 20:57:27.263649: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cufft64_10.dll
2020-11-13 20:57:27.267925: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library curand64_10.dll
2020-11-13 20:57:27.276118: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusolver64_10.dll
2020-11-13 20:57:27.281221: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cusparse64_10.dll
2020-11-13 20:57:27.294877: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dll
2020-11-13 20:57:27.296893: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1858] Adding visible gpu devices: 0
2020-11-13 20:57:27.816906: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-11-13 20:57:27.819919: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]      0
2020-11-13 20:57:27.821177: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1276] 0:   N
2020-11-13 20:57:27.822583: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/device:GPU:0 with 6190 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2080 SUPER, pci bus id: 0000:01:00.0, compute capability: 7.5)
2020-11-13 20:57:27.828799: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2cf60ae0ab0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-11-13 20:57:27.831224: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce RTX 2080 SUPER, Compute Capability 7.5
True
>>>

说明GPU也可以用。

总结

以上步骤,比起上英伟达官网下安装包的磕磕碰碰,可以说是丝般顺滑,不过在执行第二步之前,最好是先给conda配置几个国内的源,当然如果能“科学上网”,应该会更好一些。如果2-4步之间有哪步因各种原因安装出错,先不要进行下一步,重复执行一下当前命令,一般都没什么问题。我在第2步的时候第一次执行就出错了,再执行一次就好了。

 

补充

1. 最近,又装了一台 i7-7700 + GT 730的机器,在进行到第二步的时候,死活装不上。遇到这个情况呢,直接下载一个CUDA的安装包下来安装就行了。安装程序已经把环境变量加进去了,注销或重启一下就可以用。先走完3、4步再安装CUDA也是可以的。

2. 配置国内源。

    在“C:\Users\xx”路径下有一个“.condarc”文件,在channels后面加入以下源。

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

 

你可能感兴趣的:(深度学习,tensorflow,深度学习,神经网络,机器学习)