Tensorflow学习笔记——MNIST手写数字识别运行

        作为非专业代码爱好者,之前粗略阅读过一些相关书籍,同时配合一些代码实践想要更加近距离了解和学习深度学习。通过实践之后更新博客记录下自己的心路历程,算是督促自己,同时想验证一下“不想当程序猿的理工男不是好工程师”这句话吧。对于小白宽容一些,不喜勿喷谢谢。

>>> from tensorflow.examples.tutorials.mnist import input_data
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally
>>> import tensorflow as tf
>>> mnist = input_data.read_data_sets("MNIST_data",one_hot = True)
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
>>> sess = tf.InteractiveSession()
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
>>> x = tf.placeholder("float",shape=[None,784])
>>> y_ = tf.placeholder("float",shape=[None,10])
>>> W = tf.Variable(tf.zeros([784,10]))
>>> b = tf.Variable(tf.zeros([10]))
>>> sess.run(tf.initialize_all_variables())
WARNING:tensorflow:From :1: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.
>>> y = tf.nn.softmax(tf.matmul(x,W)+b)
>>> cross_entropy = -tf.reduce_sum(y_*tf.log(y))
>>> train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
>>> for i in range(1000):
...     batch = mnist.train.next_batch(50)
...     train_step.run(feed_dict = {x:batch[0],y_:batch[1]})
... 
>>> correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
>>> accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
>>> print(accuracy.eval(feed_dict={x:mnist.test.images,y_:mnist.test.labels}))
0.9092001
>>> sess.run(tf.global_variables_initializer())

1.read_data_sets()函数用于自动下载MNIST数据集,one_hot为独热码,作用是将状态值编码成状态向量。

2.InteractiveSession()目的是在交互式环境下,手动设定当前会话为默认会话

3.placeholder(dtype, shape=None, name=None)为占位符,在tensorflow中类似于函数参数,运行时必须传入值。dtype为数据类型,shape为数据维度,name名称。

4.Variable(initializer,name),参数initializer是初始化参数,name是可自定义的变量名称.

5.sess.run(tf.initialize_all_variables())中tf.initialize_all_variables()用于初始化变量,sess.run用于运行参数的初始化。

6.tf.nn.softmax(tf.matmul(x,W)+b)中matmul()表示矩阵相乘,为x * W,nn.softmax()计算softmax回归激活。

7.reduce_sum(arg1, arg2)中arg1即为要求和的数据,arg2有两个取值分别为0和1,分别为纵向对矩阵求和与横向对矩阵求和;省略arg2即默认对矩阵所有元素求和,该公式用于计算交叉熵cross_entropy。

8.tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)表示用梯度下降的方式以0.01的学习速率最小化交叉熵,同时可以计数global step。

9.train.next_batch(50)批量抓取50个数据,train_step.run()执行梯度下降训练,并循环1000次。

10.tf.argmax(y,1)返回模型预测标签值, tf.argmax(y_,1) 代表正确的标签,通过tf.equal()来检测模型得出预测是否匹配真实值。

MNIST学习完成。

你可能感兴趣的:(tensorflow学习)