cuda+cudnn+tensorflow+Pycharm配置+踩坑自救

前言

其实我们在很多环境下都可以通过直接用Anaconda来安装tensorflow-gpu,但这样安装有弊端,就是不太好安装tensorflow预览版以及tensorflow 2.0 beta版。如果你想省劲,直接安装Anaconda,并通过如下命令安装稳定版的tensorflow-gpu:

conda install tensorflow-gpu  # gpu版本
conda install tensorflow  #非gpu版本

个人建议我们从安装cuda和cudnn安装开始一直到安装tensorflow-gpu,体验完整的安装步骤,这样安装下来,我们就可以完美体验tensorflow的预览版本和2.0 beta版本了。

 

主要参考官方链接:

https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html

https://docs.nvidia.com/deeplearning/sdk/cudnn-archived/cudnn_742/cudnn-install/index.html

https://tensorflow.google.cn/install/gpu

 

下面开始

个人环境和配置如下:

Ubuntu 18.04.2 LTS + GeForce GTX 1070

 

1. 安装驱动:

系统的“software & update”中,additional drivers处直接“Apply Changes”快捷安装驱动,重启即可。

完美解决通过命令行等方式安装带来的各种重启,循环登陆等问题

安装完成后可以通过nvidia-smi命令检验是否安装成功:

 

2. 检验系统能否支持安装cuda:

https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html

 

3. cuda 安装:

这里注意,不要安装cuda-10.1,当前tensorflow最高支持cuda10.0,如下链接能看到版本对应关系:

https://tensorflow.google.cn/install/source

下面的链接直接是cuda-10.0:

https://developer.nvidia.com/cuda-10.0-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1804&target_type=runfilelocal

按照如下说明安装版本以及对应的补丁:

 

 

4. 安装cudnn:

继续参考官方链接,内容如下:

The following steps describe how to build a cuDNN dependent program. Choose the installation method that meets your environment needs. For example, the tar file installation applies to all Linux platforms. The debian installation package applies to Ubuntu 14.04 and 16.04.

In the following sections:

  • your CUDA directory path is referred to as /usr/local/cuda/

  • your cuDNN download path is referred to as <cudnnpath>

Installing from a Tar File

  1. Navigate to your <cudnnpath> directory containing the cuDNN Tar file.

  2. Unzip the cuDNN package.

    $ tar -xzvf cudnn-9.0-linux-x64-v7.tgz

  3. Copy the following files into the CUDA Toolkit directory, and change the file permissions.

    $ sudo cp cuda/include/cudnn.h /usr/local/cuda/include

    $ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64

    $ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

 Installing from a Debian File

  1. Navigate to your <cudnnpath> directory containing cuDNN Debian file.

  2. Install the runtime library, for example:

    sudo dpkg -i libcudnn7_7.0.3.11-1+cuda9.0_amd64.deb

  3. Install the developer library, for example:

    sudo dpkg -i libcudnn7-dev_7.0.3.11-1+cuda9.0_amd64.deb

  4. Install the code samples and the cuDNN Library User Guide, for example:

    sudo dpkg -i libcudnn7-doc_7.0.3.11-1+cuda9.0_amd64.deb

 Verifying

To verify that cuDNN is installed and is running properly, compile the mnistCUDNN sample located in the /usr/src/cudnn_samples_v7 directory in the debian file.

  1. Copy the cuDNN sample to a writable path.

    $cp -r /usr/src/cudnn_samples_v7/ $HOME

  2. Go to the writable path.

    $ cd $HOME/cudnn_samples_v7/mnistCUDNN

  3. Compile the mnistCUDNN sample.

    $make clean && make

Run the mnistCUDNN sample.

$ ./mnistCUDNN

If cuDNN is properly installed and running on your Linux system, you will see a message similar to the following:

Test passed!

 

5. 出现“no CUDA-capable device is detected”报错解决:

在校验cudnn是否成功时出现了这个错误,百度了一下,需要重新安装合适的驱动。

错误如下:

解决办法

这种问题出现在cudn驱动的问题导致的,使得与显卡版本不匹配,这时候需要更新显卡驱动,重新解决一下。

寻找合适的驱动版本

$ ubuntu-drivers devices

如上图,显示GTX 960M显卡建议的驱动版本是390

c.安装nvidia driver

$ sudo apt-get install nvidia-390

  • 根据自己显卡型号修改

d.安装完成后,重启

$ sudo reboot

e.这时,执行下面的命令,查看驱动的安装状态

  1. $ sudo nvidia-smi

  2. $ sudo nvidia-settings

 

6. 缺少依赖引起的系统启动黑屏问题:

在重新安装显卡驱动之后,出现cuda-dev依赖问题,必须apt -f install 来解决,但是实际上又解决不了,无奈重启一下吧。

重启发现桌面系统进不去了。

没办法啊,百度了一下如何修复,大部分是进入单用户模式去卸载显卡驱动,通过重启,开机过程中点esc键:

选择recovery mode。

进入root,之后使用如下命令卸载:

apt remove --purge nvidia*

竟然显示有依赖问题,不让卸载...

按照提示修复一下依赖:apt -f install ,到dpkg ********cuda***-dev时,竟然终止了!!wtf!!又修复不了依赖,又卸载不了驱动,这怎么玩?!

网上也各种找,找不到有效方法。

最后,重新进入了recovery mode,选择pkg依赖修复选项,这时奇迹发生了,ubuntu自己在疯狂的修复依赖,自己修复好了!!!(修复的时候要接网线,并且在recovery mode里选择选项把network使能)

重启系统,终于全都好了。

输入nvidia-smi,显示如下:

 

重新校验一下cudnn吧!终于全部ok:

Test passed!

 

7. 配置环境变量

在~/.bashrc最后添加如下配置:

# cuda+cudnn
export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH=/usr/local/cuda/lib64
export CUDA_HOME=/usr/local/cuda

 

8. Pycharm配置

Pycharm中的运行环境默认只导入了PATH环境变量,需要在如下地方配置一下其他环境变量:

选择Templates中Python,在Environment variables中添加如下:

;LD_LIBRARY_PATH=/usr/local/cuda/lib64;CUDA_HOME=/usr/local/cuda

这样过添加完后,Pycharm中的运行环境tensorflow就可以找到对应的cuda库文件了。

 

9. 安装tensorflow-gpu:

pip install tensorflow-gpu  # 稳定版

pip install tf-nightly-gpu  # 预览版

pip install tensorflow-gpu==2.0.0-alpha0  # Tensorflow 2.0 beta版

 

 

机器学习与深度学习个人代码仓库:
机器学习和深度学习的各类算法的实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值