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

完結撒花花~~~

發佈了26 篇原創文章 · 獲贊 15 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章