python - Jupyter 笔记本中的 TensorFlow 变量重用错误

标签 python tensorflow jupyter-notebook

我尝试在我的计算机上运行 Kaggle 教程中的以下代码(Windows 10 Home v.1709 build 16299.248、Python 3.6.3、JupyterLab 0.31.8):

from keras.models import Sequential
from keras.layers import Dense, Flatten, GlobalAveragePooling2D
from keras.applications import ResNet50

num_classes = 2
resnet_weights_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'

my_new_model = Sequential()
my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
my_new_model.add(Dense(num_classes, activation='softmax'))

我收到以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-2-7f139a1e52fa> in <module>()
      7 
      8 my_new_model = Sequential()
----> 9 my_new_model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
     10 my_new_model.add(Dense(num_classes, activation='softmax'))

~\Anaconda3\lib\site-packages\keras\models.py in add(self, layer)
    465                 # and create the node connecting the current layer
    466                 # to the input layer we just created.
--> 467                 layer(x)
    468 
    469             if len(layer._inbound_nodes[-1].output_tensors) != 1:

~\Anaconda3\lib\site-packages\keras\engine\topology.py in __call__(self, inputs, **kwargs)
    615 
    616             # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 617             output = self.call(inputs, **kwargs)
    618             output_mask = self.compute_mask(inputs, previous_mask)
    619 

~\Anaconda3\lib\site-packages\keras\engine\topology.py in call(self, inputs, mask)
   2076             return self._output_tensor_cache[cache_key]
   2077         else:
-> 2078             output_tensors, _, _ = self.run_internal_graph(inputs, masks)
   2079             return output_tensors
   2080 

~\Anaconda3\lib\site-packages\keras\engine\topology.py in run_internal_graph(self, inputs, masks)
   2227                                 if 'mask' not in kwargs:
   2228                                     kwargs['mask'] = computed_mask
-> 2229                             output_tensors = _to_list(layer.call(computed_tensor, **kwargs))
   2230                             output_masks = _to_list(layer.compute_mask(computed_tensor,
   2231                                                                        computed_mask))

~\Anaconda3\lib\site-packages\keras\layers\normalization.py in call(self, inputs, training)
    183         self.add_update([K.moving_average_update(self.moving_mean,
    184                                                  mean,
--> 185                                                  self.momentum),
    186                          K.moving_average_update(self.moving_variance,
    187                                                  variance,

~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in moving_average_update(x, value, momentum)
    999     """
   1000     return moving_averages.assign_moving_average(
-> 1001         x, value, momentum, zero_debias=True)
   1002 
   1003 

~\Anaconda3\lib\site-packages\tensorflow\python\training\moving_averages.py in assign_moving_average(variable, value, decay, zero_debias, name)
     68         decay = math_ops.cast(decay, variable.dtype.base_dtype)
     69       if zero_debias:
---> 70         update_delta = _zero_debias(variable, value, decay)
     71       else:
     72         update_delta = (variable - value) * decay

~\Anaconda3\lib\site-packages\tensorflow\python\training\moving_averages.py in _zero_debias(unbiased_var, value, decay)
    178         local_step_initializer = init_ops.zeros_initializer()
    179       biased_var = variable_scope.get_variable(
--> 180           "biased", initializer=biased_initializer, trainable=False)
    181       local_step = variable_scope.get_variable(
    182           "local_step",

~\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
   1047       collections=collections, caching_device=caching_device,
   1048       partitioner=partitioner, validate_shape=validate_shape,
-> 1049       use_resource=use_resource, custom_getter=custom_getter)
   1050 get_variable_or_local_docstring = (
   1051     """%s

~\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
    946           collections=collections, caching_device=caching_device,
    947           partitioner=partitioner, validate_shape=validate_shape,
--> 948           use_resource=use_resource, custom_getter=custom_getter)
    949 
    950   def _get_partitioned_variable(self,

~\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
    354           reuse=reuse, trainable=trainable, collections=collections,
    355           caching_device=caching_device, partitioner=partitioner,
--> 356           validate_shape=validate_shape, use_resource=use_resource)
    357 
    358   def _get_partitioned_variable(

~\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource)
    339           trainable=trainable, collections=collections,
    340           caching_device=caching_device, validate_shape=validate_shape,
--> 341           use_resource=use_resource)
    342 
    343     if custom_getter is not None:

~\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape, use_resource)
    651                          " Did you mean to set reuse=True in VarScope? "
    652                          "Originally defined at:\n\n%s" % (
--> 653                              name, "".join(traceback.format_list(tb))))
    654       found_var = self._vars[name]
    655       if not shape.is_compatible_with(found_var.get_shape()):

ValueError: Variable bn_conv1/moving_mean/biased already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  File "C:\Users\felix\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1228, in __init__
    self._traceback = _extract_stack()
  File "C:\Users\felix\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2336, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Users\felix\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
    op_def=op_def)

我读过很多关于 TensorFlow 中变量重用的内容,但上面的代码似乎在 Kaggle 的在线内核上运行得很好。任何提示将不胜感激!

最佳答案

您收到此错误是因为您运行定义模型的单元两次。由于代码在内部使用 tf.get_variable,它会提示该变量已在图中定义。

如果您想创建新模型(例如第二次运行模型单元),您可能需要使用 tf.reset_default_graph 重置默认图表。

关于python - Jupyter 笔记本中的 TensorFlow 变量重用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959947/

相关文章:

python - 如何使用 Keras 创建多输入一输出 LSTM 模型?

python - JupyterLab 中的 pdb 未进入交互模式

python - ipython 笔记本不工作 : OSError: [Errno None not found] 2

python - 流程完成后如何更新切换按钮的状态?

Python 集合与数组

python - 导入错误 : No module named 'utils'

python - 无法在 ipython 笔记本上显示图表

python - Ubuntu 和 Django,没有名为 'django' 的模块

python - 在python中查找最大有符号短整数

machine-learning - Tensorflow 中的成本敏感型学习