日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

单向LSTM与双向LSTM对比

發(fā)布時(shí)間:2024/3/24 编程问答 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单向LSTM与双向LSTM对比 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一個(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)了。

# -*- coding: utf-8 -*- import tensorflow as tf from tensorflow.contrib import rnnimport numpy as np #import input_data from tensorflow.examples.tutorials.mnist import input_data ##### mnist = input_data.read_data_sets('MNIST_data', one_hot=True) ###### configuration # O * W + b -> 10 labels for each image, O[? 28], W[28 10], B[10] # ^ (O: output 28 vec from 28 vec input) # | # +-+ +-+ +--+ # |1|->|2|-> ... |28| time_step_size = 28 # +-+ +-+ +--+ # ^ ^ ... ^ # | | | # img1:[28] [28] ... [28] # img2:[28] [28] ... [28] # img3:[28] [28] ... [28] # ... # img128 or img256 (batch_size or test_size 256) # each input size = input_vec_size=lstm_size=28# configuration variables input_vec_size = lstm_size = 28 # 輸入向量的維度 time_step_size = 28 # 循環(huán)層長(zhǎng)度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):# X, input shape: (batch_size, time_step_size, input_vec_size)# XT shape: (time_step_size, batch_size, input_vec_size)#對(duì)這一步操作還不是太理解,為什么需要將第一行和第二行置換XT = tf.transpose(X, [1, 0, 2]) # permute time_step_size and batch_size,[28, 128, 28]# XR shape: (time_step_size * batch_size, input_vec_size)XR = tf.reshape(XT, [-1, lstm_size]) # each row has input for each lstm cell (lstm_size=input_vec_size)# Each array shape: (batch_size, input_vec_size)X_split = tf.split(XR, time_step_size, 0) # split them to time_step_size (28 arrays),shape = [(128, 28),(128, 28)...]# Make lstm with lstm_size (each input vector size). num_units=lstm_size; forget_bias=1.0lstm = rnn.BasicLSTMCell(lstm_size, forget_bias=1.0, state_is_tuple=True)# Get lstm cell output, time_step_size (28) arrays with lstm_size output: (batch_size, lstm_size)# rnn..static_rnn()的輸出對(duì)應(yīng)于每一個(gè)timestep,如果只關(guān)心最后一步的輸出,取outputs[-1]即可outputs, _states = rnn.static_rnn(lstm, X_split, dtype=tf.float32) # 時(shí)間序列上每個(gè)Cell的輸出:[... shape=(128, 28)..]# tanh activation# Get the last outputreturn tf.matmul(outputs[-1], W) + B, lstm.state_size # State size to initialize the statemnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 讀取數(shù)據(jù)# mnist.train.images是一個(gè)55000 * 784維的矩陣, mnist.train.labels是一個(gè)55000 * 10維的矩陣 #訓(xùn)練集包含55000張圖片,每張圖片為28*28維矩陣 #訓(xùn)練集標(biāo)簽同樣對(duì)應(yīng)55000,10表示存在10個(gè)標(biāo)簽 trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels# 將每張圖用一個(gè)28x28的矩陣表示,(55000,28,28,1) #-1表示該數(shù)未知,根據(jù)后面28*28,將trx分成55000個(gè)28*28的矩陣,每個(gè)表示一張圖片。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])# get lstm_size and output 10 labels #生成一個(gè)初始隨機(jī)值 W = init_weights([lstm_size, 10]) # 輸出層權(quán)重矩陣28×10 B = init_weights([10]) # 輸出層bais# 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)#tf.ConfigProto,一般是在創(chuàng)建session時(shí)對(duì)session進(jìn)行配置 session_conf = tf.ConfigProto() session_conf.gpu_options.allow_growth = True#允許gpu在使用的過程中慢慢增加。# Launch the graph in a session with tf.Session(config=session_conf) as sess:# you need to initialize all variablestf.global_variables_initializer().run()for i in range(100):#從訓(xùn)練集中每段選擇一個(gè)batch訓(xùn)練,batch_size= end-startfor 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]})#X (128,28,28)s=len(teX)test_indices = np.arange(len(teX)) # Get A Test Batchnp.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)定性要好。

#coding:utf-8 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#實(shí)際上圖的像素列數(shù),每一行作為一個(gè)輸入,輸入到網(wǎng)絡(luò)中。 n_input = 28 #LSTM cell的展開寬度,對(duì)于圖像來說,也是圖像的行數(shù) #也就是圖像按時(shí)間步展開是按照行來展開的。 n_step = 28 #LSTM cell個(gè)數(shù) 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])#這里的參數(shù)只是最后的全連接層的參數(shù),調(diào)用BasicLSTMCell這個(gè)op,參數(shù)已經(jīng)包在內(nèi)部了,不需要再定義。 Weight = tf.Variable(tf.random_normal([2 * n_hidden, n_class])) #參數(shù)共享力度比cnn還大 bias = tf.Variable(tf.random_normal([n_class]))def BiRNN(x, weights, biases):#[1, 0, 2]只做第階和第二階的轉(zhuǎn)置x = tf.transpose(x, [1, 0, 2])#把轉(zhuǎn)置后的矩陣reshape成n_input列,行數(shù)不固定的矩陣。#對(duì)一個(gè)batch的數(shù)據(jù)來說,實(shí)際上有bacth_size*n_step行。x = tf.reshape(x, [-1, n_input]) #-1,表示樣本數(shù)量不固定#拆分成n_step組x = tf.split(x, n_step)#調(diào)用現(xiàn)成的BasicLSTMCell,建立兩條完全一樣,又獨(dú)立的LSTM結(jié)構(gòu)lstm_qx = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias = 1.0)lstm_hx = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias = 1.0)#兩個(gè)完全一樣的LSTM結(jié)構(gòu)輸入到static_bidrectional_rnn中,由這個(gè)op來管理雙向計(jì)算過程。outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(lstm_qx, lstm_hx, x, dtype = tf.float32)#最后來一個(gè)全連接層分類預(yù)測(cè)return tf.matmul(outputs[-1], weights) + biasespred = BiRNN(x, Weight, bias) #計(jì)算損失、優(yōu)化、精度(老套路) 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()#run圖過程。 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ò),歡迎將生活随笔推薦給好友。