2017-11-07 8 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) 
:違いを見るために

サンプルコード

関連する問題