2016-04-29 120 views
3

我正在嘗試構建一個最簡單的LSTM網絡。只需要它預測序列np_input_data中的下一個值。tensorflow:與簡單的LSTM網絡共享變量錯誤

import tensorflow as tf 
from tensorflow.python.ops import rnn_cell 
import numpy as np 

num_steps = 3 
num_units = 1 
np_input_data = [np.array([[1.],[2.]]), np.array([[2.],[3.]]), np.array([[3.],[4.]])] 

batch_size = 2 

graph = tf.Graph() 

with graph.as_default(): 
    tf_inputs = [tf.placeholder(tf.float32, [batch_size, 1]) for _ in range(num_steps)] 

    lstm = rnn_cell.BasicLSTMCell(num_units) 
    initial_state = state = tf.zeros([batch_size, lstm.state_size]) 
    loss = 0 

    for i in range(num_steps-1): 
     output, state = lstm(tf_inputs[i], state) 
     loss += tf.reduce_mean(tf.square(output - tf_inputs[i+1])) 

with tf.Session(graph=graph) as session: 
    tf.initialize_all_variables().run() 

    feed_dict={tf_inputs[i]: np_input_data[i] for i in range(len(np_input_data))} 

    loss = session.run(loss, feed_dict=feed_dict) 

    print(loss) 

的解釋回報:

ValueError: Variable BasicLSTMCell/Linear/Matrix already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at: 
    output, state = lstm(tf_inputs[i], state) 

我該怎麼辦錯了嗎?

回答

5

的調用lstm這裏:

for i in range(num_steps-1): 
    output, state = lstm(tf_inputs[i], state) 

會嘗試,除非你告訴它,否則使用相同的名稱創建變量每個迭代。您可以使用tf.variable_scope

with tf.variable_scope("myrnn") as scope: 
    for i in range(num_steps-1): 
    if i > 0: 
     scope.reuse_variables() 
    output, state = lstm(tf_inputs[i], state)  

第一次迭代創建表示您LSTM參數,以後每一次迭代(調用reuse_variables後)的變量只會看看他們的名字範圍做到這一點。

1

使用tf.nn.rnntf.nn.dynamic_rnn這樣做,以及許多其他好東西,爲你。

5

我在使用tf.nn.dynamic_rnn的TensorFlow v1.0.1中遇到類似的問題。事實證明,只有當我不得不在訓練中重新訓練或取消並重新開始訓練過程時纔會出現錯誤。基本上圖形沒有被重置。

長話短說,在代碼的開始處拋出一個tf.reset_default_graph(),它應該有所幫助。至少在使用tf.nn.dynamic_rnn和再培訓時。