变量bidirectional_rnn / fw / lstm_cell / weights已经存在,不允许。你的意思是在VarScope中设置reuse = True吗?最初定义于:

时间:2017-05-10 15:15:40

标签: python tensorflow

当我调用函数tf.nn.bidirectional_dynamic_rnn时,它返回 变量bidirectional_rnn / fw / lstm_cell / weights已经存在,不允许。你的意思是在VarScope中设置reuse = True吗?最初定义于:

我的代码:

tf.reset_default_graph()
sess = tf.InteractiveSession()
PAD = 0
EOS = 1

sequence_size = 10 #vocal_size
input_embedding_size = 20 #length of sequence

encoder_hidden_units = 20
decoder_hidden_units = encoder_hidden_units * 2

encoder_inputs = tf.placeholder(shape=(None, None), dtype=tf.int32, 
name='encoder_inputs')
#length of each sequence in the batch
encoder_inputs_length = tf.placeholder(shape=(None,), dtype=tf.int32, 
name='encoder_inputs_length')
decoder_targets = tf.placeholder(shape=(None, None), dtype=tf.int32, 
name='decoder_targets')
embeddings = tf.Variable(tf.random_uniform([sequence_size, 
input_embedding_size], -1.0, 1), dtype=tf.float32 )
encoder_inputs_embedded = tf.nn.embedding_lookup(embeddings, 
encoder_inputs)

encoder_cell = tf.contrib.rnn.LSTMCell(encoder_hidden_units)
( (encoder_fw_outputs,
encoder_bw_outputs),
(encoder_fw_final_state,
encoder_bw_final_state)) = (
tf.nn.bidirectional_dynamic_rnn(cell_fw=encoder_cell,
                                cell_bw=encoder_cell,
                                inputs=encoder_inputs_embedded,
                                sequence_length=encoder_inputs_length,
                                dtype=tf.float32, time_major=False)
)

我认为这是错误:

ValueError                   Traceback (most recent call last)
<ipython-input-211-41a8d71d81df> in <module>()
 23                                     
inputs=encoder_inputs_embedded,
 24                                     
 sequence_length=encoder_inputs_length,
  ---> 25                                     dtype=tf.float32, 
 time_major=False)
 26     )
 27 

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/rnn.pyc in 
bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, sequence_length, 
initial_state_fw, initial_state_bw, dtype, parallel_iterations, 
swap_memory, time_major, scope)
348           initial_state=initial_state_fw, dtype=dtype,
349           parallel_iterations=parallel_iterations, 
swap_memory=swap_memory,
--> 350           time_major=time_major, scope=fw_scope)
351 
352     # Backward direction

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/rnn.pyc in dynamic_rnn(cell, inputs, 
sequence_length, initial_state, dtype, parallel_iterations, 
swap_memory, time_major, scope)
544         swap_memory=swap_memory,
545         sequence_length=sequence_length,
--> 546         dtype=dtype)
547 
548     # Outputs of _dynamic_rnn_loop are always shaped [time, batch, 
depth].

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/rnn.pyc in _dynamic_rnn_loop(cell, 
inputs, initial_state, parallel_iterations, swap_memory, 
sequence_length, dtype)
711       loop_vars=(time, output_ta, state),
712       parallel_iterations=parallel_iterations,
 --> 713       swap_memory=swap_memory)
714 
715   # Unpack final output if not using output tuples.

 /home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
 packages/tensorflow/python/ops/control_flow_ops.pyc in 
 while_loop(cond, body, loop_vars, shape_invariants, 
 parallel_iterations, back_prop, swap_memory, name)
 2603     context = WhileContext(parallel_iterations, back_prop, 
 swap_memory, name)
 2604     ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, context)
 -> 2605     result = context.BuildLoop(cond, body, loop_vars, 
 shape_invariants)
 2606     return result
  2607 

 /home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
 packages/tensorflow/python/ops/control_flow_ops.pyc in 
 BuildLoop(self, pred, body, loop_vars, shape_invariants)
 2436       self.Enter()
 2437       original_body_result, exit_vars = self._BuildLoop(
 -> 2438           pred, body, original_loop_vars, loop_vars, 
shape_invariants)
 2439     finally:
 2440       self.Exit()

 /home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
 packages/tensorflow/python/ops/control_flow_ops.pyc in 
