RNN LSTM 是对
#-*-coding:utf-8-*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib import rnn
#载入数据
minist = input_data.read_data_sets('MNIST_data',one_hot=True)
#输入图片是28*28
#一行是28
n_input = 28
#一共28行
max_time = 28
#隐层单元
lstm_size = 100
#一共10个分类
n_classes = 10
#每批次50个样本
batch_size = 50
#一共多少个批次
n_batch = minist.train.num_examples//batch_size
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#初始化权值
w = tf.Variable(tf.truncated_normal([lstm_size,n_classes],stddev=0.1))
#初始化偏置值
b = tf.Variable(tf.constant(0.1,shape=[n_classes]))
#定义RNN网络
def RNN(x,weights,biases):
#inputs = [batch_size,max_time,n_inputs]
inputs = tf.reshape(x,[-1,max_time,n_input])
#定义LSTM基本CELL
lstm_cell = rnn.BasicLSTMCell(lstm_size)
#final_state[0]是cell_state, final_state[1]是hidden_state
out_puts,final_state = tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
result = tf.nn.softmax(tf.matmul(final_state[1],weights)+biases)
return result
#计算RNN的返回结果
prediction = RNN(x,w,b)
#损失函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#使用AdamOptimizer进行优化
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#结果存放在一个布尔列表中
correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(21):
for batch in range(n_batch):
batch_xs,batch_ys = minist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
acc = sess.run(accuracy,feed_dict={x:minist.test.images,y:minist.test.labels})
print('Iterator : '+str(epoch)+' accuracy : '+str(acc))
运行结果:
可以看到,运行准确率有波动,收敛速度较快
保存训练模型和训练好的参数以供以后继续训练或直接使用
#用于保存训练好的模型和参数
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess, 'net/my_net.ckpt')
for epoch in range(1):
for batch in range(n_batch):
batch_xs,batch_ys = minist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
acc = sess.run(accuracy,feed_dict={x:minist.test.images,y:minist.test.labels})
print('Iterator : '+str(epoch)+' accuracy : '+str(acc))
saver.save(sess, 'net/my_net.ckpt')
第一次保存时,只有 saver.save(sess, ‘net/my_net.ckpt’) 这句
第二次使用时,可以使用 saver.restore(sess, ‘net/my_net.ckpt’)直接使用参数
第一次训练结果:
第二次训练结果:
版权声明:本文为qxdoit原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。