TensorFlow学习笔记(二):手写数字识别之多层感知机
2017-03-27 14:48
525 查看
在【TensorFlow学习笔记(一):手写数字识别之softmax回归】中:
我使用softmax回归算法识别mnist数据集的手写数字,在我机器上的mnist测试集上最好结果是
92.9% 。
在本节中,我们基于【TensorFlow学习笔记(一):手写数字识别之softmax回归】的代码,只修改几行代码,把softmax回归改为含一个隐层的多层感知机。
然后看看在mnist测试集上的准确率会是多少?
我增加一个含
500 个神经元的隐层。
在【TensorFlow学习笔记(一):手写数字识别之softmax回归】的基础上修改:
修改前的代码:
#定义变量 w 和 b
w = tf.Variable ( tf.zeros ( [ 784, 10 ] ) )
b = tf.Variable ( tf.zeros ( [ 10 ] ) )
#实现 softmax 模型
y = tf.nn.softmax ( tf.matmul ( x, w ) + b )
修改后的代码:
#定义变量 w1 、 b1、w2、b2
w1 = tf.Variable ( tf.truncated_normal ( [ 784, 500 ] , stddev = 0.1 ) )
b1 = tf.Variable ( tf.zeros ( [ 500 ] ) )
w2 = tf.Variable ( tf.truncated_normal ( [ 500, 10 ], stddev = 0.1 ) )
b2 = tf.Variable ( tf.zeros ( [ 10 ] ) )
#含一个隐层的多层感知机
h1 = tf.nn.relu ( tf.matmul ( x, w1 ) + b1 )
y = tf.nn.softmax ( tf.matmul (h1, w2 ) + b2 )
然后运行整个网络,在我的电脑上,准确率是
98.14% ,结果如下:
==================== RESTART: D:\Python_code\ML\mnist.py ====================
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
accuracy: 0.9814
>>>
完整代码如下:
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets ( "MNIST_data/", one_hot = True )
x = tf.placeholder ( tf.float32, [ None, 784 ] )
y_ = tf.placeholder ( tf.float32, [ None, 10 ] )
w1 = tf.Variable ( tf.truncated_normal ( [ 784, 500 ] , stddev = 0.1 ) )
b1 = tf.Variable ( tf.zeros ( [ 500 ] ) )
w2 = tf.Variable ( tf.truncated_normal ( [ 500, 10 ] , stddev = 0.1 ) )
b2 = tf.Variable ( tf.zeros ( [ 10 ] ) )
h1 = tf.nn.relu ( tf.matmul ( x, w1 ) + b1 )
y = tf.nn.softmax ( tf.matmul ( h1, w2 ) + b2 )
cross_entropy = tf.reduce_mean ( -tf.reduce_sum ( y_ * tf.log ( y ), 1 ) )
train_step = tf.train.GradientDescentOptimizer ( 0.03 ) .minimize ( cross_entropy )
correct_prediction = tf.equal ( tf.argmax ( y, 1 ) ,tf.argmax ( y_, 1 ) )
accuracy = tf.reduce_mean ( tf.cast ( correct_prediction, "float" ) )
init = tf.global_variables_initializer ( )
with tf.Session() as sess:
sess.run ( init )
for i in range ( 200000 ) :
batch_xs, batch_ys = mnist.train.next_batch ( 100 )
result = sess.run ( [ accuracy, train_step ], feed_dict = { x:batch_xs, y_:batch_ys } )
if ( i % 100 == 0 ):
print ( "accuracy:", sess.run ( accuracy, feed_dict = { x:mnist.test.images, y_:mnist.test.labels } )," step:", i )
我使用softmax回归算法识别mnist数据集的手写数字,在我机器上的mnist测试集上最好结果是
92.9% 。
在本节中,我们基于【TensorFlow学习笔记(一):手写数字识别之softmax回归】的代码,只修改几行代码,把softmax回归改为含一个隐层的多层感知机。
然后看看在mnist测试集上的准确率会是多少?
我增加一个含
500 个神经元的隐层。
在【TensorFlow学习笔记(一):手写数字识别之softmax回归】的基础上修改:
修改前的代码:
#定义变量 w 和 b
w = tf.Variable ( tf.zeros ( [ 784, 10 ] ) )
b = tf.Variable ( tf.zeros ( [ 10 ] ) )
#实现 softmax 模型
y = tf.nn.softmax ( tf.matmul ( x, w ) + b )
修改后的代码:
#定义变量 w1 、 b1、w2、b2
w1 = tf.Variable ( tf.truncated_normal ( [ 784, 500 ] , stddev = 0.1 ) )
b1 = tf.Variable ( tf.zeros ( [ 500 ] ) )
w2 = tf.Variable ( tf.truncated_normal ( [ 500, 10 ], stddev = 0.1 ) )
b2 = tf.Variable ( tf.zeros ( [ 10 ] ) )
#含一个隐层的多层感知机
h1 = tf.nn.relu ( tf.matmul ( x, w1 ) + b1 )
y = tf.nn.softmax ( tf.matmul (h1, w2 ) + b2 )
然后运行整个网络,在我的电脑上,准确率是
98.14% ,结果如下:
==================== RESTART: D:\Python_code\ML\mnist.py ====================
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
accuracy: 0.9814
>>>
完整代码如下:
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets ( "MNIST_data/", one_hot = True )
x = tf.placeholder ( tf.float32, [ None, 784 ] )
y_ = tf.placeholder ( tf.float32, [ None, 10 ] )
w1 = tf.Variable ( tf.truncated_normal ( [ 784, 500 ] , stddev = 0.1 ) )
b1 = tf.Variable ( tf.zeros ( [ 500 ] ) )
w2 = tf.Variable ( tf.truncated_normal ( [ 500, 10 ] , stddev = 0.1 ) )
b2 = tf.Variable ( tf.zeros ( [ 10 ] ) )
h1 = tf.nn.relu ( tf.matmul ( x, w1 ) + b1 )
y = tf.nn.softmax ( tf.matmul ( h1, w2 ) + b2 )
cross_entropy = tf.reduce_mean ( -tf.reduce_sum ( y_ * tf.log ( y ), 1 ) )
train_step = tf.train.GradientDescentOptimizer ( 0.03 ) .minimize ( cross_entropy )
correct_prediction = tf.equal ( tf.argmax ( y, 1 ) ,tf.argmax ( y_, 1 ) )
accuracy = tf.reduce_mean ( tf.cast ( correct_prediction, "float" ) )
init = tf.global_variables_initializer ( )
with tf.Session() as sess:
sess.run ( init )
for i in range ( 200000 ) :
batch_xs, batch_ys = mnist.train.next_batch ( 100 )
result = sess.run ( [ accuracy, train_step ], feed_dict = { x:batch_xs, y_:batch_ys } )
if ( i % 100 == 0 ):
print ( "accuracy:", sess.run ( accuracy, feed_dict = { x:mnist.test.images, y_:mnist.test.labels } )," step:", i )
相关文章推荐
- TensorFlow笔记(二)---多层感知机识别手写数字
- TensorFlow学习笔记【二】 手写数字识别入门
- TensorFlow学习笔记之源码分析(2)----手写数字识别mnist example
- Tensorflow实战——利用多层感知机识别手写数字
- 【theano-windows】学习笔记十——多层感知机手写数字分类
- 学习笔记TF024:TensorFlow实现Softmax Regression(回归)识别手写数字
- 学习笔记(五)Tensorflow实现Soft Regression简单识别MNIST手写数字
- tensorflow学习之识别单张图片的实现(python手写数字)
- 一文全解:利用谷歌深度学习框架Tensorflow识别手写数字图片(初学者篇)
- 深度学习笔记(四)用Torch实现MNIST手写数字识别
- 神经网络与深度学习笔记——第1章 使用神经网络识别手写数字
- Spark学习笔记——手写数字识别
- TensorFlow学习记录-- 5.用lstm对手写数字进行识别(待修改,差增加rnn以及lstm的知识)
- 机器学习笔记:tensorflow实现卷积神经网络经典案例--识别手写数字
- TensorFlow学习_02_CNN卷积神经网络_Mnist手写数字识别
- TensorFlow学习-基于CNN实现手写数字识别
- tensorflow 学习笔记9 卷积神经网络(CNN)实现mnist手写识别
- SVM学习笔记(二)----手写数字识别
- 机器学习深度学习基础笔记(2)——梯度下降之手写数字识别算法实现
- 基于TensorFlow解决手写数字识别的Softmax方法、多层卷积网络方法和前馈神经网络方法