一個(gè)簡(jiǎn)單的DEMO:實(shí)現(xiàn)手寫數(shù)字圖片的識(shí)別
單向LSTM
利用的數(shù)據(jù)集是tensorflow提供的一個(gè)手寫數(shù)字?jǐn)?shù)據(jù)集。該數(shù)據(jù)集是一個(gè)包含55000張28*28的數(shù)據(jù)集。
訓(xùn)練100次
識(shí)別準(zhǔn)確率還不是很穩(wěn)定,但是從第17次開始就趨于相對(duì)穩(wěn)定的狀態(tài)了。
import tensorflow
as tf
from tensorflow.contrib
import rnn
import numpy
as np
from tensorflow.examples.tutorials.mnist
import input_data
mnist = input_data.read_data_sets(
'MNIST_data', one_hot=
True)
input_vec_size = lstm_size =
28
time_step_size =
28 batch_size =
128
test_size =
256def init_weights(shape):return tf.Variable(tf.random_normal(shape, stddev=
0.01))
def model(X, W, B, lstm_size):XT = tf.transpose(X, [
1,
0,
2]) XR = tf.reshape(XT, [-
1, lstm_size]) X_split = tf.split(XR, time_step_size,
0) lstm = rnn.BasicLSTMCell(lstm_size, forget_bias=
1.0, state_is_tuple=
True)outputs, _states = rnn.static_rnn(lstm, X_split, dtype=tf.float32)
return tf.matmul(outputs[-
1], W) + B, lstm.state_size mnist = input_data.read_data_sets(
"MNIST_data/", one_hot=
True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
trX = trX.reshape(-
1,
28,
28)
teX = teX.reshape(-
1,
28,
28)X = tf.placeholder(
"float", [
None,
28,
28])
Y = tf.placeholder(
"float", [
None,
10])
W = init_weights([lstm_size,
10])
B = init_weights([
10])
py_x, state_size = model(X, W, B, lstm_size)cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
train_op = tf.train.RMSPropOptimizer(
0.001,
0.9).minimize(cost)
predict_op = tf.argmax(py_x,
1)
session_conf = tf.ConfigProto()
session_conf.gpu_options.allow_growth =
True
with tf.Session(config=session_conf)
as sess:tf.global_variables_initializer().run()
for i
in range(
100):
for start, end
in zip(range(
0, len(trX), batch_size), range(batch_size, len(trX)+
1, batch_size)):sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})s=len(teX)test_indices = np.arange(len(teX)) np.random.shuffle(test_indices)test_indices = test_indices[
0:test_size]print(i, np.mean(np.argmax(teY[test_indices], axis=
1) ==sess.run(predict_op, feed_dict={X: teX[test_indices]})))
結(jié)果:
雙向LSTM
利用的數(shù)據(jù)集是tensorflow提供的一個(gè)手寫數(shù)字?jǐn)?shù)據(jù)集。該數(shù)據(jù)集是一個(gè)包含55000張28*28的數(shù)據(jù)集。
訓(xùn)練300次
可以看到使用雙向LSTM要訓(xùn)練更多次才能趨于相對(duì)穩(wěn)定,但是經(jīng)過100次訓(xùn)練后,準(zhǔn)確率明顯比單向LSTM要高,穩(wěn)定性要好。
import tensorflow
as tf
import numpy
as np
from tensorflow.examples.tutorials.mnist
import input_data
mnist = input_data.read_data_sets(
"/tmp/data", one_hot=
True)learning_rate =
0.01
max_samples =
400000
display_size =
10
batch_size =
128
n_input =
28
n_step =
28
n_hidden =
256
n_class =
10x = tf.placeholder(tf.float32, shape=[
None, n_step, n_input])
y = tf.placeholder(tf.float32, shape =[
None, n_class])
Weight = tf.Variable(tf.random_normal([
2 * n_hidden, n_class]))
bias = tf.Variable(tf.random_normal([n_class]))
def BiRNN(x, weights, biases):x = tf.transpose(x, [
1,
0,
2])x = tf.reshape(x, [-
1, n_input]) x = tf.split(x, n_step)lstm_qx = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias =
1.0)lstm_hx = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias =
1.0)outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(lstm_qx, lstm_hx, x, dtype = tf.float32)
return tf.matmul(outputs[-
1], weights) + biasespred = BiRNN(x, Weight, bias)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = pred, labels = y))
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
correct_pred = tf.equal(tf.argmax(pred,
1), tf.argmax(y,
1))
accurancy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))init = tf.global_variables_initializer()
with tf.Session()
as sess:sess.run(init)step =
1while step * batch_size < max_samples:batch_x, batch_y = mnist.train.next_batch(batch_size)batch_x = batch_x.reshape((batch_size, n_step, n_input))sess.run(optimizer, feed_dict = {x:batch_x, y:batch_y})
if step % display_size ==
0:acc = sess.run(accurancy, feed_dict={x:batch_x, y:batch_y})loss = sess.run(cost, feed_dict = {x:batch_x, y:batch_y})
print 'Iter' + str(step*batch_size) +
', Minibatch Loss= %.6f'%(loss) +
', Train Accurancy= %.5f'%(acc)step +=
1print "Optimizer Finished!"test_len =
10000test_data = mnist.test.images[:test_len].reshape(-
1, n_step, n_input)test_label = mnist.test.labels[:test_len]
print 'Testing Accurancy:%.5f'%(sess.run(accurancy, feed_dict={x: test_data, y:test_label}))Coord = tf.train.Coordinator()threads = tf.train.start_queue_runners(coord=Coord)
結(jié)果:
經(jīng)過對(duì)比發(fā)現(xiàn),在簡(jiǎn)單的圖像識(shí)別方面,雙向LSTM預(yù)測(cè)效果會(huì)更好。
總結(jié)
以上是生活随笔為你收集整理的单向LSTM与双向LSTM对比的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。