2017-06-28 34 views
0

假設我有2個輸入qa,如何使2個輸入共享1個LSTM單元?現在,我的代碼一部分初級講座如何在TensorFlow中共享2個獨立輸入的LSTM單元?

def lstmnets(self, sequence, seq_len): 
    seq_embeds = self.embeds(sequence) 

    # lstm_cell = tf.contrib.rnn.BasicLSTMCell(self.hidden_size) 
    lstm_cell = tf.nn.rnn_cell.LSTMCell(self.hidden_size) 
    init_state = lstm_cell.zero_state(self.batch_size, dtype=tf.float32) 
    lstm_out, final_state = tf.nn.dynamic_rnn(lstm_cell, seq_embeds, initial_state=init_state, sequence_length=seq_len) 
    return lstm_out 

def inference(self, q, a, q_len, a_len): 
    with tf.variable_scope('lstmnets') as scope: 
     query_rep = self.lstmnets(q, q_len) 
     scope.reuse_variables() 
     title_rep = self.lstmnets(a, a_len) 

但對於這個代碼,我已經結構堆疊2作爲LSTM如下圖。我怎樣才能爲兩者使用一個LSTM?另外,如何初始化LSTM權重並將它們添加到histogram?到目前爲止,我找不到相關的教程。謝謝。

enter image description here

回答

0

你的代碼似乎要被罰款,因爲它使用scope.reuse_variable()來共享LSTM權重。最好的方法是檢查是通過在圖中打印變量並驗證lstm_cell是否僅聲明一次。所以在你的推理函數中打印變量名稱:

def inference(self, q, a, q_len, a_len): 
    with tf.variable_scope('lstmnets') as scope: 
    query_rep = self.lstmnets(q, q_len) 
    scope.reuse_variables() 
    title_rep = self.lstmnets(a, a_len) 
    for v in tf.global_variables(): 
    print(v.name) 
+0

謝謝!我會試一試...你知道我在哪裏可以初始LSTM細胞重量? – danche

+0

我打印這個名字,似乎只有一個LSTM,因爲參數是'lstmnets/embeddings:0 lstmnets/rnn/lstm_cell/kernel:0 lstmnets/rnn/lstm_cell/bias:0', 2堆疊rnn ... – danche

+0

或者堆疊的LSTM完全相同,它們之間代表'share'的線?這與美國有線電視新聞網的份額相比是非常奇怪的...... – danche

相關問題