AttributeError:模块“ tensorflow”在jupyter笔记本中没有属性“ get_default_graph”

时间:2019-12-08 12:07:45

标签: python tensorflow keras

我正在尝试为某些分类运行cnn模型,但是出现以下错误:

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

这些是我安装的软件包:

import numpy as np
from keras.models import load_model
import time
import pandas as pd
from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd 
import csv
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.models import Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers import Convolution2D, MaxPooling2D, Dropout , Flatten, Dense
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import StandardScaler

我已经将keras.models import Sequential更改为tensorflow.python.keras.models import Sequential,但这似乎也无济于事。

这是我收到的输出:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-5c123eb040bc> in <module>
     50 
     51 model = Sequential()
---> 52 model.add(Conv2D(32, (3, 3), padding="same",input_shape=(99,13,1),activation='relu'))
     53 model.add(MaxPooling2D(pool_size=(2, 2)))
     54 model.add(Conv2D(32, (3, 3), padding="same",input_shape=(99,13,1),activation='relu'))

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

/opt/anaconda3/envs/MlChallenge/lib/python3.6/site-packages/keras/layers/convolutional.py in __init__(self, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
    488             kernel_constraint=kernel_constraint,
    489             bias_constraint=bias_constraint,
--> 490             **kwargs)
    491 
    492     def get_config(self):

/opt/anaconda3/envs/MlChallenge/lib/python3.6/site-packages/keras/layers/convolutional.py in __init__(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
    103                  bias_constraint=None,
    104                  **kwargs):
--> 105         super(_Conv, self).__init__(**kwargs)
    106         self.rank = rank
    107         self.filters = filters

/opt/anaconda3/envs/MlChallenge/lib/python3.6/site-packages/keras/engine/base_layer.py in __init__(self, **kwargs)
    130         if not name:
    131             prefix = self.__class__.__name__
--> 132             name = _to_snake_case(prefix) + '_' + str(K.get_uid(prefix))
    133         self.name = name
    134 

/opt/anaconda3/envs/MlChallenge/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
     72     """
     73     global _GRAPH_UID_DICTS
---> 74     graph = tf.get_default_graph()
     75     if graph not in _GRAPH_UID_DICTS:
     76         _GRAPH_UID_DICTS[graph] = defaultdict(int)

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

我正在使用Tensorflow 2.0和keras 2.2.4,有人知道如何解决吗?

1 个答案:

答案 0 :(得分:0)

这里有多个问题:

  1. 您正在tf.keraskeras之间混合导入,不支持此操作,请选择一个包装并坚持使用
  2. Keras 2.2.4不支持TensorFlow 2.0,为此,您需要将Keras升级到2.3.x版,或将TensorFlow降级到1.x版。