【问题标题】:Hybrid Deep Learning Applied to artificial data混合深度学习应用于人工数据
【发布时间】:2021-07-10 17:19:41
【问题描述】:

晚上好,假设我们有使用以下代码生成的人工数据

import numpy as np
from sklearn.datasets import make_classification
X ,y =make_classification(n_samples=5000,n_features=10,n_informative=3,random_state=1)

我已阅读以下有关混合深度学习的文章:Hybrid Deep learning

让我们假设我想使用经典的 MLP(多层感知器)和一些经典的 ML 算法,例如 catboost,根据文章,我想要以下内容:正如您从代码中看到的那样,信息丰富的是三个特征(所有剩余的 7 个特征是冗余),因此我想以某种方式使用 MLP 提取重要特征,然后应用 catboost,这是在 keras 中为 MLP 模型做准备

import keras
from keras.models import  Sequential
from keras.layers import Dense
model =Sequential()
model.add(Dense(units=30,input_dim=10,activation='relu'))
model.add(Dense(units=15,activation='relu'))
model.add(Dense(units=1,activation='sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

让我们导入 catboost 库

import catboost
from catboost import CatBoostClassifier

请用一个非常简单的代码帮助我如何将这些东西组合在一起(我的意思是使用 MLP 作为特征提取并使用 catboostclassifier 进行分类)。非常感谢

【问题讨论】:

  • 你只需要在中间切割你训练好的网络,提取特征,然后在它们上安装 catboost
  • 是的,但是怎么做?这是实际的问题

标签: python tensorflow machine-learning keras deep-learning


【解决方案1】:

您只需将训练有素的网络从中间剪掉,只要您喜欢,但要注意您的网络返回 2D 数据。只有这样才能拟合表格模型。

在下面的示例中,我们拟合了一个密集的 NN,然后将来自 sklearn 的 RandomForest 应用于隐藏的提取特征。您可以修改网络或更改最终的表格模型

import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from tensorflow.keras.layers import *
from tensorflow.keras.models import *

# create dummy data
X,y = make_classification(n_samples=5000, n_features=10, n_informative=3, random_state=1)

# fit NN
model = Sequential()
model.add(Dense(units=30,input_dim=10,activation='relu'))
model.add(Dense(units=15,activation='relu'))
model.add(Dense(units=1,activation='sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(X,y, epochs=10)

# extract feature from NN
extraction_model = Model(model.input, model.layers[1].output)
# in model.layers you can find all the list of available layers' output
# we select the output of our second Dense layer
# the second Dense return 2D data
new_X = extraction_model.predict(X) # (n_sample, hidden_dim)

# fit a tabular model
rf = RandomForestClassifier()
rf.fit(new_X, y)
print(rf.score(new_X, y))

【讨论】:

    猜你喜欢
    • 2020-07-25
    • 2023-01-03
    • 2014-02-24
    • 2015-02-01
    • 1970-01-01
    • 2019-09-23
    • 2017-10-20
    • 1970-01-01
    • 2018-11-12
    相关资源
    最近更新 更多