Bi LSTM的理解以及 Tensorflow實現

2021-08-29 02:26:39 字數 1464 閱讀 1752

bidirectional lstm,由兩個lstms上下疊加在 一起組成。輸出由這兩個lstms的隱藏層的狀態決定。

def bilstm(self,x):

# 輸入的資料格式轉換

# x.shape [batch_size, time_steps, input_size]

x=tf.transpose(x,[1,0,2])

fw_x = tf.reshape(x, [-1, self.n_input_text]) # step*batch, feature

fw_x = tf.split(0, self.n_step_text, fw_x)

with tf.variable_scope('bilstm_lt'):

#定義cell,單層lstm

lstm_fw_cell = rnn_cell.basiclstmcell(self.n_hidden_text, forget_bias=1.0, state_is_tuple=true)#前向的lstm cell

lstm_bw_cell = rnn_cell.basiclstmcell(self.n_hidden_text, forget_bias=1.0, state_is_tuple=true)#反向的rnn cell

#dropout

#構建雙向的rnn網路

with tf.variable_scope('fw_lt'):

(output_fw, state_fw) = rnn.rnn(lstm_fw_cell,fw_x,dtype=tf.float32)

t=tf.convert_to_tensor(output_fw)

print (t.get_shape().as_list())

with tf.variable_scope('bw_lt'):

bw_x = tf.reverse(x, [true,false,false])# reverse time dim

bw_x = tf.reshape(bw_x, [-1, self.n_input_text]) # step*batch, feature

bw_x = tf.split(0, self.n_step_text, bw_x)

(output_bw, state_bw) = rnn.rnn(lstm_bw_cell,bw_x,dtype=tf.float32)

# output_bw.shape = [timestep_size, batch_size, hidden_size]

output_bw = tf.reverse(output_bw, [true,false,false])

output = tf.concat(2,[output_fw, output_bw])#在第2個維度上,將output_fw, output_bw拼接

return output#返回值:(outputs, output_states:最後一層隱藏層)

指標的基礎理解,以及引用的理解

指標這個概念是乙個比較頭疼的概念,如果學過資料結構還懂點程式語言的人應該好懂一點,下面是我多年學習以來對於指標的一些理解.1.首先你需要明白的是指標是乙個變數 在沒有宣告const的特殊情況下 這就夠了.如果你需要我解釋什麼是變數,那就請你別象下看了 2.接下來要知道的是指標是乙個特殊的變數,聽其名...

OPP OOP以及AOP的理解

接觸到opp oop以及aop,是因為學校開了這些相關的課程,對此有了一些小小的了解吧,在這裡結合網上了解到的寫了這篇文件。opp顧名思義,面向過程程式設計,主要是針對過程的,按照每個過程的實際執行順序進行處理。而oop是針對物件的,通過抽象出乙個共有的bean,然後各個物件還有各自的方法。到了需要...

python property 的理解以及它的坑

1 property就是既擁有set get方法的靈活性,又具有屬性直接賦值取值的簡便性 2 property的屬性名必須有下劃線,不然會報錯 3 在乙個方法前加上 property之後,你的方法就會自動擁有 get 直接取值的能力,以及可賦值的屬性 硬要理解的話,下面兩段 效果是一樣的 prope...