簡體   English   中英

Keras:如何將 CNN 模型與決策樹連接起來

[英]Keras: How to connect a CNN model with a decision tree

我想訓練一個模型來從物理信號中預測一個人的情緒。 我有一個物理信號並將其用作輸入功能;

心電圖(心電圖)

我想使用 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變量時,結果是這樣的;

輸出:Tensor("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()方法? 提前致謝。

要提取可傳遞給另一算法的特征向量,需要在softmax層之前有一個完全連接的層。 這樣的事情將在您的softmax層之前添加一個128維的層:

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(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation = 'softmax'))

如果然后運行model.summary() ,則可以看到層的名稱:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_9 (Conv1D)            (None, 17941, 15)         915       
_________________________________________________________________
max_pooling1d_9 (MaxPooling1 (None, 8970, 15)          0         
_________________________________________________________________
dropout_10 (Dropout)         (None, 8970, 15)          0         
_________________________________________________________________
batch_normalization_9 (Batch (None, 8970, 15)          60        
_________________________________________________________________
conv1d_10 (Conv1D)           (None, 8911, 30)          27030     
_________________________________________________________________
max_pooling1d_10 (MaxPooling (None, 2227, 30)          0         
_________________________________________________________________
dropout_11 (Dropout)         (None, 2227, 30)          0         
_________________________________________________________________
batch_normalization_10 (Batc (None, 2227, 30)          120       
_________________________________________________________________
flatten_6 (Flatten)          (None, 66810)             0         
_________________________________________________________________
dense_7 (Dense)              (None, 128)               8551808   
_________________________________________________________________
dropout_12 (Dropout)         (None, 128)               0         
_________________________________________________________________
dense_8 (Dense)              (None, 3)                 387       
=================================================================
Total params: 8,580,320
Trainable params: 8,580,230
Non-trainable params: 90
_________________________________________________________________

訓練好網絡后,您可以創建一個新模型,其中輸出層變為'dense_7',它將生成128維特征向量:

feature_vectors_model = Model(model.input, model.get_layer('dense_7').output)
dtc_features = feature_vectors_model.predict(your_X_data)  # fit your decision tree on this data

feature_vectors_model = Model(model.input, model.get_layer('dense_7').output) dtc_features = feature_vectors_model.predict(your_X_data) # 在這個數據上擬合你的決策樹

我用了你的這個代碼。 我得到模型未定義。 如何解決?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM