Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 adam优化器的Tensorflow变量\u范围?_Python 2.7_Tensorflow_Scope_Lstm_Rnn - Fatal编程技术网

Python 2.7 adam优化器的Tensorflow变量\u范围?

Python 2.7 adam优化器的Tensorflow变量\u范围?,python-2.7,tensorflow,scope,lstm,rnn,Python 2.7,Tensorflow,Scope,Lstm,Rnn,版本:Python2.7.13和TF1.2.1 背景:我试图创建一个LSTM单元,并传递一个nxm的输入和nxm+1的输出。我想通过softmax层传递输出,然后通过Adam优化器,其损失函数为负对数似然 问题:如标题所述,当我试图设置我的training_op=optimizer.minimizell时,它崩溃并询问变量范围。我该怎么办 代码: with tf.variable_scope('lstm1', reuse=True): LSTM_cell_1 = tf.nn.rnn_ce

版本:Python2.7.13和TF1.2.1

背景:我试图创建一个LSTM单元,并传递一个nxm的输入和nxm+1的输出。我想通过softmax层传递输出,然后通过Adam优化器,其损失函数为负对数似然

问题:如标题所述,当我试图设置我的training_op=optimizer.minimizell时,它崩溃并询问变量范围。我该怎么办

代码:

with tf.variable_scope('lstm1', reuse=True):
    LSTM_cell_1 = tf.nn.rnn_cell.LSTMCell(num_units=n_neurons, activation=tf.nn.relu)
    rnn_outputs_1, states_1 = tf.nn.dynamic_rnn(LSTM_cell_1, X_1, dtype=tf.float32)
    rnn_outputs_1 = tf.nn.softmax(rnn_outputs_1)
    stacked_rnn_outputs_1 = tf.reshape(rnn_outputs_1, [-1, n_neurons])
    stacked_outputs_1 = tf.layers.dense(stacked_rnn_outputs_1, n_outputs)
    outputs_1 = tf.reshape(stacked_outputs_1, [-1, n_steps, n_outputs])

mu = tf.Variable(np.float32(1))
sigma = tf.Variable(np.float32(1))

def normal_log(X, mu, sigma, left=-np.inf, right=np.inf):
    val = -tf.log(tf.constant(np.sqrt(2.0 * np.pi), dtype=tf.float32) * sigma) - \
       tf.pow(X - mu, 2) / (tf.constant(2.0, dtype=tf.float32) * tf.pow(sigma, 2))
    return val

nll = -tf.reduce_sum(normal_log(outputs, mu, sigma))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(nll)
错误消息:

ValueError                                Traceback (most recent call last)

/usr/local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.pyc in minimize(self, loss, global_step, var_list, gate_gradients, aggregation_method, colocate_gradients_with_ops, name, grad_loss)
323 
324     return self.apply_gradients(grads_and_vars, global_step=global_step,
--> 325                                 name=name)
326 
327   def compute_gradients(self, loss, var_list=None,

/usr/local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.pyc in apply_gradients(self, grads_and_vars, global_step, name)
444                        ([str(v) for _, _, v in converted_grads_and_vars],))
445     with ops.control_dependencies(None):
--> 446       self._create_slots([_get_variable_for(v) for v in var_list])
447     update_ops = []
448     with ops.name_scope(name, self._name) as name:

/usr/local/lib/python2.7/site-packages/tensorflow/python/training/adam.pyc in _create_slots(self, var_list)
126     # Create slots for the first and second moments.
127     for v in var_list:
--> 128       self._zeros_slot(v, "m", self._name)
129       self._zeros_slot(v, "v", self._name)
130 

/usr/local/lib/python2.7/site-packages/tensorflow/python/training/optimizer.pyc in _zeros_slot(self, var, slot_name, op_name)
764     named_slots = self._slot_dict(slot_name)
765     if _var_key(var) not in named_slots:
--> 766       named_slots[_var_key(var)] = slot_creator.create_zeros_slot(var, op_name)
767     return named_slots[_var_key(var)]

/usr/local/lib/python2.7/site-packages/tensorflow/python/training/slot_creator.pyc in create_zeros_slot(primary, name, dtype, colocate_with_primary)
172     return create_slot_with_initializer(
173         primary, initializer, slot_shape, dtype, name,
--> 174         colocate_with_primary=colocate_with_primary)
175   else:
176     val = array_ops.zeros(slot_shape, dtype=dtype)

/usr/local/lib/python2.7/site-packages/tensorflow/python/training/slot_creator.pyc in create_slot_with_initializer(primary, initializer, shape, dtype, name, colocate_with_primary)
144       with ops.colocate_with(primary):
145         return _create_slot_var(primary, initializer, "", validate_shape, shape,
--> 146                                 dtype)
147     else:
148       return _create_slot_var(primary, initializer, "", validate_shape, shape,

/usr/local/lib/python2.7/site-packages/tensorflow/python/training/slot_creator.pyc in _create_slot_var(primary, val, scope, validate_shape, shape, dtype)
 64       use_resource=_is_resource(primary),
 65       shape=shape, dtype=dtype,
---> 66       validate_shape=validate_shape)
 67   variable_scope.get_variable_scope().set_partitioner(current_partitioner)
 68 

/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.pyc in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
960           collections=collections, caching_device=caching_device,
961           partitioner=partitioner, validate_shape=validate_shape,
--> 962           use_resource=use_resource, custom_getter=custom_getter)
963 
964   def _get_partitioned_variable(self,

/usr/local/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, use_resource, custom_getter)
365           reuse=reuse, trainable=trainable, collections=collections,
366           caching_device=caching_device, partitioner=partitioner,
--> 367           validate_shape=validate_shape, use_resource=use_resource)
368 
369   def _get_partitioned_variable(

/usr/local/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, use_resource)
350           trainable=trainable, collections=collections,
351           caching_device=caching_device, validate_shape=validate_shape,
--> 352           use_resource=use_resource)
353 
354     if custom_getter is not None:

/usr/local/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, use_resource)
662                          " Did you mean to set reuse=True in VarScope? "
663                          "Originally defined at:\n\n%s" % (
--> 664                              name, "".join(traceback.format_list(tb))))
665       found_var = self._vars[name]
666       if not shape.is_compatible_with(found_var.get_shape()):

ValueError: Variable lstm1/dense/kernel/Adam_1/ already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

File "<ipython-input-107-eed033b85dc0>", line 11, in <module>
training_op = optimizer.minimize(nll)
File "/usr/local/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "/usr/local/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
if self.run_code(code, result):

原来我在一个Python笔记本里一遍又一遍地执行这个部分,所以对所有tf新手来说,记住每次我有3个并行的LSTM时都要重置内核,这就解释了编号的输出。输出只是所有3个单一输出的串联版本。请提供完整的错误消息。完整的错误消息@AlexanderSvetkin