Bidirectional_Lstm_mnist

该博客演示了如何利用TensorFlow构建一个双向LSTM模型来识别MNIST数据集的手写数字。通过reshape操作将数据预处理,设置训练迭代次数、批次大小和学习率,并使用Adam优化器进行模型训练。最终,模型在测试集上展示了其识别准确率。
摘要由CSDN通过智能技术生成
import tensorflow as tf
import numpy as np
from tensorflow.contrib import rnn
from tensorflow.examples.tutorials.mnist import input_data


mnist = input_data.read_data_sets('E:/tensorflow/1005/data/MNIST_data/',one_hot=True)


trainX = np.array(mnist.train.images.reshape([-1,28,28]))
trainY = np.array(mnist.train.labels)
testX = np.array(mnist.test.images.reshape([-1,28,28]))
testY = np.array(mnist.test.labels)

learning_rate = 0.001
traing_iters = 10000
batch_size = 128
display_step = 10

n_input = 128
n_steps = 28
n_hidden = 128
n_classes = 10
tf.reset_default_graph()

x = tf.placeholder('float',[None,28,28])
y = tf.placeholder('float',[None,10])
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)

output,_,_ = rnn.static_bidirectional_rnn(lstm_fw_cell,lstm_bw_cell,x1,dtype=tf.float32)

#output[0].shape   #batch , 128*2

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

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))


optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)


initializer = tf.global_variables_initializer()

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

with tf.Session() as sess:
    sess.run(initializer)
    

    for i in range(1000):
        timg = trainX[55*i:55*(i+1)]
        tlab = trainY[55*i:55*(i+1)]
        #print(timg.shape)
        #print(tlab.shape)
        sess.run(optimizer,feed_dict={x:timg,y:tlab})
        l,a = sess.run([loss,accuracy],feed_dict={x:timg,y:tlab})
        print('cishu {0} : loss = {1}, accuracy = {2}'.format(i+1,l,a))    
        
        
    l,a = sess.run([loss,accuracy],feed_dict={x:testX,y:testY})
    print('Test: loss = {0}, accuracy = {1}'.format(l,a))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值