Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scikit learn cross_val_score抛出ValueError:必须始终传递'Layer.call'的第一个参数_Python_Tensorflow_Machine Learning_Keras_Scikit Learn - Fatal编程技术网

Python Scikit learn cross_val_score抛出ValueError:必须始终传递'Layer.call'的第一个参数

Python Scikit learn cross_val_score抛出ValueError:必须始终传递'Layer.call'的第一个参数,python,tensorflow,machine-learning,keras,scikit-learn,Python,Tensorflow,Machine Learning,Keras,Scikit Learn,我正在做一个深度学习项目,我试着按照一个教程用交叉验证来评估我的模型 from keras.wrappers.scikit_learn import KerasClassifier from sklearn.model_selection import StratifiedKFold from sklearn.model_selection import cross_val_score # Function to create model, required for KerasClassifi

我正在做一个深度学习项目,我试着按照一个教程用交叉验证来评估我的模型

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score

# Function to create model, required for KerasClassifier
def create_model():
    # create model
    model= Sequential()
    model.add(Dense(128, activation='relu',input_shape = (78,1)))
    model.add(Dropout(0.01))
    model.add(Dense(15, activation='softmax'))

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

#fix random seed for reproducibility
seed = 7
np.random.seed(seed)
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)

# create model
model = KerasClassifier(build_fn=create_model(), epochs=30, batch_size=64, verbose=0)

# evaluate using 5-fold cross-validation
results = cross_val_score(model, features, labels, cv=kfold,scoring='accuracy', error_score="raise")
print(results.mean())

我正在看这个教程:

我首先将数据集拆分为功能和标签:

labels = dataset['Label']
features = dataset.loc[:, dataset.columns != 'Label'].astype('float64')
我有以下形状:

features.shape ,labels.shape
((2425727, 78), (2425727,))
我使用RobustScalar来缩放我的数据和

features
array([[ 1.40474359e+02, -1.08800488e-02,  0.00000000e+00, ...,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
       [ 1.40958974e+02, -1.08609909e-02, -2.50000000e-01, ...,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
       [ 1.40961538e+02, -1.08712390e-02, -2.50000000e-01, ...,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
       ...,
       [ 1.48589744e+02, -1.08658453e-02,  0.00000000e+00, ...,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
       [-6.92307692e-02,  1.77654485e-01,  1.00000000e+00, ...,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],
       [-6.92307692e-02,  6.18858398e-03,  5.00000000e-01, ...,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00]])

labels
array([0, 0, 0, ..., 0, 0, 0])
现在,数据已准备好执行交叉验证

from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score

# Function to create model, required for KerasClassifier
def create_model():
    # create model
    model= Sequential()
    model.add(Dense(128, activation='relu',input_shape = (78,1)))
    model.add(Dropout(0.01))
    model.add(Dense(15, activation='softmax'))

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

#fix random seed for reproducibility
seed = 7
np.random.seed(seed)
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)

# create model
model = KerasClassifier(build_fn=create_model(), epochs=30, batch_size=64, verbose=0)

# evaluate using 5-fold cross-validation
results = cross_val_score(model, features, labels, cv=kfold,scoring='accuracy', error_score="raise")
print(results.mean())

执行此操作后,我会抛出此错误:“ValueError:Layer.call的第一个参数必须始终传递。”

我还检查scikit学习文档以检查我是否做错了什么:

我还试图寻找其他可能有此问题的人,例如:

但我没办法解决这个问题。 有人能帮我吗

model = KerasClassifier(build_fn=create_model(), ...) 
请尝试删除函数create_model的大括号,因为该参数需要一个回调函数,该函数将在需要时调用。就这样吧

model = KerasClassifier(build_fn=create_model, ... ) 
请尝试删除函数create_model的大括号,因为该参数需要一个回调函数,该函数将在需要时调用。就这样吧

model = KerasClassifier(build_fn=create_model, ... ) 

哦,我错过了,谢谢。但现在我得到了另一个错误:ValueError:层sequential_9的输入0与层不兼容:输入形状的轴-1预期值为1,但收到了带形状的输入(无,78)。也许我应该编辑这个问题。哦,我错过了,谢谢。但是现在我得到了另一个错误:ValueError:layer sequential_9的输入0与layer不兼容:输入形状的轴-1应该有值1,但收到了带有形状的输入(无,78)。也许我应该编辑这个问题。对于那些在这方面遇到问题的人,我设法通过添加input_dim=78而不是input_shape=(78,1)来修复它。现在它开始工作了。对于那些遇到这个问题的人,我设法通过添加input_dim=78而不是input_shape=(78,1)来修复它。现在它开始工作了。