tensorflow 如何应用cross_瓦尔_score交叉验证我们自己的模型

xxb16uws  于 2023-05-23  发布在  其他
关注(0)|答案(3)|浏览(117)

通常,我们通过以下方式将cross_val_score应用于Sklearn模型。

scores = cross_val_score(clf, X, y, cv=5, scoring='f1_macro')

现在我有了自己的模型,我希望执行交叉验证。我该怎么做?

tf.keras.backend.clear_session()

model = tf.keras.models.Sequential()
model.add(Masking(mask_value=0.0, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Bidirectional(LSTM(128, dropout=dropout, recurrent_dropout=Rdropout, return_sequences=True)))
# model.add(Bidirectional(LSTM(64, dropout=dropout, recurrent_dropout=Rdropout, return_sequences=True)))
# model.add(Bidirectional(LSTM(128, dropout=dropout, recurrent_dropout=Rdropout, return_sequences=True)))
model.add(Bidirectional(LSTM(32, dropout=dropout, recurrent_dropout=Rdropout)))
# model.add(Dense(6, activation='relu'))
# model.add(Dense(4, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

adamopt = tf.keras.optimizers.Adam(lr=0.003, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
RMSopt = tf.keras.optimizers.RMSprop(lr=0.0007,rho=0.9, epsilon=1e-6)

model.compile(loss='binary_crossentropy',
              optimizer=RMSopt,
              metrics=['accuracy'])
print(cross_val_score(model, X_train, y_train, cv=2,scoring='accuracy'))

TypeError: Cannot clone object '<tensorflow.python.keras.engine.sequential.Sequential object at 0x7f86481170f0>' (type <class 'tensorflow.python.keras.engine.sequential.Sequential'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

我认为cross_val_scoreSklearn型号的专属?

7eumitmz

7eumitmz1#

我们不能直接将Keras模型集成到sklearn管道中。因此,如果您正在寻找使用cross_val_score对Keras模型进行评估,则需要使用 Package 器模块tf.keras.wrappers.scikit_learn来使用Keras模型的sklearn API。例如

from tf.keras.wrappers.scikit_learn import KerasClassifier

def LSTM_Network(neurons=100):

    model = tf.keras.models.Sequential()
    .
    .
    model.compile(loss='binary_crossentropy',
          optimizer=RMSopt,
          metrics=['accuracy'])    
    return model

lstm_clf = KerasClassifier(build_fn=LSTM_Network, epochs=6, batch_size=64, verbose=0)

model_selection.cross_val_score(lstm_clf, X_train, Y_test, cv=10, scoring='accuracy')
rt4zxlrg

rt4zxlrg2#

cross_val_score确实是Sklearn模型或实现相同所需功能的模型所独有的,而Keras模型则不是这样。
Keras没有允许您交叉验证模型的预构建函数,您需要编写交叉验证算法。
首先,你应该决定你想要有多少折叠,然后你可以使用sklearn中的KFold类来将你的数据集划分成多少折叠。(注意,KFold.split返回数据点的索引,而不是实际的数据点)
然后,您应该为每个分割训练一个新模型,并计算您想要的指标。您可以按照this教程了解更多信息。

kpbwa7wx

kpbwa7wx3#

试试这个:

## How to use cross_val_score for Cross Validation in Keras

def Learn_By_Example_320(): 

    print()
    print(format('How to use cross_val_score for Cross Validation in Keras','*^82'))    

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from keras.wrappers.scikit_learn import KerasClassifier
    from keras.initializers import VarianceScaling
    from keras.regularizers import l2
    from keras.models import Sequential
    from keras.layers import Dense
    from sklearn import datasets
    from sklearn.model_selection import cross_val_score

    # simulated data
    dataset = datasets.make_classification(n_samples=10000, n_features=20, n_informative=5, 
                n_redundant=2, n_repeated=0, n_classes=2, n_clusters_per_class=2, 
                weights=None, flip_y=0.01, class_sep=1.0, hypercube=True, shift=0.0, 
                scale=1.0, shuffle=True, random_state=None)

    X = dataset[0];  y = dataset[1]

    print(X.shape);  print(y.shape)

    # Define a Deep Learning Model
    def create_network(optimizer='RMSprop'):
        model = Sequential()
        model.add(Dense(units=36, input_shape=(X.shape[1],), 
                        kernel_regularizer=l2(0.001),           # weight regularizer
                        kernel_initializer=VarianceScaling(),   # initializer
                        activation='relu'))
        model.add(Dense(units=28, 
                        kernel_regularizer=l2(0.01),            # weight regularizer
                        kernel_initializer=VarianceScaling(),   # initializer                   
                        activation='relu'))
        model.add(Dense(units=1, activation='sigmoid'))
    
        # Compile the Model
        model.compile(loss='binary_crossentropy', optimizer = optimizer, 
                      metrics=['acc','mae'])    
        return model

    # Wrap Keras model so it can be used by scikit-learn
    neural_network = KerasClassifier(build_fn=create_network, epochs=5, batch_size=10,
                                     verbose=0)

    # evaluate using 10-fold cross validation    
    results = cross_val_score(neural_network, X, y, cv=10, scoring='accuracy')
    print(); print(results)
    print(); print("Accucary: ", results.mean()*100)
    print("Standard Deviation: ", results.std())

Learn_By_Example_320()

相关问题