首页 > 解决方案 > 如何使用 sklearn 管道缩放 Keras 自动编码器模型的目标值?

问题描述

我正在使用 sklearn 管道构建 Keras 自动编码器模型并使用网格搜索来查找最佳超参数。如果我使用多层感知器模型进行分类,这很好用;但是,在自动编码器中,我需要输出值与输入相同。换句话说,我正在使用StandardScalar管道中的实例来缩放输入值,因此这导致了我的问题:如何使StandardScalar管道内的实例同时处理输入数据和目标数据,以便它们最终是一样的吗?

我提供了一个代码片段作为示例。

from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV, KFold
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop, Adam
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor

X, y = make_classification (n_features = 50, n_redundant = 0, random_state = 0,
                            scale = 100, n_clusters_per_class = 1)

# Define wrapper
def create_model (learn_rate = 0.01, input_shape, metrics = ['mse']):
  model = Sequential ()
  model.add (Dense (units = 64, activation = 'relu',
                   input_shape = (input_shape, )))
  model.add (Dense (32, activation = 'relu'))
  model.add (Dense (8,  activation = 'relu'))
  model.add (Dense (32, activation = 'relu'))
  model.add (Dense (input_shape, activation = None))
  model.compile (loss = 'mean_squared_error',
                 optimizer = Adam (lr = learn_rate),
                 metrics = metrics)
  return model

# Create scaler
my_scaler = StandardScaler ()
steps = list ()
steps.append (('scaler', my_scaler))
standard_scaler_transformer = Pipeline (steps)

# Create classifier
clf = KerasRegressor (build_fn = create_model, verbose = 2)

# Assemble pipeline
# How to scale input and output??
clf = Pipeline (steps = [('scaler', my_scaler),
                         ('classifier', clf)],
                verbose = True)

# Run grid search
param_grid = {'classifier__input_shape' : [X.shape [1]],
              'classifier__batch_size' : [50],
              'classifier__learn_rate' : [0.001],
              'classifier__epochs' : [5, 10]}
cv = KFold (n_splits = 5, shuffle = False)
grid = GridSearchCV (estimator = clf, param_grid = param_grid,
                     scoring = 'neg_mean_squared_error', verbose = 1, cv = cv)
grid_result = grid.fit (X, X)

print ('Best: %f using %s' % (grid_result.best_score_, grid_result.best_params_))

标签: pythontensorflowmachine-learningkerasscikit-learn

解决方案


您可以通过提供函数(即使用参数)或转换器(即参数)TransformedTargetRegressor来对目标值(即)应用任意转换。yfunctransformer

在这种情况下(即拟合自动编码器模型),由于您也想StandardScalar在目标值上应用相同的实例,因此可以使用transformer参数。它可以通过以下方式之一完成:

  • 您可以将其用作管道步骤之一,包装回归器:

    scaler = StandardScaler()
    regressor = KerasRegressor(...)
    
    pipe = Pipeline(steps=[
        ('scaler', scaler),
        ('ttregressor', TransformedTargetRegressor(regressor, transformer=scaler))
    ])
    
    # Use `__regressor` to access the regressor hyperparameters
    param_grid = {'ttregressor__regressor__hyperparam_name' : ...}
    
    gridcv = GridSearchCV(estimator=pipe, param_grid=param_grid, ...)
    gridcv.fit(X, X)
    
  • 或者,您可以像这样将其包裹起来GridSearchCV

     ttgridcv = TransformedTargetRegressor(GridSearchCV(...), transformer=scalar)
     ttgridcv.fit(X, X)
    
     # Use `regressor_` attribute to access the fitted regressor (i.e. `GridSearchCV` instance) 
     print(ttgridcv.regressor_.best_score_, ttgridcv.regressor_.best_params_))
    

推荐阅读