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.解决方法

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

---------------------------------------------------------------------------
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?

时间: 2024-10-19 08:50:08

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.解决方法的相关文章

第二十二节,TensorFlow中RNN实现一些其它知识补充

一 初始化RNN 上一节中介绍了 通过cell类构建RNN的函数,其中有一个参数initial_state,即cell初始状态参数,TensorFlow中封装了对其初始化的方法. 1.初始化为0 对于正向或反向,第一个cell传入时没有之前的序列输出值,所以需要对其进行初始化.一般来讲,不用刻意取指定,系统会默认初始化为0,当然也可以手动指定其初始化为0. initial_state = lstm_cell.zero_state(batch_size, dtype=tf.float32) 2.初

关于tensorflow里面的tf.contrib.rnn.BasicLSTMCell 中num_units参数问题

这里的num_units参数并不是指这一层油多少个相互独立的时序lstm,而是lstm单元内部的几个门的参数,这几个门其实内部是一个神经网络,答案来自知乎: class TRNNConfig(object): """RNN配置参数""" # 模型参数 embedding_dim = 100 # 词向量维度 seq_length = 100 # 序列长度 num_classes = 2 # 类别数 vocab_size = 10000 # 词汇表达

深度学习原理与框架-递归神经网络-RNN网络基本框架(代码?) 1.rnn.LSTMCell(生成单层LSTM) 2.rnn.DropoutWrapper(对rnn进行dropout操作) 3.tf.contrib.rnn.MultiRNNCell(堆叠多层LSTM) 4.mlstm_cell.zero_state(state初始化) 5.mlstm_cell(进行LSTM求解)

问题:LSTM的输出值output和state是否是一样的 1. rnn.LSTMCell(num_hidden, reuse=tf.get_variable_scope().reuse)  # 构建单层的LSTM网络 参数说明:num_hidden表示隐藏层的个数,reuse表示LSTM的参数进行复用 2.rnn.DropoutWrapper(cell, output_keep_prob=keep_prob) # 表示对rnn的输出层进行dropout 参数说明:cell表示单层的lstm,o

跟我学算法- tensorflow 实现RNN操作

对一张图片实现rnn操作,主要是通过先得到一个整体,然后进行切分,得到的最后input结果输出*_w['out'] + _b['out']  = 最终输出结果 第一步: 数据载入 import tensorflow as tf from tensorflow.contrib import rnn from tensorflow.examples.tutorials.mnist import input_data import numpy as np import matplotlib.pyplo

tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别

tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别 https://blog.csdn.net/u014365862/article/details/78238807 MachineLP的Github(欢迎follow):https://github.com/MachineLP 我的GitHub:https://github.com/MachineLP/train_cnn-rnn-attention 自己搭建的一个框架,包含模型有:vgg(vgg16,vg

图融合之加载子图:Tensorflow.contrib.slim与tf.train.Saver之坑

import tensorflow as tf import tensorflow.contrib.slim as slim import rawpy import numpy as np import tensorflow as tf import struct import glob import os from PIL import Image import time __sony__ = 0 __huawei__ = 1 __blackberry__ = 2 __stage_raw2ra

[翻译] Tensorflow中name scope和variable scope的区别是什么

翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-scope-in-tensorflow 问题:下面这几个函数的区别是什么? tf.variable_op_scope(values, name, default_name, initializer=None) Returns a context manager for defining an op t

ValueError: The field authtoken.Token.user was declared with a lazy reference to &#39;api.user&#39;, but app &#39;api&#39; isn&#39;t installed解决方法

找到自己的python3.x,进入site-packages/django/contrib/admin/migrations文件目录下,除了__init__.py文件,其他的全部删除.(注意,切勿把__init__.py文件删了,也不要把contrib/contenttypes这个文件夹下的migrations删了,不然会导致migrate功能失效,就只能把django卸了重下). 转自:https://blog.csdn.net/cf313995/article/details/8434218

TensorFlow 常见错误与解决方法——长期不定时更新

1. TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a builtin_function_or_method into a Tensor. 错误代码如下: _, summary, train_cost = sess.run([optimizer, merged, loss], feed_dict={ X: real_data, Y: real_label, is_training: True, iter: