InvalidArgumentError: assertion failed: [0] [Op:Assert] name: EagerVariableNameReuse

 

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Cell In[2], line 32
     25 y_train = np.array(y_train)
     27 model = tf.keras.Sequential([
     28     SimpleRNN(3),
     29     Dense(5, activation='softmax')
     30 ])
---> 32 model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
     33               loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
     34               metrics=['sparse_categorical_accuracy'])
     36 checkpoint_save_path = "./rnn_onehot_1pre1.ckpt"
     38 if os.path.exists(checkpoint_save_path + '.index'):

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:329, in Model.compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, **kwargs)
    326 self._run_eagerly = kwargs.pop('run_eagerly', None)
    328 self.optimizer = self._get_optimizer(optimizer)
--> 329 self.compiled_loss = compile_utils.LossesContainer(
    330     loss, loss_weights, output_names=self.output_names)
    331 self.compiled_metrics = compile_utils.MetricsContainer(
    332     metrics, weighted_metrics, output_names=self.output_names)
    334 # Initializes attrs that are reset each time `compile` is called.

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/compile_utils.py:120, in LossesContainer.__init__(self, losses, loss_weights, output_names)
    118 self._loss_weights = loss_weights
    119 self._per_output_metrics = None  # Per-output losses become metrics.
--> 120 self._loss_metric = metrics_mod.Mean(name='loss')  # Total loss.
    121 self._built = False

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/keras/metrics.py:470, in Mean.__init__(self, name, dtype)
    463 def __init__(self, name='mean', dtype=None):
    464   """Creates a `Mean` instance.
    465 
    466   Args:
    467     name: (Optional) string name of the metric instance.
    468     dtype: (Optional) data type of the metric result.
    469   """
--> 470   super(Mean, self).__init__(
    471       reduction=metrics_utils.Reduction.WEIGHTED_MEAN, name=name, dtype=dtype)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/keras/metrics.py:307, in Reduce.__init__(self, reduction, name, dtype)
    305 self.reduction = reduction
    306 with ops.init_scope():
--> 307   self.total = self.add_weight(
    308       'total', initializer=init_ops.zeros_initializer)
    309   if reduction in [metrics_utils.Reduction.SUM_OVER_BATCH_SIZE,
    310                    metrics_utils.Reduction.WEIGHTED_MEAN]:
    311     self.count = self.add_weight(
    312         'count', initializer=init_ops.zeros_initializer)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/keras/metrics.py:276, in Metric.add_weight(self, name, shape, aggregation, synchronization, initializer, dtype)
    273 if distributed_training_utils.is_tpu_strategy(strategy):
    274   synchronization = tf_variables.VariableSynchronization.ON_WRITE
--> 276 return super(Metric, self).add_weight(
    277     name=name,
    278     shape=shape,
    279     dtype=self._dtype if dtype is None else dtype,
    280     trainable=False,
    281     initializer=initializer,
    282     collections=[],
    283     synchronization=synchronization,
    284     aggregation=aggregation)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:560, in Layer.add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint, partitioner, use_resource, synchronization, aggregation, **kwargs)
    556     tf_logging.warn('`caching_device` does not work with mixed precision '
    557                     'API. Ignoring user specified `caching_device`.')
    558     caching_device = None
--> 560 variable = self._add_variable_with_custom_getter(
    561     name=name,
    562     shape=shape,
    563     # TODO(allenl): a `make_variable` equivalent should be added as a
    564     # `Trackable` method.
    565     getter=getter,
    566     # Manage errors in Layer rather than Trackable.
    567     overwrite=True,
    568     initializer=initializer,
    569     dtype=dtype,
    570     constraint=constraint,
    571     trainable=trainable,
    572     partitioner=partitioner,
    573     use_resource=use_resource,
    574     collections=collections_arg,
    575     synchronization=synchronization,
    576     aggregation=aggregation,
    577     caching_device=caching_device)
    578 if regularizer is not None:
    579   # TODO(fchollet): in the future, this should be handled at the
    580   # level of variable creation, and weight regularization losses
    581   # should be variable attributes.
    582   name_in_scope = variable.name[:variable.name.find(':')]

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py:738, in Trackable._add_variable_with_custom_getter(self, name, shape, dtype, initializer, getter, overwrite, **kwargs_for_getter)
    736     initializer = checkpoint_initializer
    737     shape = None
