如何将参数传递给Scikit-Learn Keras模型函数 [英] How to pass a parameter to Scikit-Learn Keras model function
问题描述
我有以下代码,使用 Keras Scikit-Learn Wrapper ,它可以正常工作:
from keras.models import从keras.layers中导入Sequential
import Dense
from sklearn导入数据集
from keras.wrappers.scikit_learn从sklearn.model_selection导入KerasClassifier
从sklearn.model_selection导入StratifiedKFold
导入cross_val_score
导入numpy作为np
$ b $ def create_model():
#create model
model = Sequential()
model.add(Dense(12,input_dim = 4,init ='uniform ',activation ='relu'))
model.add(Dense(6,init ='uniform',activation ='relu'))
model.add(Dense(1,init ='uniform ',activation ='sigmoid'))
#编译模型
model.compile(loss ='binary_crossentropy',optimizer ='adam',metrics = ['accuracy'])
返回模型
def main():
主
的描述
iris = datasets。 load_iris()
X,y = iris.data,iris.target
NOF_ROW,NOF_COL = X.shape
#使用10倍交叉验证评估
seed = 7
np.random.seed(seed)
model = KerasClassifier(build_fn = create_model,nb_epoch = 150,batch_size = 10,verbose = 0)
kfold = StratifiedKFold n_splits = 10,shuffle = True,random_state = seed)
results = cross_val_score(model,X,y,cv = kfold)
print(results.mean())
#0.666666666667
if __name__ =='__main__':
main()
可以下载 pima-indians-diabetes.data
here 。
现在我想要做的是传递一个值 NOF _COL
转换为 create_model()
函数的参数如下
model = KerasClassifier(build_fn = create_model(input_dim = NOF_COL),nb_epoch = 150,batch_size = 10,verbose = 0)
使用 create_model()
函数如下所示:
<$ p $创建模型
model = Sequential()
model.add(Dense(12,input_dim = input_dim,init = 'uniform',activation ='relu'))
model.add(Dense(6,init ='uniform',activation ='relu'))
model.add(Dense(1,init = 'uniform',activation ='sigmoid'))
#编译模型
model.compile(loss ='binary_crossentropy',optimizer ='adam',metrics = ['accuracy'])
返回模型
但是它失败给出这个错误:
TypeError:__call __()至少需要2个参数(给出1个)
什么是正确的方法?
您可以添加 input_dim
keyarg到 KerasClassifier
构造函数:
model = KerasClassifier(build_fn = create_model,input_dim = 5,nb_epoch = 150,batch_size = 10,verbose = 0)
I have the following code, using Keras Scikit-Learn Wrapper, which work fine:
from keras.models import Sequential
from keras.layers import Dense
from sklearn import datasets
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
import numpy as np
def create_model():
# create model
model = Sequential()
model.add(Dense(12, input_dim=4, init='uniform', activation='relu'))
model.add(Dense(6, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
def main():
"""
Description of main
"""
iris = datasets.load_iris()
X, y = iris.data, iris.target
NOF_ROW, NOF_COL = X.shape
# evaluate using 10-fold cross validation
seed = 7
np.random.seed(seed)
model = KerasClassifier(build_fn=create_model, nb_epoch=150, batch_size=10, verbose=0)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(model, X, y, cv=kfold)
print(results.mean())
# 0.666666666667
if __name__ == '__main__':
main()
The pima-indians-diabetes.data
can be downloaded here.
Now what I want to do is to pass a value NOF_COL
into a parameter of create_model()
function the following way
model = KerasClassifier(build_fn=create_model(input_dim=NOF_COL), nb_epoch=150, batch_size=10, verbose=0)
With the create_model()
function that looks like this:
def create_model(input_dim=None):
# create model
model = Sequential()
model.add(Dense(12, input_dim=input_dim, init='uniform', activation='relu'))
model.add(Dense(6, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
But it fails giving this error:
TypeError: __call__() takes at least 2 arguments (1 given)
What's the right way to do it?
You can add an input_dim
keyarg to the KerasClassifier
constructor:
model = KerasClassifier(build_fn=create_model, input_dim=5, nb_epoch=150, batch_size=10, verbose=0)
这篇关于如何将参数传递给Scikit-Learn Keras模型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!