【问题标题】:How can I bind parameters in a scikit-learn pipeline?如何在 scikit-learn 管道中绑定参数?
【发布时间】:2018-07-29 16:42:23
【问题描述】:

我有一个pipeline 对象,我想使用RandomizedSearchCV 优化其超参数,但我需要绑定两个参数,如果一个设置为一个值,另一个自动设置为相同的值价值。

这是我的具体案例:我将一个减少到 nbFeature 维度的 PCA 链接到一个 Keras 分类器,该分类器需要明确其输入昏暗 nbFeature。显然,当两者不匹配时,这将失败。请看下面的玩具示例:

# setup
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.model_selection import RandomizedSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier

# toy data
n = 500
p = 100
X = np.random.normal(size=(n,p))
Y = np.concatenate((np.zeros(int(n/2)),np.ones(int(n/2))))

# toy pipeline
nbFeature = 10 # the guy to bind between the PCA and my Keras model

reducer = PCA(n_components=nbFeature)

def myBasicDense(n_feature):
    return KerasClassifier(build_fn=buildfn_myBasicDense,n_feature=n_feature,verbose=0) 
def buildfn_myBasicDense(n_feature=777):
    model = Sequential()
    model.add(Dense(1,input_dim=n_feature,activation='softmax'))
    model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
    return model  
model = myBasicDense(n_feature=nbFeature) # tried using 'reducer.n_components' but this only uses the value once, instead of binding

pipeStep = [('reducer',reducer),('model',model)]
pipe = Pipeline(pipeStep)

# run RandomizedSearchCV
# this works only when sampled 'reducer__n_components' and 'model__n_feature' are equal
gridDist = {'reducer__n_components': [10, 50],'model__n_feature': [10, 50]}

n_iter_search = 2
optimizedPipe = RandomizedSearchCV(
        refit=True,        
        estimator=pipe,
        param_distributions=gridDist,
        n_iter=n_iter_search,
        scoring='accuracy',
        cv=3,         
        verbose=2,
        random_state=12 # chosen so that is fails on second round...
        )

optimizedPipe.fit(X,Y)

所以这是我的问题:有没有办法指定管道的两个或多个参数必须始终相同,以便我可以只搜索其中一个?

(或者,欢迎任何解决方法,包括更好地使用RandomizedSearchCV)。

非常感谢!

【问题讨论】:

  • 将您的两个步骤组合在一个包装器中,该包装器将参数作为输入并将它们传递给两个步骤。
  • 嗨维维克。谢谢,能详细说明一下吗?

标签: scikit-learn keras


【解决方案1】:

您的问题有两种解决方案:

更新:此方法仅适用于 GridSearchCV,不适用于 RandomizedSearchCV。请使用下面的(2)。

1) 将 gridDist 中的参数组合在一起。

代替

gridDist = {'reducer__n_components': [10, 50],'model__n_feature': [10, 50]}

你应该这样做:

gridDist = [{'reducer__n_components': [10],'model__n_feature': [10]},
            {'reducer__n_components': [50],'model__n_feature': [50]}]

它是做什么的,它制作了 2 个字典。并且字典里面的参数总是一起探索的。因此,您将始终拥有相同的 n_components 和 n_feature 值。请参阅此示例以更好地使用此类参数网格:

2) 按照我在评论中的建议制作一个包装器。像这样的:

def myBasicDense(n_feature):
    return KerasClassifier(build_fn= buildfn_myBasicDense, n_feature=n_feature, verbose=0) 
def buildfn_myBasicDense(n_feature=777):
    model = Sequential()
    model.add(Dense(1,input_dim=n_feature,activation='softmax'))
    model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
    return model

class CustomWrapper(BaseEstimator, ClassifierMixin):

    def __init__(self, n_features=10):
        self.n_features = n_features

        # This n_features is passed to both your parts of the pipeline
        self.pipe = Pipeline([('reducer',PCA(n_components=n_features)),('model', myBasicDense(n_feature=n_features))])

    def fit(self, X, y):

        self.pipe.fit(X, y)
        return self

    def predict(self, X):
        return self.pipe.predict(X)

    def set_params(self, **params):
        super(CustomWrapper, self).set_params(**params)
        self.pipe = Pipeline([('reducer',PCA(n_components=self.n_features)),('model',myBasicDense(n_feature=self.n_features))])
        return self

现在您只需搜索一个超参数 - n_features。所以你的参数网格变成:

gridDist = {'n_features': [10, 50]}

然后按如下方式初始化 RandomSearch:

wrapperModel = CustomWrapper()

optimizedPipe = RandomizedSearchCV(
        refit=True,        
        estimator=wrapperModel,
        param_distributions=gridDist,
        n_iter=n_iter_search,
        scoring='accuracy',
        cv=3,         
        verbose=2,
        random_state=12 # chosen so that is fails on second round...
        )

【讨论】:

  • 嗨 Vivek,我认为这没问题,因为 CV 运行时没有错误,但我自省后意识到 n_features 在 CV 期间实际上并未在估算器中更新。如果我们在CustomWrapper.buildfn_myBasicDense 中添加print(n_features),这是可见的:它仍然是__init__ 的默认值。这有点像 set_params 没有被正确调用,但我不明白为什么,因为我们使用类 BaseEstimatorClassifierMixin 的继承(如 [scikit-learn.org/stable/developers/… 文档中所述))
  • @0-tree 不,它与 set_params() 无关。它是如何 keras build_fn 工作的。我已经编辑了代码来处理它。
  • 现在确实可以用了,谢谢!不是很重要,但你介意解释为什么我们需要这个额外的 super() 调用吗?是不是因为我们在这里使用了 Keras 分类器?
  • @0-tree 不,因为我们覆盖了 set_params() 调用,所以我们需要确保调用来自 BaseEstimator 的原始 set_params()。然后我们通过再次初始化管道来满足 Keras 的需要。
  • @0-tree 对于您建议的编辑,我拒绝了它,因为我们已经在 CustomWrapper 中,所以我认为再次初始化它是不合适的。
猜你喜欢
  • 2015-08-14
  • 1970-01-01
  • 2017-09-14
  • 2021-03-17
  • 1970-01-01
  • 2014-06-04
  • 2018-07-07
  • 2020-09-22
  • 2017-02-25
相关资源
最近更新 更多