--> 738 new_variable = getter(
    739     name=name,
    740     shape=shape,
    741 dtype=dtype,
    742 initializer=initializer,
    743 **kwargs_for_getter)
    745 # If we set an initializer and the variable processed it, tracking will not
    746 # assign again. It will add this variable to our dependencies, and if there
    747 # is a non-trivial restoration queued, it will handle that. This also
    748 # handles slot variables.
    749 if not overwrite or isinstance(new_variable, Trackable):

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_utils.py:129, in make_variable(name, shape, dtype, initializer, trainable, caching_device, validate_shape, constraint, use_resource, collections, synchronization, aggregation, partitioner)
    126 # TODO(apassos,rohanj) figure out how to remove collections from here so we
    127 # can remove the V1.
    128 variable_shape = tensor_shape.TensorShape(shape)
--> 129 return tf_variables.VariableV1(
    130 initial_value=init_val,
    131 name=name,
    132 trainable=trainable,
    133 caching_device=caching_device,
    134 dtype=variable_dtype,
    135 validate_shape=validate_shape,
    136 constraint=constraint,
    137 use_resource=use_resource,
    138 collections=collections,
    139 synchronization=synchronization,
    140 aggregation=aggregation,
    141 shape=variable_shapeifvariable_shapeelseNone)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:259, in VariableMetaclass.__call__(cls, *args, **kwargs)
    257 def __call__(cls, *args, **kwargs):
    258   if cls is VariableV1:
--> 259     return cls._variable_v1_call(*args,**kwargs)
    260   elif cls is Variable:
    261     return cls._variable_v2_call(*args, **kwargs)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:205, in VariableMetaclass._variable_v1_call(cls, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope, constraint, use_resource, synchronization, aggregation, shape)
    203 if aggregation is None:
    204   aggregation = VariableAggregation.NONE
--> 205 return previous_getter(
    206 initial_value=initial_value,
    207 trainable=trainable,
    208 collections=collections,
    209 validate_shape=validate_shape,
    210 caching_device=caching_device,
    211 name=name,
    212 variable_def=variable_def,
    213 dtype=dtype,
    214 expected_shape=expected_shape,
    215 import_scope=import_scope,
    216 constraint=constraint,
    217 use_resource=use_resource,
    218 synchronization=synchronization,
    219 aggregation=aggregation,
    220 shape=shape)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:66, in _make_getter.<locals>.getter(**kwargs)
     65 def getter(**kwargs):
---> 66   return captured_getter(captured_previous,**kwargs)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2562, in _DefaultDistributionContext.__init__.<locals>.creator(next_creator, **kwargs)
   2560 def creator(next_creator, **kwargs):
   2561   _require_strategy_scope_strategy(strategy)
-> 2562   return next_creator(**kwargs)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:198, in VariableMetaclass._variable_v1_call.<locals>.<lambda>(**kwargs)
    181 def _variable_v1_call(cls,
    182                       initial_value=None,
    183                       trainable=None,
   (...)
    195                       aggregation=VariableAggregation.NONE,
    196                       shape=None):
    197   """Call on Variable class. Useful to force the signature."""
--> 198   previous_getter = lambda **kwargs: default_variable_creator(None,**kwargs)
    199   for _, getter in ops.get_default_graph()._variable_creator_stack:  # pylint: disable=protected-access
    200     previous_getter = _make_getter(getter, previous_getter)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py:2584, in default_variable_creator(next_creator, **kwargs)
   2582 if use_resource:
   2583   distribute_strategy = kwargs.get("distribute_strategy", None)
-> 2584   return resource_variable_ops.ResourceVariable(
   2585 initial_value=initial_value,
   2586 trainable=trainable,
   2587 collections=collections,
   2588 validate_shape=validate_shape,
   2589 caching_device=caching_device,
   2590 name=name,
   2591 dtype=dtype,
   2592 constraint=constraint,
   2593 variable_def=variable_def,
   2594 import_scope=import_scope,
   2595 distribute_strategy=distribute_strategy,
   2596 synchronization=synchronization,
   2597 aggregation=aggregation,
   2598 shape=shape)
   2599 else:
   2600   return variables.RefVariable(
   2601       initial_value=initial_value,
   2602       trainable=trainable,
   (...)
   2613       aggregation=aggregation,
   2614       shape=shape)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/variables.py:263, in VariableMetaclass.__call__(cls, *args, **kwargs)
    261   return cls._variable_v2_call(*args, **kwargs)
    262 else:
