Ubuntu 16.04 + CUDA 9.0下安装GPU版TensorFlow 1.4(无需从源码编译安装)

因为安装cuda时选择了9.0版本,不能通过 $ pip install tensorflow-gpu 的方法简单地安装GPU版TensorFlow,嫌使用源码编译安装过于麻烦,所以一直在使用CPU版的TensorFlow。知道昨天发现GitHub上有第三方提供的支持cuda 9.0的编译后的whl安装包。

https://github.com/mind/wheels

然后就可以简单地通过pip install的方式安装了。下面是我基于Python 3.6安装TensorFlow 1.4版本的代码,其他版本可类似操作。

#Python 3.6环境
$ export TF_BINARY_URL=https://github.com/mind/wheels/releases/download/tf1.4-gpu/tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl
$ pip install --upgrade $TF_BINARY_URL

另外,这种安装方式需要安装Intel MKL-DNN库组件。详细的安装教程可见:

https://software.intel.com/zh-cn/articles/intel-mkl-dnn-part-1-library-overview-and-installation?language=fr


(注意最后一定要在mkl-dnn/build目录下执行 sudo install make 命令完成安装,否则导入TensorFlow时仍会报错)


为了测试GPU版TensorFlow的效果,用我以下代码在MNIST上训一个MLP进行测试:

# -*- coding: utf-8 -*-

import time
start =time.clock()

from tensorflow.examples.tutorials.mnist import input_data
mnist =input_data.read_data_sets("MNIST_DATA/", one_hot = True)
import tensorflow as tf  
sess = tf.InteractiveSession()

in_units = 784
h1_units = 300
W1 = tf.Variable(tf.truncated_normal(shape = [in_units, h1_units], mean = 0, stddev = 0.1))
b1 = tf.Variable(tf.zeros(shape = [h1_units]))
W2 = tf.Variable(tf.zeros(shape = [h1_units,10]))
b2 = tf.Variable(tf.zeros(shape = [10]))

x = tf.placeholder(dtype = tf.float32, shape = [None, in_units])
keep_prob = tf.placeholder(dtype = tf.float32)

h1 = tf.nn.relu(tf.matmul(x,W1)+b1)
h1_drop = tf.nn.dropout(h1, keep_prob)
y = tf.nn.softmax(tf.matmul(h1_drop, W2)+b2)
y_ =tf.placeholder(tf.float32, [None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
#cross_entropy = (-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[0,1]))

train_step = tf.train.AdagradOptimizer(0.2).minimize(cross_entropy)

tf.global_variables_initializer().run()
for i in range(5000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    train_step.run({x: batch_xs, y_: batch_ys, keep_prob: 0.75})


if_correct = tf.equal(tf.argmax(y,1), tf.argmax(y_, 1))
acc = tf.reduce_mean(tf.cast(if_correct, tf.float32))
print(acc.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
#print(sess.run(y_,feed_dict={y_: mnist.test.labels}))

结果使用GPU版TensorFlow用时10s左右,CPU版TensorFlow用时>60s。(我的配置是i7 6700HQ + GTX 1050)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值