tensorflow用RNN实现手写数字识别

#保存模型参数
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import warnings
warnings.filterwarnings("ignore")
input_length=28
max_time=28
x=tf.placeholder(tf.float32,[None,784],name="x_input")
y=tf.placeholder(tf.float32,[None,10],name="y_input")

lstm_cell=tf.nn.rnn_cell.BasicLSTMCell(28)
xx=tf.reshape(x,[-1,28,28])
outputs,final=tf.nn.dynamic_rnn(lstm_cell,xx,dtype=tf.float32)

w=tf.Variable(tf.truncated_normal([28,10],stddev=0.1))
b=tf.Variable(tf.zeros([10]))

logit=tf.nn.softmax(tf.add(tf.matmul(final[1],w),b))
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=logit))
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)
precise=    tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y,1),tf.argmax(logit,1)),dtype=tf.float32))

mnist=input_data.read_data_sets("MNIST_data",one_hot=True)
batch_size=1000
n_batch=mnist.train.num_examples//batch_size

saver=tf.train.Saver()
with tf.Session() as sess:
    init=tf.global_variables_initializer()
    sess.run(init)
    # try:
    saver.restore(sess,"net/my_net.ckpt")  #加载保存训练参数
    for i in range(10):
        for j in range(n_batch):
            x_input,y_input=mnist.train.next_batch(batch_size)
            sess.run(train_step,feed_dict={x:x_input,y:y_input})
            loss_p=sess.run(loss,feed_dict={x:x_input,y:y_input})
            precise_p=sess.run(precise,feed_dict={x:x_input,y:y_input})
            print(precise_p)
    saver.save(sess,"net/my_net.ckpt")  #保存模型


#准确率96.5%左右




 #使用静态RNN

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist=input_data.read_data_sets("MNIST_data",one_hot=True)
n_input=28
n_steps=28
n_hidden=128
batch_size=1000
tf.reset_default_graph()

x=tf.placeholder("float",[None,n_steps,n_input])
y=tf.placeholder("float",[None,10])

#重置tf.contrib.rnn.static_rnn要求的输入格式
x1=tf.unstack(x,n_steps,1)  #n_step个batch_size*n_input长度,x1中总共有28个列表,第一个列表表示所有batch第一步的集合
# #使用RNN网络,生成时间长,占用内存多
##基本的RNN单元,静态
############################
# lstm_cell=tf.contrib.rnn.BasicLSTMCell(n_hidden,forget_bias=1.0)
# outputs,states=tf.contrib.rnn.static_rnn(lstm_cell,x1,dtype=tf.float32)
# ##########################
# ##高级的RNN网络,静态
# ###################################
# lstm_cell=tf.contrib.rnn.LSTMCell(n_hidden,forget_bias=1.0)
# outputs,states=tf.contrib.rnn.static_rnn(lstm_cell,x1,dtype=tf.float32)
# ###################################
##gru  静态
##############################################
gru=tf.contrib.rnn.GRUCell(n_hidden)
outputs=tf.contrib.rnn.static_rnn(gru,x1,dtype=tf.float32)
###############################################

pred=tf.contrib.layers.fully_connected(outputs[-1],10,activation_fn=None)

learning_rate=0.1
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred,labels=y))
optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

correct_pred=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32))

n_batch_size=mnist.train.num_examples//batch_size
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(10):
        for j in range(n_batch_size):
            batch_x,batch_y=mnist.train.next_batch(batch_size)
            batch_x=batch_x.reshape((-1,n_steps,n_input))
            acc=sess.run(accuracy,feed_dict={x:batch_x,y:batch_y})
            loss=sess.run(cost,feed_dict={x:batch_x,y:batch_y})
            print(acc)

    #计算128张测试图片的准确率
    test_len=128
    test_data=mnist.test.images[:test_len].reshape((-1,n_steps,n_input))
    test_label=mnist.test.labels[:test_len]
    print(sess.run(accuracy,feed_dict={x:test_data,y:test_label}))

 使用多层静态和动态RNN

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib import rnn

mnist=input_data.read_data_sets("MNIST_data",one_hot=True)
n_input=28
n_steps=28
n_hidden=128
batch_size=1000
tf.reset_default_graph()

