gpt4 book ai didi

python - 模块 'tensorflow' 没有属性 'get_default_graph' - 我不需要任何图表

转载 作者:行者123 更新时间:2023-11-30 08:56:46 25 4
gpt4 key购买 nike

对于我的硕士学位,我正在尝试创建一个简单的神经网络。但我的代码中有一些错误,因此程序停止并且没有创建经过训练的模型。

我无法弄清楚错误消息想告诉我什么以及我需要在代码中更改什么。因此我需要你的帮助。我用谷歌搜索了这个错误,但既不理解,也无法用其他帖子提出的想法以任何方式解决我的错误。

谁能解释一下为什么tensorflow想要创建一个图以及框架怎么可能不知道它所需的函数?我只需要安装可视化包吗?是否可以忽略这个错误?

我不需要任何图形。但是计算机需要它来进行机器学习算法的分类和计算吗?

请原谅我糟糕的英语和我对 Tensorflow 的不了解。

提前致谢!

我已经安装了最新的tensorflow版本2.0.0-beta1,以及最新的keras版本。

此外,我尝试创建一些图表来显示分类过程。不起作用。

我还激活了逐步 Debug模式来找出我的问题。看来错误出现在我创建、训练和评估神经网络的evaluate_model函数内。

错误发生在模型创建过程中(model = Sequantial())。

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 3 16:26:14 2019

@author: mattdoe
"""

from data_preprocessor_db import data_storage # validation data
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import confusion_matrix
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import normalize
from numpy import mean
from numpy import std
from numpy import array



# create and evaluate a single multi-layer-perzeptron
def evaluate_model(Train, Test, Target_Train, Target_Test):
# define model
model = Sequential()
# input layer automatically created
model.add(Dense(9, input_dim=9, kernel_initializer='normal', activation='relu')) # 1st hidden layer
model.add(Dense(9, kernel_initializer='normal', activation='relu')) # 2nd hidden layer
model.add(Dense(9, activation='softmax')) #output layer

# create model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# fit model
model.fit(Train, to_categorical(Target_Train), epochs=50, verbose=0)

# evaluate the model
test_loss, test_acc = model.evaluate(Test, to_categorical(Target_Test), verbose=0)

# as well: create a confussion matrix
predicted = model.predict(Test)
conf_mat = confusion_matrix(Target_Test, predicted)

return model, test_acc, conf_mat



# for seperation of data_storage
# Link_ID = []
Input, Output = list(), list()

# list all results of k-fold cross-validation
scores, members, matrix = list(), list(), list()

# seperate data_storage in Input and Output data
for items in data_storage:
# Link_ID = items[0] # identifier not needed
Input.append([items[1], items[2], items[3], items[4], items[5], items[6], items[7], items[8], items[9]]) # Input: all characteristics
Output.append(items[10]) # Output: scenario_class 1 to 8

# change to numpy_array (scalar index array)
Input = array(Input)
Output = array(Output)

# normalize Data
Input = normalize(Input)
# Output = normalize(Output) not needed; categorical number

# prepare k-fold cross-validation
kfold = StratifiedKFold(n_splits=15, random_state=1, shuffle=True)

for train_ix, test_ix in kfold.split(Input, Output):
# select samples
Train, Target_Train = Input[train_ix], Output[train_ix]
Test, Target_Test = Input[test_ix], Output[test_ix]

# evaluate model
model, test_acc, conf_mat = evaluate_model(Train, Test, Target_Train, Target_Test)

# display each evalution result
print('>%.3f' % test_acc)

# add result to list
scores.append(test_acc)
members.append(model)
matrix.append(conf_mat)

# summarize expected performance
print('Estimated Accuracy %.3f (%.3f)' % (mean(scores), std(scores)))
# as well in confursion_matrix
print ('Confussion Matrix %' %(mean(matrix)))



# save model // trained neuronal network
model.save('neuronal_network_1.h5')

此回溯显示在 Spyder 中:

Traceback (most recent call last):

File "<ipython-input-12-25afb095a816>", line 1, in <module>
runfile('C:/Workspace/Master-Thesis/Programm/MapValidationML/ml_neuronal_network_1.py', wdir='C:/Workspace/Master-Thesis/Programm/MapValidationML')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Workspace/Master-Thesis/Programm/MapValidationML/ml_neuronal_network_1.py", line 77, in <module>
model, test_acc, conf_mat = evaluate_model(Train, Test, Target_Train, Target_Test)

File "C:/Workspace/Master-Thesis/Programm/MapValidationML/ml_neuronal_network_1.py", line 24, in evaluate_model
model = Sequential()

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 87, in __init__
super(Sequential, self).__init__(name=name)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 96, in __init__
self._init_subclassed_network(**kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 294, in _init_subclassed_network
self._base_init(name=name)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 109, in _base_init
name = prefix + '_' + str(K.get_uid(prefix))

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 74, in get_uid
graph = tf.get_default_graph()

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

最佳答案

如果您使用的是 tf 2.0 beta,请确保您的所有 keras 导入都是 tensorflow.keras... 任何 keras 导入都将采用假设为 tensorflow 1.4 的标准 keras 包。

即使用:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, ...

关于python - 模块 'tensorflow' 没有属性 'get_default_graph' - 我不需要任何图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56986100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com