具有预训练的Sklearn GridSearch

时间:2019-01-07 15:34:46

标签: python machine-learning scikit-learn keras grid-search

我正在使用Sklearn进行的参数优化来制作Pipeline GridSearchCV。管道必须为实施预训练的几个不同实体获得最佳模型,然后进行微调方法:预训练实体在一起,并对每个元素进行微调,并为每个实体返回一个模型。这些是管道的约束:

  • 预训练和微调必须位于相同的管道中,因为两个模型在每个GridSearchCV的折叠中必须具有相同的数据。
  • 训练前模型必须将其权重传递给微调模型。

我已经实现:

  • 一个Sklearn变压器,可将所有实体输入并容纳自身的数据框。
  • 一种Sklearn回归器,用于将每个实体的数据帧拆分为一个数据帧,并为每个实体拟合Keras模型。

我所缺少的是如何将预训练变压器获得的权重从预训练变压器传递到微调变压器(考虑到每个GridSearchCV折叠具有不同的权重)

代码如下:

import pandas as pd
import numpy as np
import random
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.base import BaseEstimator, RegressorMixin, TransformerMixin
from sklearn.metrics import mean_squared_error
from keras.models import Model
from keras.layers import Dense, Input
import copy

class MyRegressor(BaseEstimator, TransformerMixin):
    def __init__(self, neurons, featInput, featOutput):
        self.neurons = neurons
        self.preTrain = None
        self.featInput = featInput
        self.featOutput = featOutput

    def fit(self, X, y=None):
        X_train = X[self.featInput]
        y_train = X[self.featOutput]

        inputLayer = Input(shape=(len(self.featInput), ), name='INPUT')
        hidden = Dense(self.neurons, name='HIDDEN')(inputLayer)
        outputLayer = Dense(len(self.featOutput), name='OUTPUT')(hidden)

        self.model = Model(inputLayer, outputLayer)
        self.model.compile(loss='mse', optimizer='rmsprop')

        if self.preTrain is not None:
            self.model.loadWeights(self.preTrain)

        self.model.fit(X_train, y_train)

        return self

    def predict(self, X):
        return self.model.predict(X[self.featInput])

    def transform(self, X):
        return X

    def score(self, X, y=None, sample_weight=None):
        y_true = X[self.featOutput]
        y_pred = self.predict(X)
        return mean_squared_error(y_true, y_pred)


class LoopTransformer(BaseEstimator, TransformerMixin):
    def __init__(self, columns, component):
        self.columns = columns
        self.component = component
        self.components = []

    def fit(self, X, y=None):
        for index, idx in X[self.columns].drop_duplicates().iterrows():
            entityDf = X[(X[self.columns] == idx).sum(axis=1) == len(self.columns)].copy()
            self.components.append({'id': idx, 'component': copy.deepcopy(self.component)})
            self.components[-1]['component'].fit(entityDf, y)
        return self

    def predict(self, X):
        results = []
        for comp in self.components:
            entityDf = X[(X[self.columns] == comp['id']).sum(axis=1) == len(self.columns)].copy()
            res = comp['component'].predict(entityDf)
            results.append(res)
        dfRes = pd.concat(results)
        return dfRes

    def score(self, X, y=None, sample_weight=None):
        results = []
        for comp in self.components:
            entityDf = X[(X[self.columns] == comp['id']).sum(axis=1) == len(self.columns)].copy()
            if len(entityDf) > 0:
                results.append(comp['component'].score(entityDf))
        return np.average(results)


#create the input dataframe: 3 entities
dataFrame = pd.DataFrame([], columns=['entityId', 'input', 'output'])
for entity in range(3):
    x = np.arange(random.randint(10, 20))
    y = x * (entity + 1)
    tempDf = pd.DataFrame(np.array([x, y]).T, columns=['input', 'output'])
    tempDf['entityId'] = entity
    dataFrame = pd.concat([dataFrame, tempDf], sort=False)
dataFrame = dataFrame.reset_index(drop=True)

#create the pipeline
neurons = [5, 10]
myPipe = Pipeline([('preTrain',
                    MyRegressor(neurons=neurons[0], featInput=['input'], featOutput=['output'])),
                   ('fineTuning',
                    LoopTransformer(['entityId'],
                                    MyRegressor(
                                        neurons=neurons[0],
                                        featInput=['input'],
                                        featOutput=['output'])))])

#pre-train and fine-tuning has to have always the same number of neurons
params = [{
    'preTrain__neurons': [neurons[0]],
    'fineTuning__component__neurons': [neurons[0]]
}, {
    'preTrain__neurons': [neurons[1]],
    'fineTuning__component__neurons': [neurons[1]]
}]

gs = GridSearchCV(myPipe, params, verbose=1, cv=3)

gs.fit(dataFrame, dataFrame)
score = gs.score(dataFrame, dataFrame)
print(score)

1 个答案:

答案 0 :(得分:-1)

我很喜欢sklearn.Pipeline,因为它不支持此操作。但是只要您不克隆管道(例如,如果您使用GridSearchCV就会发生这种情况),就可以使用下面的代码来破解自己的方法,该代码提供了传递到下一步。您可以在管道中应用相同的原理:

from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
from sklearn.base import BaseEstimator, TransformerMixin


class MyTransformer(BaseEstimator, TransformerMixin):
    def __init__(self, scaler):
        self.scaler = scaler

    def fit(self, X, y=None):
        print("got the means: %s" % self.scaler.mean_)
        return self

    def transform(self, X):
        return X


X, y = load_iris(return_X_y=True)
scaler = StandardScaler()
pipeline = make_pipeline(scaler,
                         MyTransformer(scaler),
                         LogisticRegression(solver='lbfgs',
                                            multi_class='auto'))
pipeline = pipeline.fit(X, y)

X = X - 1
pipeline = pipeline.fit(X, y)

将为您提供预期的输出:

got the means: [5.84333333 3.05733333 3.758      1.19933333]
got the means: [4.84333333 2.05733333 2.758      0.19933333]