搭建环境(四)Ubuntu14.04 安装TensorFlow1.3.0

来源:互联网 发布:淘宝下载手机版 编辑:程序博客网 时间:2024/05/19 02:03

安装完CUDA以及cudnn就剩最后一步安装TensorFlow了。官网中给出了几种安装方式:
1.基于pip的安装
2.基于 VirtualEnv 的安装
3.基于 Docker 的安装
4.基于 Anaconda 的安装
我是使用Anaconda 安装成功的,所以这里仅介绍基于 Anaconda 的安装方式,其他安装方式请参见官方文档。

1.安装Anaconda

从anaconda官网https://www.continuum.io/downloads上下载linux版本的安装文件,下载速度比较慢,推荐使用国内的清华镜像,传送门:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
这里写图片描述
Anaconda是在Python2环境,Anaconda3是在Python3环境,选择版本下载完成后,得到Anaconda3-4.x.x-Linux-x86_64.sh安装文件。
打开命令行,输入如下命令,

bash Anaconda3-4.x.x-Linux-x86_64.sh

阅读license,一步步回车阅读文档,出来提示输入yes回车,设置安装路径,直接输入回车,使用默认安装路径,最后提示是否将Anaconda的安装路径添加到环境变量中,输入yes,否则后面需要自行手动添加。(如果anaconda安装后出现问题,可以回来排查一下环境变量有没有正确添加)
安装完成后,打开新的terminal,输入Jupyter notebook,如果弹出来浏览器页面,则jupyter被成功安装了
这里写图片描述
这里写图片描述

2.利用anaconda安装tensorflow

a.建立一个 conda 计算环境

conda create -n tensorflow python=3.5#我使用的是Python3.5

b.激活 conda环境

source activate tensorflow

这里写图片描述

c.安装tensorflow

pip install --ignore-installed --upgrade tfBinaryURL#tfBinaryURL 是tensorflow的安装包地址,官网中提供了各种版本的下载地址,例如,我使用的是Python3,5安装tensorflow1.3.0-GPU版本,则输入:pip install --ignore-installed --upgrade \https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp35-cp35m-linux_x86_64.whl

官网地址传送https://www.tensorflow.org/install/install_linux#the_url_of_the_tensorflow_python_package
安装成功后,测试一下tensorflow是否安装成功
(1) 激活conda环境(每次使用 TensorFlow 的时候需要激活 conda 环境,)
(2)进入python
(3)import tensorflow as tf
如果正常不报错的话,就证明安装成功了。
这里写图片描述

3.测试程序

安装成功后,测试一个小程序来开启你的tensorflow之路吧、

from tensorflow.examples.tutorials.mnist import input_dataimport tensorflow as tfmnist = input_data.read_data_sets("MNIST_data/", one_hot=True)sess = tf.InteractiveSession()def weight_variable(shape):  initial = tf.truncated_normal(shape, stddev=0.1)  return tf.Variable(initial)def bias_variable(shape):  initial = tf.constant(0.1, shape=shape)  return tf.Variable(initial)def conv2d(x, W):  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')def max_pool_2x2(x):  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],                        strides=[1, 2, 2, 1], padding='SAME')  x = tf.placeholder(tf.float32, [None, 784])y_ = tf.placeholder(tf.float32, [None, 10])x_image = tf.reshape(x, [-1,28,28,1])W_conv1 = weight_variable([5, 5, 1, 32])b_conv1 = bias_variable([32])h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)h_pool1 = max_pool_2x2(h_conv1)W_conv2 = weight_variable([5, 5, 32, 64])b_conv2 = bias_variable([64])h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)h_pool2 = max_pool_2x2(h_conv2)W_fc1 = weight_variable([7 * 7 * 64, 1024])b_fc1 = bias_variable([1024])h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)keep_prob = tf.placeholder(tf.float32)h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)W_fc2 = weight_variable([1024, 10])b_fc2 = bias_variable([10])y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))sess.run(tf.global_variables_initializer())for i in range(20000):  batch = mnist.train.next_batch(50)  if i%100 == 0:    train_accuracy = accuracy.eval(feed_dict={        x:batch[0], y_: batch[1], keep_prob: 1.0})    print("step %d, training accuracy %g"%(i, train_accuracy))  train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})print("test accuracy %g"%accuracy.eval(feed_dict={    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

这是mnist手写字体库的一个测试程序,程序正常运行会想下图一样
这里写图片描述
最后输出正确率 0.91

4.总结

至此,整个tensorflow的安装环境搭建完成,前后大概用了两周左右的时间,期间碰到了许多的坑,一个一个的填起来的。总结就是一定要**仔细的阅读官方文档**,这个真的很重要,许多问题其实文档中已经介绍的很清楚了,可以避免很多问题,百度的教程只是作为参考,不要过分依赖。还有就是有问题多谷歌,回答比百度有营养多了。
原创粉丝点击