_BuildLoop(self, pred, body, original_loop_vars, loop_vars, 
 shape_invariants)
 2386         structure=original_loop_vars,
 2387         flat_sequence=vars_for_body_with_tensor_arrays)
  -> 2388     body_result = body(*packed_vars_for_body)
 2389     if not nest.is_sequence(body_result):
 2390       body_result = [body_result]

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/rnn.pyc in _time_step(time, 
output_ta_t, state)
694           call_cell=call_cell,
695           state_size=state_size,
--> 696           skip_conditionals=True)
697     else:
698       (output, new_state) = call_cell()

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/rnn.pyc in _rnn_step(time, 
sequence_length, min_sequence_length, max_sequence_length, 
zero_output, state, call_cell, state_size, skip_conditionals)
175     # steps.  This is faster when max_seq_len is equal to the 
number of unrolls
176     # (which is typical for dynamic_rnn).
--> 177     new_output, new_state = call_cell()
178     nest.assert_same_structure(state, new_state)
179     new_state = nest.flatten(new_state)

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/rnn.pyc in <lambda>()
682 
683     input_t = nest.pack_sequence_as(structure=inputs, 
flat_sequence=input_t)
 --> 684     call_cell = lambda: cell(input_t, state)
685 
686     if sequence_length is not None:

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.pyc in 
__call__(self, inputs, state, scope)
336       # i = input_gate, j = new_input, f = forget_gate, o = 
output_gate
337       lstm_matrix = _linear([inputs, m_prev], 4 * self._num_units, 
bias=True,
 --> 338                             scope=scope)
339       i, j, f, o = array_ops.split(
340           value=lstm_matrix, num_or_size_splits=4, axis=1)

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.pyc in 
_linear(args, output_size, bias, bias_start, scope)
745   with vs.variable_scope(scope) as outer_scope:
746     weights = vs.get_variable(
 --> 747         "weights", [total_arg_size, output_size], 
 dtype=dtype)
748     if len(args) == 1:
749       res = math_ops.matmul(args[0], weights)

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/variable_scope.pyc in 
get_variable(name, shape, dtype, initializer, regularizer, trainable, 
collections, caching_device, partitioner, validate_shape, 
custom_getter)
986       collections=collections, caching_device=caching_device,
987       partitioner=partitioner, validate_shape=validate_shape,
 --> 988       custom_getter=custom_getter)
989 get_variable_or_local_docstring = (
990     """%s

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/variable_scope.pyc in 
get_variable(self, var_store, name, shape, dtype, initializer, 
regularizer, trainable, collections, caching_device, partitioner, 
validate_shape, custom_getter)
888           collections=collections, caching_device=caching_device,
889           partitioner=partitioner, validate_shape=validate_shape,
--> 890           custom_getter=custom_getter)
891 
892   def _get_partitioned_variable(self,

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/variable_scope.pyc in 
get_variable(self, name, shape, dtype, initializer, regularizer, 
reuse, trainable, collections, caching_device, partitioner, 
validate_shape, custom_getter)
346           reuse=reuse, trainable=trainable, 
collections=collections,
347           caching_device=caching_device, partitioner=partitioner,
--> 348           validate_shape=validate_shape)
349 
350   def _get_partitioned_variable(

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/variable_scope.pyc in 
_true_getter(name, shape, dtype, initializer, regularizer, reuse, 
trainable, collections, caching_device, partitioner, validate_shape)
331           initializer=initializer, regularizer=regularizer, 
reuse=reuse,
332           trainable=trainable, collections=collections,
--> 333           caching_device=caching_device, 
validate_shape=validate_shape)
334 
335     if custom_getter is not None:

/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
packages/tensorflow/python/ops/variable_scope.pyc in 
_get_single_variable(self, name, shape, dtype, initializer, 
regularizer, partition_info, reuse, trainable, collections, 
caching_device, validate_shape)
637                          " Did you mean to set reuse=True in 
VarScope? "
638                          "Originally defined at:\n\n%s" % (
--> 639                              name, 
"".join(traceback.format_list(tb))))
640       found_var = self._vars[name]
641       if not shape.is_compatible_with(found_var.get_shape()):

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

File "/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/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 "/home/cesar/anaconda2/envs/tensorflow/lib/python2.7/site-
 packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py",      
 line 338, in __call__
 scope=scope)
 File "<ipython-input-23-f4f28501e56f>", line 24, in <module>
 time_major=True

2 个答案:

答案 0 :(得分:0)

我会尝试按如下方式定义编码器:

with tf.variable_scope('encoder_cell_fw'):
    encoder_cell_fw = LSTMCell(encoder_hidden_units)   
with tf.variable_scope('encoder_cell_bw'):
    encoder_cell_bw = LSTMCell(encoder_hidden_units) 

( (encoder_fw_outputs,
encoder_bw_outputs),
(encoder_fw_final_state,
encoder_bw_final_state)) = (
tf.nn.bidirectional_dynamic_rnn(cell_fw=encoder_cell_fw,
                            cell_bw=encoder_cell_bw,
                            inputs=encoder_inputs_embedded,
                            sequence_length=encoder_inputs_length,
                            dtype=tf.float32, time_major=False))

它适用于我,这是因为如果它没有设法两次使用相同的名称,bw是fw,另一个选项将使用reuse = True定义,但它没有用。

答案 1 :(得分:0)

尝试这种方式: 在块的开头插入tf.reset_default_graph()。 通过在同一图表上多次运行脚本,您可以将变量添加到图表中以进行每次运行...