ubuntu18.04 tensorflow-gpu1.13+cuda10.0+cudnn7.5安装配置

ubuntu tensorflow-gpu+cuda+cudnn安装配置

  • tensorflow笔记
    • 系统及配置
    • 安装CUDA
    • 安装CUDNN
    • 安装tensorflow

tensorflow笔记

系统及配置

系统为Ubuntu18.04,GPU为GeForce 940M
推荐安装最新版本驱动。Nvidia已经提供了CUDA、cudnn的deb文件或者runfile,极不推荐使用解压安装。

安装CUDA

须知:CUDA与cudnn、tensorflow有严格的版本对应关系。
这里安装CUDA 10.0,cudnn7.5,tensorflow-gpu 1.13.1
推荐安装CUDA 10.0
CUDA下载链接
ubuntu18.04 tensorflow-gpu1.13+cuda10.0+cudnn7.5安装配置_第1张图片
推荐下载runfile,下载后添加可执行权限,运行。如已安装Nvidia驱动,可不选驱动安装。安照提示安装即可。推荐安装samples用来测试是否安装成功。
添加环境变量:

sudo gedit ~/.bashrc

在文件最后添加

export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

然后设置环境变量和动态链接库,在命令行输入:

sudo gedit /etc/profile

在打开的文件末尾加入:

export PATH=/usr/local/cuda/bin:$PATH

保存之后,创建链接文件:

sudo gedit /etc/ld.so.conf.d/cuda.conf

在打开的文件中添加如下语句:

/usr/local/cuda/lib64

然后执行,使链接立即生效

sudo ldconfig

测试CUDA

cd /usr/local/cuda-10.0/samples/
cd 1_Utilities/bandwidthTest/
sudo make
./bandwidthTest

编译无错误,输出如下:
ubuntu18.04 tensorflow-gpu1.13+cuda10.0+cudnn7.5安装配置_第2张图片

安装CUDNN

CUDNN下载
ubuntu18.04 tensorflow-gpu1.13+cuda10.0+cudnn7.5安装配置_第3张图片
需要注册,下载Rutime Library、Developer Library、Code Samples(Deb)并按此顺序安装。

安装tensorflow

安装tensorflow与tensorflow-gpu,版本均为1.13.1,推荐使用Pycharm。
在这里插入图片描述
测试:

# coding=utf-8

import tensorflow as tf
from numpy.random import RandomState

batch_size = 8

w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))

x = tf.placeholder(tf.float32, shape=(None, 2), name='x-input')
y_ = tf.placeholder(tf.float32, shape=(None, 1), name='y-input')

a = tf.matmul(x, w1)
y = tf.matmul(a, w2)

cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)

rdm = RandomState(1)
dataset_size = 128
X = rdm.rand(dataset_size, 2)
Y = [[int(x1 + x2 < 1)] for (x1, x2) in X]

with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    print(sess.run(w1))
    print(sess.run(w2))

    STEPS = 5000
    for i in range(STEPS):
        start = (i * batch_size) % dataset_size
        end = min(start + batch_size, dataset_size)

        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
        if i % 1000 == 0:
            total_cross_entropy = sess.run(cross_entropy, feed_dict={x: X, y_: Y})
            print("After %d training step(s),cross entropy on all data is %g" % (i, total_cross_entropy))
    print(sess.run(w1))
    print(sess.run(w2))

运行之:
ubuntu18.04 tensorflow-gpu1.13+cuda10.0+cudnn7.5安装配置_第4张图片
可以看到启用了GPU。

你可能感兴趣的:(tensorflow)