python实现LSTM神经网络模型

参考https://github.com/aymericdamien/TensorFlow-Examples

'''
用tensorflow实现递归循环网络(LSTM)
'''
from __future__ import print_function

import tensorflow as tf
from tensorflow.contrib import rnn

#导入MINIST数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/",one_hot=True)
'''
为了使用递归神经网络对图像进行分类,我们考虑每个图像
行作为一系列像素。 因为MNIST的图像形状是28 * 28px,我们会
为每个样本处理28个步骤的28个序列。
'''
#训练参数
learning_rate = 0.001
training_steps = 10000
batch_size = 128
display_step = 200

#神经网络参数
num_input = 28
timesteps = 28
num_hidden = 128
num_classes = 10

#tf图表输入
X = tf.placeholder("float",[None,timesteps,num_input])
Y = tf.placeholder("float",[None,num_classes])

#定义权重
weights = {
    'out':tf.Variable(tf.random_normal([num_hidden,num_classes]))
}
biases = {
    'out':tf.Variable(tf.random_normal([num_classes]))
}

def RNN(x,weights,biases):
    #准备数据形状以匹配`rnn`功能需求
    #当前数据输入形状:(batch_size,timesteps,n_input)
    #所需形状:形状的'timesteps'张量列表(batch_size,n_input)
    #Unstack获取形状的“时间步长”张量列表(batch_size,n_input)
    x = tf.unstack(x,timesteps,1)

    #通过tensorflow定义一个lstm单元
    lstm_cell = rnn.BasicLSTMCell(num_hidden,forget_bias=1.0)

    #lstm输出单元
    outputs,states = rnn.static_rnn(lstm_cell,x,dtype=tf.float32)

    #线性激活,使用rnn内部循环的最后输出
    return tf.matmul(outputs[-1],weights['out']) + biases['out']

logits = RNN(X,weights,biases)
prediction = tf.nn.softmax(logits)

#定义损失和优化器
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)

#评估模型(使用测试日志,禁用退出)
correct_pred = tf.equal(tf.argmax(prediction,1),tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))

#变量初始化
init = tf.global_variables_initializer()

#开始训练
with tf.Session() as sess:
    sess.run(init)

    for step in range(1,training_steps+1):
        batch_x,batch_y = mnist.train.next_batch(batch_size)

        #重塑数据以获得28个元素的28个序列
        batch_x = batch_x.reshape((batch_size,timesteps,num_input))

        #运行优化操作(backprop)
        sess.run(train_op,feed_dict={X:batch_x,Y:batch_y})
        if step % display_step == 0 or step ==1:
            #计算批次损失和准确性
            loss,acc = sess.run([loss_op,accuracy],feed_dict={X:batch_x,Y:batch_y})

            print("step" + str(step) + ",Minibatch Loss=" + "{:.4f}".format(loss) +
                  ",Training Accuracy=" + "{:.3f}".format(acc))

        print("优化完成")

        #计算128个mnist测试图像准确度
        test_len = 128
        test_data = mnist.test.images[:test_len].reshape((-1,timesteps,num_input))
        test_label = mnist.test.labels[:test_len]
        print("Testing Accuracy:",sess.run(accuracy,feed_dict={X:test_data,Y:test_label}))







你可能感兴趣的:(技术之路)