--> 263   return super(VariableMetaclass,cls).__call__(*args,**kwargs)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py:1423, in ResourceVariable.__init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, variable_def, import_scope, constraint, distribute_strategy, synchronization, aggregation, shape)
   1421   self._init_from_proto(variable_def, import_scope=import_scope)
   1422 else:
-> 1423   self._init_from_args(
   1424 initial_value=initial_value,
   1425 trainable=trainable,
   1426 collections=collections,
   1427 caching_device=caching_device,
   1428 name=name,
   1429 dtype=dtype,
   1430 constraint=constraint,
   1431 synchronization=synchronization,
   1432 aggregation=aggregation,
   1433 shape=shape,
   1434 distribute_strategy=distribute_strategy)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py:1577, in ResourceVariable._init_from_args(self, initial_value, trainable, collections, caching_device, name, dtype, constraint, synchronization, aggregation, distribute_strategy, shape)
   1575   else:
   1576     shape = initial_value.shape
-> 1577   handle = eager_safe_variable_handle(
   1578 initial_value=initial_value,
   1579 shape=shape,
   1580 shared_name=shared_name,
   1581 name=name,
   1582 graph_mode=self._in_graph_mode)
   1583 # pylint: disable=protected-access
   1584 if (self._in_graph_mode and initial_value is not None and
   1585     initial_value.op._get_control_flow_context() is not None):

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py:242, in eager_safe_variable_handle(initial_value, shape, shared_name, name, graph_mode)
    200 """Creates a variable handle with information to do shape inference.
    201 
    202 The dtype is read from `initial_value` and stored in the returned
   (...)
    239   The handle, a `Tensor` of type `resource`.
    240 """
    241 dtype = initial_value.dtype.base_dtype
--> 242 return _variable_handle_from_shape_and_dtype(
    243 shape,dtype,shared_name,name,graph_mode,initial_value)

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/resource_variable_ops.py:174, in _variable_handle_from_shape_and_dtype(shape, dtype, shared_name, name, graph_mode, initial_value)
    169 exists = gen_resource_variable_ops.var_is_initialized_op(handle)
    171 # We create an assert Op instead of checking right away in order to be
    172 # compatible with ASYNC execution mode. Further, since not all devices
    173 # support string tensors, we encode the assertion string in the Op name
--> 174 gen_logging_ops._assert(# pylint: disable=protected-access
    175 math_ops.logical_not(exists),[exists],name="EagerVariableNameReuse")
    177 handle_data = cpp_shape_inference_pb2.CppShapeInferenceResult.HandleData()
    178 handle_data.is_set = True

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/ops/gen_logging_ops.py:55, in _assert(condition, data, summarize, name)
     53       pass  # Add nodes to the TensorFlow graph.
     54   except _core._NotOkStatusException as e:
---> 55     _ops.raise_from_not_ok_status(e,name)
     56 # Add nodes to the TensorFlow graph.
     57 if summarize is None:

File /home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:6653, in raise_from_not_ok_status(e, name)
   6651 message = e.message + (" name: " + name if name is not None else "")
   6652 # pylint: disable=protected-access
-> 6653 six.raise_from(core._status_to_exception(e.code,message),None)

File <string>:3, in raise_from(value, from_value)

InvalidArgumentError: assertion failed: [0] [Op:Assert] name: EagerVariableNameReuse

 

原因1:

GPU内存不够

原因2:

tensflorw版本号2.3不兼容,建议换成2.1

1删除当前tensorflow2.3版本
pip uninstall tensorflow-gpu==2.3.1
如果没有删除掉,可以先用命令行查看自己tf2的信息
pip list
然后删除对应的tensorflow信息


2安装tensorflow2.0.1版本
pip install tensorflow-gpu==2.0.1 -i https://pypi.douban.com/simple

 

posted @ 2023-02-15 23:56  emanlee  阅读(102)  评论(0编辑  收藏  举报