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))
Bidirectional_Lstm_mnist
最新推荐文章于 2023-08-31 22:04:48 发布
该博客演示了如何利用TensorFlow构建一个双向LSTM模型来识别MNIST数据集的手写数字。通过reshape操作将数据预处理,设置训练迭代次数、批次大小和学习率,并使用Adam优化器进行模型训练。最终,模型在测试集上展示了其识别准确率。
摘要由CSDN通过智能技术生成