【问题标题】:keras: returning model.summary() vs scikit learn wrapperkeras:返回 model.summary() vs scikit learn wrapper
【发布时间】:2018-06-24 18:10:24
【问题描述】:

在使用 keras 时,我了解到使用包装器会对 keras 和 scikit learn api 请求产生不利影响。我对两者兼有的解决方案感兴趣。

变体 1:scikit 包装器

from keras.wrappers.scikit_learn import KerasClassifier

    def model():
        model = Sequential()
        model.add(Dense(10, input_dim=4, activation='relu'))
        model.add(Dense(3, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        return model

estimator = KerasClassifier(build_fn=model, epochs=100, batch_size=5)
model.fit(X, y)

-> 这让我可以打印诸如 accuracy_score() 或 classification_report() 之类的 scikit 命令。但是,model.summary() 不起作用:

AttributeError: 'KerasClassifier' 对象没有属性 'summary'

变体 2:无包装器

model = Sequential()
model.add(Dense(10, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=100, batch_size=5)

-> 这让我可以打印 model.summary() 但不能打印 scikit 命令。

ValueError: Mix type of y not allowed, got types {'multiclass', '多标签指标'}

有没有一种方法可以同时使用?

【问题讨论】:

    标签: python scikit-learn keras wrapper summary


    【解决方案1】:

    KerasClassifier 只是keras 中实际Model 的包装,因此可以将keras api 的实际方法路由到scikit 中使用的方法,因此可以与scikit 实用程序结合使用。但在内部它只是使用可以通过estimator.model访问的模型。

    上面的例子:

    from keras.models import Sequential
    from keras.layers import Dense
    from keras.wrappers.scikit_learn import KerasClassifier
    from sklearn.datasets import make_classification
    def model():
        model = Sequential()
        model.add(Dense(10, input_dim=20, activation='relu'))
        model.add(Dense(2, activation='softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        return model
    
    estimator = KerasClassifier(build_fn=model, epochs=100, batch_size=5)
    X, y = make_classification()
    estimator.fit(X, y)
    
    # This is what you need
    estimator.model.summary()
    

    这个的输出是:

    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_9 (Dense)              (None, 10)                210       
    _________________________________________________________________
    dense_10 (Dense)             (None, 2)                 22        
    =================================================================
    Total params: 232
    Trainable params: 232
    Non-trainable params: 0
    _________________________________________________________________
    

    【讨论】:

      【解决方案2】:

      总结的函数在这个库里:from keras. models import Model 你可以看到这个:

      【讨论】:

        猜你喜欢
        • 2019-03-14
        • 2016-08-09
        • 2018-01-29
        • 2014-04-20
        • 2014-04-14
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多