'KerasClassifier' 对象没有属性 'summary' - 尝试从 KerasClassifier 构建的 lstm 模型中获取摘要

'KerasClassifier' object has no attribute 'summary' - try to get summary from KerasClassifier built lstm model

好像是 KerasClassifier 对可定制模型做了一些包装,但我不知道如何把它弄出来...

我想将我的 lstm 模型从勉强创建到 keras 包装器,例如KerasClassifier:

model1 = Sequential()
model1.add(LSTM(units=60, activation='relu', input_shape=(60, 1),
        return_sequences=True, recurrent_dropout=0.1))
model1.add(LSTM(units=30))
model1.add(Dense(units=1, activation='sigmoid'))
model1.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

def create_model():
    model = Sequential()
    model.add(LSTM(units=60, activation='relu', input_shape=(60, 1),
        return_sequences=True, recurrent_dropout=0.1))
    model.add(LSTM(units=30))
    model.add(Dense(units=1, activation='sigmoid'))
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    return model

modelk = KerasClassifier(build_fn=create_model,
                         epochs=10,
                         batch_size=30,
                         verbose=0)

如果我使用 model1 执行 model1.summary() 返回第一个方法,我得到类似:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 60, 60)            14880     
                                                                 
 lstm_1 (LSTM)               (None, 30)                10920     
                                                                 
 dense (Dense)               (None, 1)                 31        
                                                                 
=================================================================
Total params: 25,831
Trainable params: 25,831
Non-trainable params: 0

但是如果我使用从第二种方法返回的 modelk 'modelk.summary()',我会得到如下错误:

'KerasClassifier' object has no attribute 'summary'

尝试 modelk.build_fn().summary():

from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.wrappers.scikit_learn import KerasClassifier
 
def create_model():
    model = Sequential()
    model.add(LSTM(units=60, activation='relu', input_shape=(60, 1),
        return_sequences=True, recurrent_dropout=0.1))
    model.add(LSTM(units=30))
    model.add(Dense(units=1, activation='sigmoid'))
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    return model

modelk = KerasClassifier(build_fn=create_model,
                         epochs=10,
                         batch_size=30,
                         verbose=0)
print(modelk.build_fn().summary())
Model: "sequential_2"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm_4 (LSTM)               (None, 60, 60)            14880     
                                                                 
 lstm_5 (LSTM)               (None, 30)                10920     
                                                                 
 dense_2 (Dense)             (None, 1)                 31        
                                                                 
=================================================================
Total params: 25,831
Trainable params: 25,831
Non-trainable params: 0
_________________________________________________________________
None

您还可以在 create_model 中使用 model.summary 并且在内部调用 model.fit 时将打印摘要:

from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
import numpy as np

def create_model(optimizer='rmsprop'):
    model = Sequential()
    model.add(LSTM(units=60, activation='relu', input_shape=(60, 1),
        return_sequences=True, recurrent_dropout=0.1))
    model.add(LSTM(units=30))
    model.add(Dense(units=1, activation='sigmoid'))
    model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
    print(model.summary())
    return model

modelk = KerasClassifier(build_fn=create_model,
                         epochs=10,
                         batch_size=25,
                         verbose=0)

optimizers = ['rmsprop', 'adam']
param_grid = dict(optimizer=optimizers)
grid = GridSearchCV(estimator=modelk, param_grid=param_grid)

X = np.random.random((50, 60, 1))
Y = np.random.random((50,))
grid_result = grid.fit(X, Y)