首页 > 解决方案 > Tensorflow VGG16属性错误:模块tensorflow没有属性get_default_graph?

问题描述

大家好,我刚开始学习机器学习并尝试在Jupyter Notebook. 我正在尝试运行以下代码。我用谷歌搜索了错误并将tensorflow版本更新为 2.3.1 和kersa2.1.6。但它仍然抛出这个TypeError。我看到还有另一种方法可以通过删除参数来修改 python 源代码axis。但是如何更改 vgg16 源代码Jupyter Notebook?我试图进入/use/local/lib我的 macbook,但找不到 python 源文件。希望有人能帮忙!干杯

代码:

from keras.application.vgg16 import VGG16
vggmodel = VGG16(weight='imagenet', include_top=True)
vggmodel.summary()

更新:我找到了源文件并删除了轴参数。但是现在又抛出了另一个错误。

Tensorflow Attribute Error: module tensorflow has no attribute get_default_graph


AttributeError                            Traceback (most recent call last)
<ipython-input-14-5b351c8334f5> in <module>
----> 1 vggmodel = VGG16(weights='imagenet', include_top=True)
      2 vggmodel.summary()

~/.local/lib/python3.7/site-packages/keras/applications/vgg16.py in VGG16(include_top, weights, input_tensor, input_shape, pooling, classes)
    107 
    108     if input_tensor is None:
--> 109         img_input = Input(shape=input_shape)
    110     else:
    111         if not K.is_keras_tensor(input_tensor):

~/.local/lib/python3.7/site-packages/keras/engine/topology.py in Input(shape, batch_shape, name, dtype, sparse, tensor)
   1455                              name=name, dtype=dtype,
   1456                              sparse=sparse,
-> 1457                              input_tensor=tensor)
   1458     # Return tensor including _keras_shape and _keras_history.
   1459     # Note that in this case train_output and test_output are the same pointer.

~/.local/lib/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name +
     90                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~/.local/lib/python3.7/site-packages/keras/engine/topology.py in __init__(self, input_shape, batch_size, batch_input_shape, dtype, input_tensor, sparse, name)
   1317         if not name:
   1318             prefix = 'input'
-> 1319             name = prefix + '_' + str(K.get_uid(prefix))
   1320         super(InputLayer, self).__init__(dtype=dtype, name=name)
   1321 

~/.local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
     66     """
     67     global _GRAPH_UID_DICTS
---> 68     graph = tf.get_default_graph()
     69     if graph not in _GRAPH_UID_DICTS:
     70         _GRAPH_UID_DICTS[graph] = defaultdict(int)

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

标签: pythontensorflowmachine-learningkerasconv-neural-network

解决方案


推荐阅读