github博客传送门 博客园传送门
本章所需知识:
没有基础的请观看深度学习系列视频 tensorflow Python基础
资料下载链接:
深度学习基础网络模型(mnist手写体识别数据集)
MNIST数据集手写体识别(CNN实现)
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 )
class RNNNet :
def __init__ ( self) :
self. x = tf. placeholder( dtype= tf. float32, shape= [ None , 28 , 28 ] , name= 'input_x' )
self. y = tf. placeholder( dtype= tf. float32, shape= [ None , 10 ] , name= 'input_y' )
self. fc_w1 = tf. Variable( tf. truncated_normal( shape= [ 128 , 10 ] , dtype= tf. float32, stddev= tf. sqrt( 1 / 10 ) , name= 'fc_w1' ) )
self. fc_b1 = tf. Variable( tf. zeros( shape= [ 10 ] ) , dtype= tf. float32, name= 'fc_b1' )
def forward ( self) :
cell = tf. nn. rnn_cell. BasicLSTMCell( 128 )
state1 = cell. zero_state( 100 , dtype= tf. float32)
self. rnn_ouput, self. state = tf. nn. dynamic_rnn( cell, self. x, initial_state= state1, time_major= False )
self. fc1 = tf. matmul( self. rnn_ouput[ : , - 1 , : ] , self. fc_w1) + self. fc_b1
self. output = tf. nn. softmax( self. fc1)
def backward ( self) :
self. cost = tf. reduce_mean( tf. nn. softmax_cross_entropy_with_logits( labels= self. y, logits= self. fc1, name= 'cost' ) )
self. opt = tf. train. AdamOptimizer( ) . minimize( self. cost)
def acc ( self) :
self. acc1 = tf. equal( tf. argmax( self. output, 1 ) , tf. argmax( self. y, 1 ) )
self. accaracy = tf. reduce_mean( tf. cast( self. acc1, tf. float32) )
if __name__ == '__main__' :
net = RNNNet( )
net. forward( )
net. backward( )
net. acc( )
init = tf. global_variables_initializer( )
with tf. Session( ) as sess:
sess. run( init)
for i in range ( 10000 ) :
ax, ay = mnist. train. next_batch( 100 )
ax_batch = ax. reshape( [ - 1 , 28 , 28 ] )
loss, output, accaracy, _ = sess. run( fetches= [ net. cost, net. output, net. accaracy, net. opt] , feed_dict= { net. x: ax_batch, net. y: ay} )
if i % 10 == 0 :
test_ax, test_ay = mnist. test. next_batch( 100 )
test_ax_batch = sess. run( tf. reshape( test_ax, [ - 1 , 28 , 28 ] ) )
test_output = sess. run( fetches= net. output, feed_dict= { net. x: test_ax_batch} )
test_acc = tf. equal( tf. argmax( test_output, 1 ) , tf. argmax( test_ay, 1 ) )
test_accaracy = sess. run( tf. reduce_mean( tf. cast( test_acc, tf. float32) ) )
print ( test_accaracy)
最后附上训练截图: