Keras:如何将 CNN 模型与决策树连接起来

数据挖掘 深度学习 分类 喀拉斯 scikit-学习 决策树
2021-10-10 06:19:17

我想训练一个模型来从物理信号中预测一个人的情绪。我有一个物理信号并将其用作输入功能;

ecg(心电图)

我想使用 CNN 架构从数据中提取特征,然后使用这些提取的特征来提供经典的“决策树分类器”下面,你可以看到我的 CNN 方法没有决策树;

model = Sequential()
model.add(Conv1D(15,60,padding='valid', activation='relu',input_shape=(18000,1), strides = 1,  kernel_regularizer=regularizers.l1_l2(l1=0.1, l2=0.1)))
model.add(MaxPooling1D(2,data_format='channels_last'))
model.add(Dropout(0.6))
model.add(BatchNormalization())
model.add(Conv1D(30, 60, padding='valid', activation='relu',kernel_regularizer = regularizers.l1_l2(l1=0.1, l2=0.1), strides=1))
model.add(MaxPooling1D(4,data_format='channels_last'))
model.add(Dropout(0.6))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(3, activation = 'softmax'))

我想编辑此代码,以便在输出层中将有工作决策树而不是model.add(Dense(3, activation = 'softmax')). 我试图像这样保存最后一个卷积层的输出;

output = model.layers[-6].output

当我打印出output变量时,结果是这样的;

输出:张量("conv1d_56/Relu:0", shape=(?, 8971, 30), dtype=float32)

我猜,该output变量包含提取的特征。现在,我怎样才能用存储在变量中的这些数据来提供我的决策树分类器模型?output这是来自 scikit learn 的决策树;

from sklearn.tree import DecisionTreeClassifier

dtc = DecisionTreeClassifier(criterion = 'entropy')
dtc.fit()

我应该如何喂养该fit()方法?提前致谢。

1个回答

您可以将特征提取到 keras 中的中间模型。它可能看起来像这样:

# Build a model for only the feature extraction layers
feature_extractor = Sequential()
feature_extractor.add(Conv1D(15,60,padding='valid', activation='relu',input_shape=(18000,1), strides = 1,  kernel_regularizer=regularizers.l1_l2(l1=0.1, l2=0.1)))
feature_extractor.add(MaxPooling1D(2,data_format='channels_last'))
feature_extractor.add(Dropout(0.6))
feature_extractor.add(BatchNormalization())
feature_extractor.add(Conv1D(30, 60, padding='valid', activation='relu',kernel_regularizer = regularizers.l1_l2(l1=0.1, l2=0.1), strides=1))

# Keep adding new layers for prediciton outside of feature extraction model
x = feature_extractor.output
x = MaxPooling1D(4,data_format='channels_last')(x)
x = Dropout(0.6)(x)
x = BatchNormalization()(x)
x = Flatten()(x)
prediction_layer = Dense(3, activation = 'softmax')(x)

# Make a new model combining both
cnn_model=Model(inputs=feature_extractor.input, outputs=prediction_layer)
cnn_model.compile(optimizer=opt,loss=loss)

然后你使用 full 进行训练,cnn_model但你只在你的特征提取模型上调用 predict feature_extractor.predict(X)然后您可以使用预测的输出来训练您的决策树,如下所示:

# Train full network, both feature extractor and softmax part
cnn_model.fit(X, y_one_hot) # y needs to be one hot for keras

# Predict only the output of the feature extraction model
X_ext = feature_extractor.predict(X)

dtc = DecisionTreeClassifier(criterion = 'entropy')

# Train the decision tree on the extracted features
dtc.fit(X_ext, y) # y should be one-dimensional for sklearn