Win10安装TensorFlow2.1.0+PyTorch1.3.1纪实

放寒假了,又到了折腾新环境的时间。截止目前(2020年1月15日),TensorFlow的最新版本为2.1.0,PyTorch的最新版本为1.3.1。作为强迫症,也为可能的工程化考虑,希望能够把它们安装在同一个环境中。

基本环境:

  • windows10
  • Python3.6

首先在Anaconda中新建一个名叫cv的Conda环境:

conda create -n cv python=3.6

为了加快安装速度,将conda源换成清华源。.condarc中的内容如下:

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true

要安装PyTorch,注意先看一下可供安装的版本:

conda search pytorch=1.3.1
Loading channels: done
# Name                       Version           Build  Channel
pytorch                        1.3.1 cpu_py36h9f948e0_0  anaconda/pkgs/main
pytorch                        1.3.1 cpu_py36h9f948e0_0  pkgs/main
pytorch                        1.3.1 cpu_py37h9f948e0_0  anaconda/pkgs/main
pytorch                        1.3.1 cpu_py37h9f948e0_0  pkgs/main
pytorch                        1.3.1     py3.5_cpu_0  anaconda/cloud/pytorch
pytorch                        1.3.1 py3.5_cuda101_cudnn7_0  anaconda/cloud/pytorch
pytorch                        1.3.1 py3.5_cuda92_cudnn7_0  anaconda/cloud/pytorch
pytorch                        1.3.1     py3.6_cpu_0  anaconda/cloud/pytorch
pytorch                        1.3.1 py3.6_cuda101_cudnn7_0  anaconda/cloud/pytorch
pytorch                        1.3.1 py3.6_cuda92_cudnn7_0  anaconda/cloud/pytorch
pytorch                        1.3.1     py3.7_cpu_0  anaconda/cloud/pytorch
pytorch                        1.3.1 py3.7_cuda101_cudnn7_0  anaconda/cloud/pytorch
pytorch                        1.3.1 py3.7_cuda92_cudnn7_0  anaconda/cloud/pytorch

注意到PyTorch1.3.1对应的cuda版本是10.1或9.2,因此系统中的cuda版本不能低于这个。
安装PyTorch1.3.1:

conda install pytorch=1.3.1=py3.6_cuda101_cudnn7_0 torchvision cudatoolkit=10.1

由于网络原因,下载部分包时可能会出现HTTP Error,重复上述命令,直到安装成功为止。


接下来安装TensorFlow。先看一下可供安装的版本

conda search tensorflow=2
Loading channels: done
# Name                       Version           Build  Channel
tensorflow                     2.0.0 eigen_py36h457aea3_0  anaconda/pkgs/main
tensorflow                     2.0.0 eigen_py36h457aea3_0  pkgs/main
tensorflow                     2.0.0 eigen_py37hbfc5123_0  anaconda/pkgs/main
tensorflow                     2.0.0 eigen_py37hbfc5123_0  pkgs/main
tensorflow                     2.0.0 gpu_py36hfdd5754_0  anaconda/pkgs/main
tensorflow                     2.0.0 gpu_py36hfdd5754_0  pkgs/main
tensorflow                     2.0.0 gpu_py37h57d29ca_0  anaconda/pkgs/main
tensorflow                     2.0.0 gpu_py37h57d29ca_0  pkgs/main
tensorflow                     2.0.0 mkl_py36h781710d_0  anaconda/pkgs/main
tensorflow                     2.0.0 mkl_py36h781710d_0  pkgs/main
tensorflow                     2.0.0 mkl_py37he1bbcac_0  anaconda/pkgs/main
tensorflow                     2.0.0 mkl_py37he1bbcac_0  pkgs/main

可见conda源中的TensorFlow只更新到2.0.0,因此用pip安装,默认安装最新版本:

pip install tensorflow 

安装完成后import tensorflow as tf时遇到了问题:

ImportError: DLL load failed: The specified module could not be found.

在Github上找到了解决方法:https://github.com/tensorflow/tensorflow/issues/35749

方法一:TensorFlow降到2.0.0 【O__O "…】
方法二:下载并安装:https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads

亲测方法二可行


最后测试一下是否能用cuda:

import torch
print(torch.cuda.is_available())
print(torch.version.cuda)

import tensorflow as tf
print(tf.test.is_gpu_available())

输出:

True
10.1
2020-01-15 20:52:48.968059: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
WARNING:tensorflow:From E:/0WorkSpace/PyCharm_Projects/Kaggle/Bengali/test.py:11: 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-01-15 20:52:51.762533: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-01-15 20:52:51.763974: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-01-15 20:52:51.765366: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties: 
pciBusID: 0000:01:00.0 name: GeForce RTX 2060 computeCapability: 7.5
coreClock: 1.2GHz coreCount: 30 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 312.97GiB/s
2020-01-15 20:52:51.765712: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-01-15 20:52:51.765887: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-01-15 20:52:51.766218: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-01-15 20:52:51.766447: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-01-15 20:52:51.804424: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-01-15 20:52:51.804613: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-01-15 20:52:51.804787: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-01-15 20:52:51.805795: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-01-15 20:52:53.983713: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-01-15 20:52:53.983903: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      0 
2020-01-15 20:52:53.984016: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N 
2020-01-15 20:52:53.986073: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/device:GPU:0 with 4604 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2060, pci bus id: 0000:01:00.0, compute capability: 7.5)
True

完结撒花花~~~

你可能感兴趣的:(工具配置)