深度学习之mnist⑦——LSTM(长短期记忆)

此代码主体与前面mnist一样,用rnn长短期记忆进行多分类

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.layers import fully_connected as fc

# 读取数据
mnist = input_data.read_data_sets('../../datas/MNIST_data',one_hot=True)

# 站位
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.int32,[None,10])
x_img = tf.reshape(x,[-1,28,28])

# 测试集
test_x = mnist.test.images
test_y = mnist.test.labels

#
cell = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.BasicLSTMCell(10) for i in range(3)])

# 动态rnn
outputs , state = tf.nn.dynamic_rnn(cell,x_img,dtype = tf.float32)

# 全连接
in_top_key = state[-1][1]

logits = fc(in_top_key,10,activation_fn=None)

# 代价
cost = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y,1),logits=logits)

# 优化器
optimizer = tf.train.AdamOptimizer(0.01).minimize(cost)

# 准确率
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits,1),tf.argmax(y,1)),tf.float32))

# 开启会话
sess = tf.Session()
sess.run(tf.global_variables_initializer())

batch_size = 150
for epoch in range(3):
    batch = int(mnist.train.num_examples / batch_size)

    for k in range(batch):
        batch_x,batch_y = mnist.train.next_batch(batch_size)
        sess.run([cost,optimizer],feed_dict={x:batch_x,y:batch_y})
    acc_train = sess.run(accuracy,feed_dict={x:batch_x,y:batch_y})
    acc_test = sess.run(accuracy,feed_dict={x:test_x,y:test_y})
    print("Epoch", epoch, "Train accuracy =", acc_train, "Test accuracy =", acc_test)

Epoch 0 Train accuracy = 0.8933333 Test accuracy = 0.905
Epoch 1 Train accuracy = 0.96 Test accuracy = 0.9494
Epoch 2 Train accuracy = 0.96666664 Test accuracy = 0.955
Epoch 3 Train accuracy = 0.98 Test accuracy = 0.9631
Epoch 4 Train accuracy = 0.98 Test accuracy = 0.9646
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值