TensorFlow安装(Ubuntu14.04)

来源:互联网 发布:阿里云代备案 编辑:程序博客网 时间:2024/06/06 14:23

TensorFlow是谷歌研发的最新的深度学习框架,其安装过程比caffe的安装要容易。目前,很多论文提供的开源代码是基于TensorFlow框架的。

Ubuntu14.04安装

在虚拟机上安装的步骤其实只需要两步,也可以参考TensorFlow官网的说明.由于Ubuntu系统自带的python2.7,因此不需要安装python。虽然python已经有更高级的版本了,但是高版本的很多库无法使用,因此还是使用2.7版本的。

第一步

必不可少的python-pip和python-dev
在命令行输入以下命令:

$ sudo apt-get install python-pip python-dev

中间会出现

Do you want to continue? [Y/n]

输入y并回车,就可以了。

第二步

安装TensorFlow,输入以下命令

$ sudo pip install --upgradehttps://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl

这个中间可能会提示你需要更新pip版本,按照提示给的命令即可。

pip install --upgrade pip

有时这个https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl网址并不能访问,提示“Network is unreachable”。这时可以手动下载文件tensorflow-0.8.0-cp27-none-linux_x86_64.whl,并将网址改为文件所在路径即可。

MNIST运行实例1

使用以下命令运行TensorFlow提供的手写识别训练集体会并验证TensorFlow安装成功。

$ python -m tensorflow.models.image.mnist.convolutional

运行结果

Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.Extracting data/train-images-idx3-ubyte.gzExtracting data/train-labels-idx1-ubyte.gzExtracting data/t10k-images-idx3-ubyte.gzExtracting data/t10k-labels-idx1-ubyte.gzInitialized!Step 0 (epoch 0.00), 68.8 msMinibatch loss: 12.053, learning rate: 0.010000Minibatch error: 90.6%Validation error: 84.6%Step 100 (epoch 0.12), 1197.0 msMinibatch loss: 3.283, learning rate: 0.010000Minibatch error: 6.2%Validation error: 6.8%Step 200 (epoch 0.23), 1231.3 msMinibatch loss: 3.455, learning rate: 0.010000Minibatch error: 17.2%Validation error: 4.0%Step 300 (epoch 0.35), 1147.7 msMinibatch loss: 3.248, learning rate: 0.010000Minibatch error: 7.8%Validation error: 3.3%Step 400 (epoch 0.47), 1139.3 msMinibatch loss: 3.213, learning rate: 0.010000Minibatch error: 7.8%Validation error: 2.6%Step 500 (epoch 0.58), 1159.7 msMinibatch loss: 3.301, learning rate: 0.010000Minibatch error: 9.4%Validation error: 2.5%

按Ctrl+C可以停止代码的运行。

MNIST运行实例2

之前一个是利用TensorFlow自带的代码运行的,只是看一下效果,现在我们通过代码详细理解一些训练的过程。以下完整代码可以直接运行。

import tensorflow as tfimport tensorflow.examples.tutorials.mnist.input_data as input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)x = tf.placeholder(tf.float32, [None, 784])y_actual = tf.placeholder(tf.float32, shape=[None, 10])W = tf.Variable(tf.zeros([784,10]))        #初始化权值Wb = tf.Variable(tf.zeros([10]))            #初始化偏置项by_predict = tf.nn.softmax(tf.matmul(x,W) + b)     #加权变换并进行softmax回归,得到预测概率cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_actual*tf.log(y_predict),reduction_indices=1))   #求交叉熵train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)   #用梯度下降法使得残差最小correct_prediction = tf.equal(tf.argmax(y_predict,1), tf.argmax(y_actual,1))   #在测试阶段,测试准确度计算accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))                #多个批次的准确度均值init = tf.initialize_all_variables()with tf.Session() as sess:    sess.run(init)    for i in range(1000):               #训练阶段,迭代1000次        batch_xs, batch_ys = mnist.train.next_batch(100)           #按批次训练,每批100行数据        sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys})   #执行训练        if(i%100==0):                  #每训练100次,测试一次            print "accuracy:",sess.run(accuracy, feed_dict={x: mnist.test.images, y_actual: mnist.test.labels})

训练10000次,可以达到%91的准确率。这个事例的详细解释可以参见这里

1 0
原创粉丝点击