当前位置:   article > 正文

python实现LSTM神经网络模型

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}))







  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/955062
推荐阅读
相关标签
  

闽ICP备14008679号