您的位置:首页 > 其它

Tensorflow第一个例子

2017-03-06 11:10 211 查看
安装完Tensorflow之后,尝试经典的MNIST手写体识别的例子。该例子在安装tensorflow之后自带,可在如下目录中找到(Anaconda_PATH代表安装Anaconda的目录位置)

Anaconda_PATH\envs\TensorFlow\Lib\site-packages\tensorflow\examples\tutorials\mnist

该目录中包含三个例子mnist_softmax.py,fully_connected_feed.py,mnist_with_summaries.py。其他几个文件是这几个例子执行时需要引用的。先从最简单的例子mnist_softmax.py来说明tensorflow的基本概念和原理。

mnist_softmax.py文件的内容如下:

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0 #
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""A very simple MNIST classifier.

See extensive documentation at http://tensorflow.org/tutorials/mnist/beginners/index.md """
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys

# Import data
from tensorflow.examples.tutorials.mnist import input_data

import tensorflow as tf

FLAGS = None

def main(_):
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b

# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])

# The raw formulation of cross-entropy,
#
#   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
#                                 reduction_indices=[1]))
#
# can be numerically unstable.
#
# So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
# outputs of 'y', and then average across the batch.
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.InteractiveSession()
# Train
tf.global_variables_initializer().run()
for _ in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images,
y_: mnist.test.labels}))

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',
help='Directory for storing input data')
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

可在命令行执行如下命令直接运行它:

python mnist_softmax.py

但是很有可能运行报错,因为天朝的网络很难下载数据集(反正我运行了好几次都不行)。最可靠的方法是新建data目录,下载下面四个文件(数据集)放到data中
http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
然后执行如下命令:

python mnist_softmax.py --data_dir data

可能的输出如下:



恭喜,说明第一个例子运行成功了。

接下来解释一下这个例子做了什么事。

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

这两句用于下载数据(很不幸一般很难下载下来),并将数据读入内存。数据读入内存后会分为三部分,训练集(mnist.train,包含55000个样例),测试集(mnist.test,包含10000个样例),校验集(mnist.validation,包含5000个样例)。每一个样例可以看成一个”点“,每个点包含一张图(手写数字的照片,可记为x)和一个标记(一个数字,表示这个手写的数字是几,可记为y)。每张图都是28x28=784像素大小的,如果把每个像素点记为一个浮点数,则每个点包含784个x(因为图片是黑白的,所以用一个浮点数表示灰度即可)和一个0-9的整数y。

在此例中,采用了单层10个神经元的神经网络,每个神经元对应0-9中的一个数,每个神经元最终的输出相当于该图片为这个数字的概率。Tensorflow在python环境中获取所有的神经网络模型信息之后,利用底层的库(通过NumPy库进行转换)来进行高效地计算(比如大量的矩阵乘法)。

import tensorflow as tf

x = tf.placeholder(tf.float32, [None, 784])

这两句中第一句引入了tensorflow库,第二句则定义了输入变量,因为要在获得整个网络信息之后才进行计算,所以此处的网络输入是未知的,因此只是定义了一个占位符变量,表示输入量是2维浮点数,其中第一维可能是任意长度(第一维是None,代表任意长度),第二维长度为784,代表一张图上所有的像素点。

接下来两句定义了变量,表示在计算中可以改变但一直存在的参数,并规定其初值均为0。由于神经网络计算就是要求出权重W和偏置b,因此这些变量可以取任意初始值。

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))


本来至此应该定义模型如下,表示输出y=f_softmax(W.x+b)

y = tf.nn.softmax(tf.matmul(x, W) + b)

但正如mnist_softmax.py原文中所解释的,为了后面定义训练模型的规则,此处改为了

y = tf.matmul(x, W) + b

y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))

第1行仅定义了y=W.x+b,然后为了定义该模型怎么样算好,或者说我们希望以什么东西为标准来进行优化,又定义了cross_entropy(交叉熵)的概念,该概念依赖于真实值y'和计算值y,满足

cross_entropy = - sum_i y'_i log(y_i)

y' 代表每张图所代表的真实值,比如 3 的话,就是(0,0,0,1,0,0,0,0,0,0)。交叉熵越大,意味着结果越差。因此第2行定义了y_占位符来保存真正的输出值,第3行给出了交叉熵的定义。

接下来利用反向传播算法最小化cross_entropy,训练模型

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

该语句表示利用梯度下降算法,以learning_rate=0.5的学习速率,最小化之前定义的cross_entropy。至此,整个神经网络模型构建好,tensorflow可以进行计算,调整参数,给出最终结果了。注意tensorflow提供了很多优化算法,可参考如下链接
https://www.tensorflow.org/api_guides/python/train#optimizers
最后可以进行真正的计算了

sess = tf.InteractiveSession()

tf.global_variables_initializer().run()
for _ in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

前两句创建了一个session进行计算,并对变量进行初始化;后面的循环进行真正的计算。在此处共循环1000次,每次从训练集中随机取100个点(每个点包含一张图和一个标签),然后用这些点替换之前的占位符,然后执行train_step定义的计算。值得注意的是,原则上每次循环都应该用上训练集中的所有点,但这样计算过于耗时,因此这个例子中使用了训练集的子集来进行计算,也能给出不错的结果。

最后,对训练的结果进行测试

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images,
y_: mnist.test.labels}))

tf.argmax函数给出张量沿某个方向的最大值所在的指标,因此tf.argmax(y,1)给出神经网络模型所认为的图片对应的数字,而tf.argmax(y_,1)则给出该图片真实对应的数字。correct_prediction表示神经网络模型预言是否正确。第2行则将这些对错序列转化为浮点数,比如[true, true, false, true,...]转化为[1,1,0,1,...],然后计算其平均值。最后一行将最终的正确率打印出来。如前面图中的结果所示,正确率达到0.9196,约为92%,还算不错。

更详细的说明可参考如下链接:
https://www.tensorflow.org/get_started/mnist/beginners
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TensorFlow mnist