2017-03-08 31 views
6

下面的代碼片斷ValueError異常:嘗試以比其第一次使用

import tensorflow as tf 
from tensorflow.contrib import rnn 

hidden_size = 100 
batch_size = 100 
num_steps = 100 
num_layers = 100 
is_training = True 
keep_prob = 0.4 

input_data = tf.placeholder(tf.float32, [batch_size, num_steps]) 
lstm_cell = rnn.BasicLSTMCell(hidden_size, forget_bias=0.0, state_is_tuple=True) 

if is_training and keep_prob < 1: 
    lstm_cell = rnn.DropoutWrapper(lstm_cell) 
cell = rnn.MultiRNNCell([lstm_cell for _ in range(num_layers)], state_is_tuple=True) 

_initial_state = cell.zero_state(batch_size, tf.float32) 

iw = tf.get_variable("input_w", [1, hidden_size]) 
ib = tf.get_variable("input_b", [hidden_size]) 
inputs = [tf.nn.xw_plus_b(i_, iw, ib) for i_ in tf.split(input_data, num_steps, 1)] 

if is_training and keep_prob < 1: 
    inputs = [tf.nn.dropout(input_, keep_prob) for input_ in inputs] 

outputs, states = rnn.static_rnn(cell, inputs, initial_state=_initial_state) 

產生以下錯誤不同的變量範圍重用RNNCell:

ValueError: Attempt to reuse RNNCell < tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.BasicLSTMCell object at 0x10210d5c0> with a different variable scope than its first use. First use of cell was with scope 'rnn/multi_rnn_cell/cell_0/basic_lstm_cell' , this attempt is with scope `'rnn/multi_rnn_cell/cell_1/basic_lstm_cell'``.

Please create a new instance of the cell if you would like it to use a different set of weights.

If before you were using: MultiRNNCell([BasicLSTMCell(...)] * num_layers) , change to: MultiRNNCell([BasicLSTMCell(...) for _ in range(num_layers)]) .

If before you were using the same cell instance as both the forward and reverse cell of a bidirectional RNN, simply create two instances (one for forward, one for reverse).

In May 2017, we will start transitioning this cell's behavior to use existing stored weights, if any, when it is called with scope=None (which can lead to silent model degradation, so this error will remain until then.)

如何解決這個問題?

我的Tensorflow版本是1.0。

+0

你能解決這個問題嗎?我堅持與張量流相同1.1 – dv3

+0

https://github.com/tensorflow/tensorflow/issues/8191#issuecomment-292881312,到目前爲止我還沒有處理這個問題,看看上面的鏈接是否可以幫助你。 –

回答

8

正如評論我的解決方案是建議:
改變這個

cell = tf.contrib.rnn.LSTMCell(state_size, state_is_tuple=True) 
cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=0.8) 
rnn_cells = tf.contrib.rnn.MultiRNNCell([cell for _ in range(num_layers)], state_is_tuple = True) 
outputs, current_state = tf.nn.dynamic_rnn(rnn_cells, x, initial_state=rnn_tuple_state, scope = "layer") 

到:

def lstm_cell(): 
    cell = tf.contrib.rnn.LSTMCell(state_size, reuse=tf.get_variable_scope().reuse) 
    return tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=0.8) 

rnn_cells = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(num_layers)], state_is_tuple = True) 
outputs, current_state = tf.nn.dynamic_rnn(rnn_cells, x, initial_state=rnn_tuple_state) 

這似乎解決了重用性的問題。我並沒有從根本上理解底層的問題,但是這個解決了我在TF 1.1rc2
歡呼聲中的問題!

+0

它也改變了代碼*做的*。 LSTM和[NAS](https://arxiv.org/pdf/1611.01578.pdf)單元格不等效。 – Ray

+0

@Ray true,但這只是由於複製粘貼。上面的代碼適用於lstm單元格。我恰好碰巧與NAS單元一起工作 – dv3