2017-11-07 12 views
2

나는 각각 3 차원의 3 개의 시계열을 가지고 RNN 분류기를 만들려고하는데, 시계열은 길이가 다를 수 있습니다. 그래서이를 해결하기 위해 3 개의 RNN을 모델링하여 최종 레이어에 연결했습니다. 어떻게해야, 재사용이 옵션을 선택하지 않습니다 그래서tensorflow에서 독립적 인 LSTM 셀을 만드는 방법은 무엇입니까?

ValueError: Variable rnn/multi_rnn_cell/cell_0/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True in VarScope?

timeSeries = ['outbound', 'rest', 'return'] 
n_steps = { 
    'outbound': 3159, 
    'rest': 3603, 
    'return': 3226 
} 
n_inputs = 3 
n_neurons = 20 
n_outputs = 2 
n_layers = 1 

learning_rate = 0.001 


y = tf.placeholder(tf.int32, [None], name="y") 
X = {} 
seq_length = {} 
for timeSeriesName in timeSeries: 
    with tf.name_scope(timeSeriesName + "_placeholders") as scope: 
     X[timeSeriesName] = tf.placeholder(tf.float32, [None, n_steps[timeSeriesName], n_inputs]) 
     seq_length[timeSeriesName] = tf.placeholder(tf.int32, [None]) 


outputs = {} 
states = {} 
top_layer_h_state = {} 
lstm_cells = {} 
multi_cell = {} 
finalRNNlayers = [] 
for timeSeriesName in timeSeries: 
    with tf.name_scope(timeSeriesName) as scope: 
     lstm_cells[timeSeriesName] = [tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons) 
             for layer in range(n_layers)] 
     multi_cell[timeSeriesName] = tf.contrib.rnn.MultiRNNCell(lstm_cells[timeSeriesName]) 
     outputs[timeSeriesName], states[timeSeriesName] = tf.nn.dynamic_rnn(
      multi_cell[timeSeriesName], X[timeSeriesName], dtype=tf.float32, 
      sequence_length=seq_length[timeSeriesName]) 
     top_layer_h_state[timeSeriesName] = states[timeSeriesName][-1][1] 
     finalRNNlayers.append(top_layer_h_state[timeSeriesName]) 

with tf.name_scope("3Stages_mixed") as scope: 
    concat3_top_layer_h_states = tf.concat(finalRNNlayers, axis=1) 
    logits = tf.layers.dense(concat3_top_layer_h_states, n_outputs, name="softmax") 

내가 때마다 시리즈는 자신의 무게를 각각 독립적 LSTM 세포를 갖고 싶어 :

그러나, 나는 다음과 같은 오류 메시지가 무엇입니까 이 오류가 수정 되었습니까?

The full traceback of the error can be found here.

답변

2

tf.name_scope(timeSeriesName)에서 tf.variable_scope(timeSeriesName)으로 변경하십시오. tf.name_scopetf.variable_scope의 차이는 this quesion에서 논의됩니다. 귀하의 경우 중요한 것은 tf.get_variable이 이름 범위를 무시하고 LSTM 셀 매개 변수가 정확하게 tf.get_variable으로 생성된다는 것입니다.

import tensorflow as tf 

state = tf.zeros([32, 6]) 

input1 = tf.placeholder(tf.float32, [32, 10]) 
input2 = tf.placeholder(tf.float32, [32, 10]) 

# Works ok: 
with tf.variable_scope('scope-1'): 
    tf.nn.rnn_cell.BasicLSTMCell(3, state_is_tuple=False)(input1, state) 
with tf.variable_scope('scope-2'): 
    tf.nn.rnn_cell.BasicLSTMCell(3, state_is_tuple=False)(input2, state) 

# Fails: 
with tf.name_scope('name-1'): 
    tf.nn.rnn_cell.BasicLSTMCell(3, state_is_tuple=False)(input1, state) 
with tf.name_scope('name-2'): 
    tf.nn.rnn_cell.BasicLSTMCell(3, state_is_tuple=False)(input2, state) 
:

샘플 코드는 차이를 볼 수