对于我的硕士学位,我正在尝试创建一个简单的神经元网络。 但是我的代码中有一些错误,因此程序会停止并且不会创建经过训练的模型。
我无法弄清楚错误消息想告诉我什么以及我需要在代码中进行哪些更改。因此,我需要您的帮助。我搜索了该错误,但既不了解,也无法用其他帖子的建议想法解决我的错误。
任何人都可以解释我为什么tensorflow要创建图以及该框架如何可能不知道所需的功能吗?我是否只需要安装一个可视化软件包?是否可以忽略此错误?
我不需要任何图形。但是计算机是否需要使用ml算法进行分类和计算?
请原谅我可怜的英语和我对Tensorflow的不了解。
谢谢!
我已经安装了最新的tensorflow版本2.0.0-beta1和最新的keras版本。
此外,我尝试创建一些图形来显示分类过程。不起作用。
我还激活了逐步调试模式,以找出问题所在。 似乎错误出现在我创建,训练和评估神经元网络的评估模型函数内部。
在模型创建过程中发生错误(模型= 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'
答案 0 :(得分:0)
如果您使用的是tf 2.0 beta,请确保所有keras导入均为tensorflow.keras...
,所有keras导入都将采用假定为tensorflow 1.4的标准keras软件包。
即使用:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, ...
答案 1 :(得分:0)
更改导入的模块。希望此方法可以解决您的错误。
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import normalize