x=tf.placeholder("float",[None,n_steps,n_input])
y=tf.placeholder("float",[None,10])

# #@@单层静态双向
# #############################
# x1=tf.unstack(x,n_steps,1)  #n_step个batch_size*n_input长度,x1中总共有28个列表,第一个列表表示所有batch第一步的集合
# lstm_fw_cell=rnn.BasicLSTMCell(n_hidden,forget_bias=1.0)
# lstm_bw_cell=rnn.BasicLSTMCell(n_hidden,forget_bias=1.0)
# outputs,_,_=rnn.static_bidirectional_rnn(lstm_fw_cell,lstm_bw_cell,x1,
#     dtype=tf.float32
# )
# #一层128,2层256
# #################################

# #@@单层动态双向
# #################################
# lstm_fw_cell=rnn.BasicLSTMCell(n_hidden,forget_bias=1.0)
# lstm_bw_cell=rnn.BasicLSTMCell(n_hidden,forget_bias=1.0)
# outputs,output_states=tf.nn.bidirectional_dynamic_rnn(
#     lstm_fw_cell,lstm_bw_cell,x,dtype=tf.float32
# )
# outputs=tf.concat(outputs,2)
# outputs=tf.transpose(outputs,[1,0,2])
####################################

# #@@多层静态双向
# ##############################
# x1=tf.unstack(x,n_steps,1)
# lstm_fw_cell=rnn.BasicLSTMCell(n_hidden,forget_bias=1.0)
# lstm_bw_cell=rnn.BasicLSTMCell(n_hidden,forget_bias=1.0)
# outputs,_,_=rnn.stack_bidirectional_rnn([lstm_fw_cell],[lstm_bw_cell],x1,
#                                         dtype=tf.float32)
# #############################

# #@@LIST多层静态双向
# #####################################
# x1=tf.unstack(x,n_steps,1)
# stacked_rnn=[]
# stacked_bw_rnn=[]
# for i in range(3):
#     stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
#     stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
#
# outputs,_,_=rnn.stack_bidirectional_rnn(
#     stacked_rnn,stacked_bw_rnn,x1,
#     dtype=tf.float32
# )
# ######################################

# #@@MultiRNNCell多层静态双向
##############################################################
# x1=tf.unstack(x,n_steps,1)
# stacked_rnn=[]
# stacked_bw_rnn=[]
# for i in range(3):
#     stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
#     stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
#
# mcell=tf.contrib.rnn.MultiRNNCell(stacked_rnn)
# mcell_bw=tf.contrib.rnn.MultiRNNCell(stacked_bw_rnn)
# outputs,_,_=rnn.stack_bidirectional_rnn([mcell],[mcell_bw],
#                                         x1,dtype=tf.float32)
##############################################################

#@@MultiRNNCell多层动态双向
#######################################################################
stacked_rnn=[]
stacked_bw_rnn=[]
for i in range(3):
    stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
    stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))

mcell=tf.contrib.rnn.MultiRNNCell(stacked_rnn)
mcell_bw=tf.contrib.rnn.MultiRNNCell(stacked_bw_rnn)
outputs,_,_=rnn.stack_bidirectional_dynamic_rnn([mcell],[mcell_bw],x,
    dtype=tf.float32
)
outputs=tf.transpose(outputs,[1,0,2])
#######################################################################



pred=tf.contrib.layers.fully_connected(outputs[-1],10,activation_fn=None)

learning_rate=0.1
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred,labels=y))
optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

correct_pred=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32))

n_batch_size=mnist.train.num_examples//batch_size
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(10):
        for j in range(n_batch_size):
            batch_x,batch_y=mnist.train.next_batch(batch_size)
            batch_x=batch_x.reshape((-1,n_steps,n_input))
            acc=sess.run(accuracy,feed_dict={x:batch_x,y:batch_y})
            loss=sess.run(cost,feed_dict={x:batch_x,y:batch_y})
            print(acc)

    #计算128张测试图片的准确率
    test_len=128
    test_data=mnist.test.images[:test_len].reshape((-1,n_steps,n_input))
    test_label=mnist.test.labels[:test_len]
    print(sess.run(accuracy,feed_dict={x:test_data,y:test_label}))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值