我正在尝试构建深度学习模型,但是使用tensorflow时出现错误,并且无法解决此问题。错误是:
模块'tensorflow'没有属性'get_default_graph'
代码:
import csv
import cv2
import os
import numpy as np
import numpy as ndarray
import tensorflow as tf
import pandas as pd
from numpy import zeros, newaxis
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot as plt
from keras import layers
from keras.layers import Input, Add, Dense, Activation, ZeroPadding1D, BatchNormalization, Flatten, Conv1D, AveragePooling1D, MaxPooling1D, GlobalMaxPooling1D
from keras.models import Model, load_model
from keras.initializers import glorot_uniform
from tensorflow.keras import backend
from tensorflow.python.framework import ops
import tensorflow.keras
def IntelNet(input_shape = (22,1), classes = 2):
# Define the input as a tensor with shape input_shape
X_input = Input(input_shape)
# Zero-Padding
#X = ZeroPadding1D(padding=1)(X_input)
# Stage 1
X = Conv1D(128, 4, strides = 1, name = 'conv1',kernel_initializer='glorot_uniform', padding = 'SAME')(X_input)
X = BatchNormalization(name = 'bn_conv1')(X)
X = Activation('relu')(X)
#X = MaxPooling1D((3), strides=(2),padding = 'SAME')(X)
# output layer
X = Flatten()(X)
X = Dense(classes, activation='softmax')(X)
# Create model
model = Model(inputs = X_input, outputs = X, name='ResNet50')
return model
model = IntelNet(input_shape = (22,1), classes = 2)
错误:
AttributeError Traceback (most recent call last)
<ipython-input-132-27edc41dcb88> in <module>
----> 1 model = IntelNet(input_shape = (22,1), classes = 2)
<ipython-input-130-31ea437308f9> in IntelNet(input_shape, classes)
2 # Define the input as a tensor with shape input_shape
3
----> 4 X_input = Input(input_shape)
5
6
~\Anaconda3\lib\site-packages\keras\engine\input_layer.py in Input(shape, batch_shape, name, dtype, sparse, tensor)
176 name=name, dtype=dtype,
177 sparse=sparse,
--> 178 input_tensor=tensor)
179 # Return tensor including _keras_shape and _keras_history.
180 # Note that in this case train_output and test_output are the same pointer.
~\Anaconda3\lib\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
~\Anaconda3\lib\site-packages\keras\engine\input_layer.py in __init__(self, input_shape, batch_size, batch_input_shape, dtype, input_tensor, sparse, name)
37 if not name:
38 prefix = 'input'
---> 39 name = prefix + '_' + str(K.get_uid(prefix))
40 super(InputLayer, self).__init__(dtype=dtype, name=name)
41
~\Anaconda3\lib\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'
如何解决此问题?我在Python 3中使用Anaconda。