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

Python Jupyter笔记本中的TensorFlow变量重用错误,python,tensorflow,jupyter-notebook,Python,Tensorflow,Jupyter Notebook,我试图在我的机器上运行Kaggle教程中的以下代码(Windows 10 Home v.1709 build 16299.248、Python 3.6.3、JupyterLab 0.31.8): 我得到了以下错误: ValueError Traceback (most recent call last) <ipython-input-2-7f139a1e52fa> in <module>() 7

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

我得到了以下错误:

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)
ValueError回溯(最近一次调用)
在()
7.
8 my_new_model=顺序()
---->9 my_new_model.add(ResNet50(include_top=False,pooling='avg',weights=resnet_weights_path))
10 my_new_model.add(密集型(num_类,activation='softmax'))
添加中的~\Anaconda3\lib\site packages\keras\models.py(self,layer)
465#并创建连接当前层的节点
466#到我们刚刚创建的输入层。
-->467层(x)
468
469如果len(层.\u入站\u节点[-1]。输出\u张量)!=1:
调用中的~\Anaconda3\lib\site packages\keras\engine\topology.py(self,inputs,**kwargs)
615
616#实际调用层,收集输出、掩码和形状。
-->617输出=自调用(输入,**kwargs)
618输出屏蔽=自计算屏蔽(输入,前一个屏蔽)
619
调用中的~\Anaconda3\lib\site packages\keras\engine\topology.py(self、输入、掩码)
2076返回自。\u输出\u张量\u缓存[缓存\u键]
2077其他:
->2078输出张量,运行内部图(输入,掩码)
2079返回输出张量
2080
运行内部图中的~\Anaconda3\lib\site packages\keras\engine\topology.py(自身、输入、掩码)
2227如果“面具”不在kwargs中:
2228 kwargs['mask']=计算的\u mask
->2229输出_张量=_到_列表(layer.call(计算的_张量,**kwargs))
2230输出\u掩码=\u到\u列表(layer.compute\u mask(computed\u tensor,
2231(U掩码)
调用中的~\Anaconda3\lib\site packages\keras\layers\normalization.py(自我、输入、培训)
183.添加更新([K.移动平均值更新(self.moving)是指,
184意味着,
-->185.自我动量),
186 K.移动平均值更新(自移动方差,
187差异,
移动平均更新中的~\Anaconda3\lib\site packages\keras\backend\tensorflow\u backend.py(x,值,动量)
999     """
1000返回移动平均值。分配移动平均值(
->1001 x,值,动量,零(debias=真)
1002
1003
~\Anaconda3\lib\site packages\tensorflow\python\training\moving\u averages.py in assign\u moving\u average(变量、值、衰减、零debias、名称)
68 decage=math_ops.cast(decage,variable.dtype.base_dtype)
69如果为零,则:
--->70更新增量=\u零\u debias(变量、值、衰减)
71.其他:
72更新δ=(变量-值)*衰减
~\Anaconda3\lib\site packages\tensorflow\python\training\moving\u averages.py in\u zero\u debias(无偏变量、值、衰减)
178局部步骤初始值设定项=初始操作零初始值设定项()
179偏置变量=变量范围。获取变量(
-->180“有偏”,初始值设定项=有偏(初始值设定项,可训练=假)
181 local_step=变量_scope.get_变量(
182“本地步骤”,
get\u变量中的~\Anaconda3\lib\site packages\tensorflow\python\ops\variable\u scope.py(名称、形状、数据类型、初始化器、正则化器、可训练、集合、缓存设备、分区器、验证形状、使用资源、自定义获取程序)
1047集合=集合,缓存设备=缓存设备,
1048分割器=分割器,验证形状=验证形状,
->1049使用资源=使用资源,自定义获取者=自定义获取者)
1050获取变量或局部文档字符串=(
1051“”%s
get\u变量中的~\Anaconda3\lib\site packages\tensorflow\python\ops\variable\u scope.py(self、var\u存储、名称、形状、数据类型、初始化器、正则化器、可训练、集合、缓存\u设备、分区器、验证\u形状、使用\u资源、自定义\u getter)
946集合=集合,缓存设备=缓存设备,
947分区器=分区器,验证形状=验证形状,
-->948使用资源=使用资源,自定义获取者=自定义获取者)
949
950 def_get_partitioned_变量(self,
get\u变量中的~\Anaconda3\lib\site packages\tensorflow\python\ops\variable\u scope.py(self、name、shape、dtype、初始化器、正则化器、重用、可训练、集合、缓存\u设备、分区器、验证\u shape、使用\u资源、自定义\u getter)
354重用=重用,可培训=可培训,集合=集合,
355缓存设备=缓存设备,分区器=分区器,
-->356验证形状=验证形状,使用资源=使用资源)
357
358 def_get_partitioned_变量(
~\Anaconda3\lib\site packages\tensorflow\python\ops\variable\u scope.py in\u true\u getter(名称、形状、数据类型、初始化器、正则化器、重用、可训练、集合、缓存设备、分区器、验证形状、使用资源)
339可培训=可培训,集合=集合,
340缓存设备=缓存设备,验证形状=验证形状,
-->341使用资源=使用资源)
342
343如果custom_getter不是None:
~\Anaconda3\lib\site packages\tensorflow\python\ops\variable\u scope.py in\u get\u single\u variable(self、name、shape、dtype、initializer、regularizer、partition\u info、重用、可培训、集合、缓存设备、验证\u shape、使用\u资源)
651“您的意思是在VarScope中设置reuse=True吗?”
652“最初定义在:\n\n%s”%(
-->653名称“.”。加入(回溯)
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)