NameError:名称“统一”未定义

时间:2019-12-09 05:08:16

标签: python pandas numpy keras neural-network

这似乎是一个非常简单的问题,但我找不到答案。

我试图用代码构建机器学习模型:

# MLP for Pima Indians Dataset with 10-fold cross validation 
from keras.models import Sequential 
from keras.layers import Dense 
from sklearn.model_selection import StratifiedKFold
import random 
import numpy

# fix random seed for reproducibility 
seed = 7 
np.random.seed(seed) 
# load pima indians dataset 
dataset = np.loadtxt("/content/drive/My Drive/pima-indians-diabetes.csv", delimiter=",")


# split into input (X) and output (Y) variables 
X = dataset[:,0:8] 
Y = dataset[:,8] 
# define 10-fold cross validation test harness 
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed) 
cvscores = [] 
for train, test in kfold.split(X, Y):

# create model 
    model = Sequential() 
    model.add(Dense(12, input_dim=8, init= uniform , activation= relu )) 
    model.add(Dense(8, init= uniform , activation= relu )) 
    model.add(Dense(1, init= uniform , activation= sigmoid )) 
# Compile model 
    model.compile(loss= binary_crossentropy , optimizer= adam , metrics=[ accuracy ]) 
# Fit the model 
    model.fit(X[train], Y[train], nb_epoch=150, batch_size=10, verbose=0) 
# evaluate the model 
    scores = model.evaluate(X[test], Y[test], verbose=0) 
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) 
    cvscores.append(scores[1] * 100)

print("%.2f%% (+/- %.2f%%)" % (numpy.mean(cvscores), numpy.std(cvscores)))

发生错误的地方

  

NameError:名称“统一”未定义

谢谢

2 个答案:

答案 0 :(得分:1)

你可能。需要

model.add(Dense(8, init= 'uniform' , activation= relu )) 

及其类似的后续行。

答案 1 :(得分:0)

出现的错误是建议的,您在下面使用了变量名而不是字符串:

model.add(Dense(12, input_dim=8, init= uniform , activation= relu )) 
model.add(Dense(8, init= uniform , activation= relu )) 
model.add(Dense(1, init= uniform , activation= sigmoid )) 

使用init='uniform'来解决错误。

同样,您的激活应该是字符串而不是变量,因此完整的解决方法是:

model.add(Dense(12, input_dim=8, init='uniform', activation='relu')) 
model.add(Dense(8, init='uniform', activation='relu')) 
model.add(Dense(1, init='uniform', activation='sigmoid'))