TensorFlow安装(Ubuntu14.04)

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 --upgrade
https://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.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Initialized!
Step 0 (epoch 0.00), 68.8 ms
Minibatch loss: 12.053, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Step 100 (epoch 0.12), 1197.0 ms
Minibatch loss: 3.283, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 6.8%
Step 200 (epoch 0.23), 1231.3 ms
Minibatch loss: 3.455, learning rate: 0.010000
Minibatch error: 17.2%
Validation error: 4.0%
Step 300 (epoch 0.35), 1147.7 ms
Minibatch loss: 3.248, learning rate: 0.010000
Minibatch error: 7.8%
Validation error: 3.3%
Step 400 (epoch 0.47), 1139.3 ms
Minibatch loss: 3.213, learning rate: 0.010000
Minibatch error: 7.8%
Validation error: 2.6%
Step 500 (epoch 0.58), 1159.7 ms
Minibatch loss: 3.301, learning rate: 0.010000
Minibatch error: 9.4%
Validation error: 2.5%

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

MNIST运行实例2

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

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = 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]))        #初始化权值W
b = tf.Variable(tf.zeros([10]))            #初始化偏置项b
y_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的准确率。这个事例的详细解释可以参见这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值