码迷,mamicode.com
首页 > 编程语言 > 详细

ValueError: Attempt to reuse RNNCell <tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.BasicLSTMCell object at 0x7f1a3c448390> with a different variable scope than its first use.解决方法

时间:2017-07-01 10:01:37      阅读:1224      评论:0      收藏:0      [点我收藏+]

标签:last   end   ipython   while   build   原因   initial   his   diff   

最近在做生成电视剧本小项目,遇到以下报错

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-a2d9a7091ca5> in <module>()
     10     input_data_shape = tf.shape(input_text)
     11     cell, initial_state = get_init_cell(input_data_shape[0], rnn_size)
---> 12     logits, final_state = build_nn(cell, rnn_size, input_text, vocab_size, embed_dim)
     13 
     14     # Probabilities for generating words

<ipython-input-13-5720cbc33483> in build_nn(cell, rnn_size, input_data, vocab_size, embed_dim)
     11     # TODO: Implement Function
     12     embed_inputs = get_embed(input_data,vocab_size,embed_dim)
---> 13     outputs, final_state = build_rnn(cell,embed_inputs)
     14     logits = tf.contrib.layers.fully_connected(outputs,vocab_size)
     15     return logits, final_state

<ipython-input-12-a2502a6f4ee0> in build_rnn(cell, inputs)
      7     """
      8     # TODO: Implement Function
----> 9     outputs, state = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32)
     10     final_state = tf.identity(state,name=‘final_state‘)
     11     return outputs, final_state

/usr/local/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py in dynamic_rnn(cell, inputs, sequence_length, initial_state, dtype, parallel_iterations, swap_memory, time_major, scope)
    551         swap_memory=swap_memory,
    552         sequence_length=sequence_length,
--> 553         dtype=dtype)
    554 
    555     # Outputs of _dynamic_rnn_loop are always shaped [time, batch, depth].

/usr/local/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py in _dynamic_rnn_loop(cell, inputs, initial_state, parallel_iterations, swap_memory, sequence_length, dtype)
    718       loop_vars=(time, output_ta, state),
    719       parallel_iterations=parallel_iterations,
--> 720       swap_memory=swap_memory)
    721 
    722   # Unpack final output if not using output tuples.

/usr/local/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name)
   2621     context = WhileContext(parallel_iterations, back_prop, swap_memory, name)
   2622     ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, context)
-> 2623     result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
   2624     return result
   2625 

/usr/local/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants)
   2454       self.Enter()
   2455       original_body_result, exit_vars = self._BuildLoop(
-> 2456           pred, body, original_loop_vars, loop_vars, shape_invariants)
   2457     finally:
   2458       self.Exit()

/usr/local/lib/python3.5/site-packages/tensorflow/python/ops/control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)
   2404         structure=original_loop_vars,
   2405         flat_sequence=vars_for_body_with_tensor_arrays)
-> 2406     body_result = body(*packed_vars_for_body)
   2407     if not nest.is_sequence(body_result):
   2408       body_result = [body_result]

/usr/local/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py in _time_step(time, output_ta_t, state)
    703           skip_conditionals=True)
    704     else:
--> 705       (output, new_state) = call_cell()
    706 
    707     # Pack state if using state tuples

/usr/local/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py in <lambda>()
    689 
    690     input_t = nest.pack_sequence_as(structure=inputs, flat_sequence=input_t)
--> 691     call_cell = lambda: cell(input_t, state)
    692 
    693     if sequence_length is not None:

/usr/local/lib/python3.5/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py in __call__(self, inputs, state, scope)
    951                 state, [0, cur_state_pos], [-1, cell.state_size])
    952             cur_state_pos += cell.state_size
--> 953           cur_inp, new_state = cell(cur_inp, cur_state)
    954           new_states.append(new_state)
    955     new_states = (tuple(new_states) if self._state_is_tuple else

/usr/local/lib/python3.5/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py in __call__(self, inputs, state, scope)
    711                              self._recurrent_input_noise,
    712                              self._input_keep_prob)
--> 713     output, new_state = self._cell(inputs, state, scope)
    714     if _should_dropout(self._state_keep_prob):
    715       new_state = self._dropout(new_state, "state",

/usr/local/lib/python3.5/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py in __call__(self, inputs, state, scope)
    233   def __call__(self, inputs, state, scope=None):
    234     """Long short-term memory cell (LSTM)."""
--> 235     with _checked_scope(self, scope or "basic_lstm_cell", reuse=self._reuse):
    236       # Parameters of gates are concatenated into one multiply for efficiency.
    237       if self._state_is_tuple:

/usr/local/lib/python3.5/contextlib.py in __enter__(self)
     57     def __enter__(self):
     58         try:
---> 59             return next(self.gen)
     60         except StopIteration:
     61             raise RuntimeError("generator didn‘t yield") from None

/usr/local/lib/python3.5/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py in _checked_scope(cell, scope, reuse, **kwargs)
     75             "this error will remain until then.)"
     76             % (cell, cell_scope.name, scope_name, type(cell).__name__,
---> 77                type(cell).__name__))
     78     else:
     79       weights_found = False

ValueError: Attempt to reuse RNNCell <tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.BasicLSTMCell object at 0x7f1a3c448390> 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.)

在此先给出解决方法,后续整理一下,写上原因

初始有报错代码:

def get_init_cell(batch_size, rnn_size):
    """
    Create an RNN Cell and initialize it.
    :param batch_size: Size of batches
    :param rnn_size: Size of RNNs
    :return: Tuple (cell, initialize state)
    """
    lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
    drop = tf.contrib.rnn.DropoutWrapper(lstm)
    cell = tf.contrib.rnn.MultiRNNCell([drop for _ in range(2)])
    initial_state = cell.zero_state(batch_size, tf.float32)
    InitailState = tf.identity(initial_state, name=initial_state)
    return cell, InitailState

改进后的代码:

def get_init_cell(batch_size, rnn_size):
    """
    Create an RNN Cell and initialize it.
    :param batch_size: Size of batches
    :param rnn_size: Size of RNNs
    :return: Tuple (cell, initialize state)
    """
    #lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
    #drop = tf.contrib.rnn.DropoutWrapper(lstm)
    #cell = tf.contrib.rnn.MultiRNNCell([drop for _ in range(2)])
    def lstm_cell():
        lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size,reuse=tf.get_variable_scope().reuse)
        drop = tf.contrib.rnn.DropoutWrapper(lstm)
        return drop
    cell = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(2)])
    initial_state = cell.zero_state(batch_size, tf.float32)
    InitailState = tf.identity(initial_state, name=initial_state)
    return cell, InitailState

 可参考:

ValueError: Attempt to reuse RNNCell with a different variable scope than its first use. 

How to reuse weights in MultiRNNCell?

ValueError: Attempt to reuse RNNCell <tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.BasicLSTMCell object at 0x7f1a3c448390> with a different variable scope than its first use.解决方法

标签:last   end   ipython   while   build   原因   initial   his   diff   

原文地址:http://www.cnblogs.com/max-hu/p/7101164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!