tensorflow1.x版本rnn生成cell 报错解决方案

来源:互联网 发布:淘宝卖快排配件犯法吗 编辑:程序博客网 时间:2024/06/05 14:33

tensorflow1.x版本叫以前有很大改动,那个rnn的cell和别的一些地方有了作用域,具体可以看官网,下面是报错和解决办法。

ValueError: Variable hello/rnn/basic_lstm_cell/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at


Traceback (most recent call last):  File "F:/Deep/Deep/githubLSTM.py", line 232, in <module>    prediction()  File "F:/Deep/Deep/githubLSTM.py", line 107, in prediction    output_rnn,final_states=tf.nn.dynamic_rnn(cell, input_rnn,initial_state=init_state, dtype=tf.float32)  #output_rnn是记录lstm每个输出节点的结果,final_states是最后一个cell的结果  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 546, in dynamic_rnn    dtype=dtype)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 713, in _dynamic_rnn_loop    swap_memory=swap_memory)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2605, in while_loop    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2438, in BuildLoop    pred, body, original_loop_vars, loop_vars, shape_invariants)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2388, in _BuildLoop    body_result = body(*packed_vars_for_body)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 698, in _time_step    (output, new_state) = call_cell()  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\rnn.py", line 684, in <lambda>    call_cell = lambda: cell(input_t, state)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 179, in __call__    concat = _linear([inputs, h], 4 * self._num_units, True, scope=scope)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 747, in _linear    "weights", [total_arg_size, output_size], dtype=dtype)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 988, in get_variable    custom_getter=custom_getter)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 890, in get_variable    custom_getter=custom_getter)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 348, in get_variable    validate_shape=validate_shape)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 333, in _true_getter    caching_device=caching_device, validate_shape=validate_shape)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 639, in _get_single_variable    name, "".join(traceback.format_list(tb))))ValueError: Variable hello/rnn/basic_lstm_cell/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1226, in __init__    self._traceback = _extract_stack()  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2327, in create_op    original_op=self._default_original_op, op_def=op_def)  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op    op_def=op_def)


You need to specify different variable scopes for the LSTM cells.

with tf.variable_scope('forward'):    self.lstm_fw_cell = rnn_cell.BasicLSTMCell(dim_hidden)   with tf.variable_scope('backward'):    self.lstm_bw_cell = rnn_cell.BasicLSTMCell(dim_hidden)

Otherwise there will be a name collision (both cells try to use the "BiRNN_FW/RNN/BasicLSTMCell/Linear/Matrix" name) and tf will interpret this as if your intention is to share the parameters in the two cells, which is not what you want. TF will throw an exception because you haven't explicitly told it to reuse variables in the second scope: with variable_scope(name, reuse=True).

Setting the variable scopes, as above, will create unique names for the variables:
BiRNN_FW/RNN/BasicLSTMCell/forward/Linear/Matrix
BiRNN_FW/RNN/BasicLSTMCell/backward/Linear/Matrix

       大意就是lstm cells 或者你遇见的什么东西有作用域,你会让后台代码不知道选哪个。

       我试了当lstm cell 保存时候因为作用域不一样会导致加载保存的W 值和B 值时候加载不到,当你声明的cell不是同一个的时候你可以照葫芦画瓢按照两个with 那个解决方法来定义不同的作用范围,当是同一个的时候一定要将不是第一个的cell 上面写上resuse = True 可以参考上面的代码片。就可以了。

       一定不要在第一个上面也写上,它会报找不到的错误的。



1 1
原创粉丝点击