我创建了一个sklearn管道,该管道使用SelectPercentile(f_classif)对通过KerasClassifier进行的特征选择进行了处理。用于SelectPercentile的百分位数是网格搜索中的超参数。这意味着输入尺寸将在gridsearch期间发生变化,并且我未能成功设置KerasClassifier的input_dim以使其相应地适应此参数。
我不认为有一种方法可以访问在sklearn的GridSearchCV中的KerasClassifier中通过管道传递的缩减数据维度。也许有一种方法可以在管道中的SelectPercentile和KerasClassifier之间共享一个单一的超参数(以便该百分比超参数可以确定input_dim)?我想可能的解决方案是构建一个自定义分类器,该分类器将管道中的两个步骤包装为一个步骤,以便可以共享百分比超参数。
到目前为止,在模型拟合过程中,该错误始终产生“ ValueError:检查输入时出错:预期density_1_input具有形状(112,)但形状为(23,)的数组”的变化。
def create_baseline(input_dim=10, init='normal', activation_1='relu', activation_2='relu', optimizer='SGD'):
# Create model
model = Sequential()
model.add(Dense(50, input_dim=np.shape(X_train)[1], kernel_initializer=init, activation=activation_1))
model.add(Dense(25, kernel_initializer=init, activation=activation_2))
model.add(Dense(1, kernel_initializer=init, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=["accuracy"])
return model
tuned_parameters = dict(
anova__percentile = [20, 40, 60, 80],
NN__optimizer = ['SGD', 'Adam'],
NN__init = ['glorot_normal', 'glorot_uniform'],
NN__activation_1 = ['relu', 'sigmoid'],
NN__activation_2 = ['relu', 'sigmoid'],
NN__batch_size = [32, 64, 128, 256]
)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=2)
for train_indices, test_indices in kfold.split(data, labels):
# Split data
X_train = [data[idx] for idx in train_indices]
y_train = [labels[idx] for idx in train_indices]
X_test = [data[idx] for idx in test_indices]
y_test = [labels[idx] for idx in test_indices]
# Pipe feature selection and classifier together
anova = SelectPercentile(f_classif)
NN = KerasClassifier(build_fn=create_baseline, epochs=1000, verbose=0)
clf = Pipeline([('anova', anova), ('NN', NN)])
# Train model
clf = GridSearchCV(clf, tuned_parameters, scoring='balanced_accuracy', n_jobs=-1, cv=kfold)
clf.fit(X_train, y_train)
# Test model
y_true, y_pred = y_test, clf.predict(X_test)
答案 0 :(得分:1)
我发现的解决方案是在ANOVASelection期间声明转换后的X的全局变量,然后在create_model中定义input_dim时访问该变量。
# Custom class to allow shape of transformed x to be known to classifier
class ANOVASelection(BaseEstimator, TransformerMixin):
def __init__(self, percentile=10):
self.percentile = percentile
self.m = None
self.X_new = None
self.scores_ = None
def fit(self, X, y):
self.m = SelectPercentile(f_classif, self.percentile)
self.m.fit(X,y)
self.scores_ = self.m.scores_
return self
def transform(self, X):
global X_new
self.X_new = self.m.transform(X)
X_new = self.X_new
return self.X_new
# Define neural net architecture
def create_model(init='normal', activation_1='relu', activation_2='relu', optimizer='SGD', decay=0.1):
clear_session()
# Determine nodes in hidden layers (Huang et al., 2003)
m = 1 # number of ouput neurons
N = np.shape(data)[0] # number of samples
hn_1 = int(np.sum(np.sqrt((m+2)*N)+2*np.sqrt(N/(m+2))))
hn_2 = int(m*np.sqrt(N/(m+2)))
# Create layers
model = Sequential()
if optimizer == 'SGD':
model.add(Dense(hn_1, input_dim=np.shape(X_new)[1], kernel_initializer=init,
kernel_regularizer=regularizers.l2(decay/2), activation=activation_1))
model.add(Dense(hn_2, kernel_initializer=init, kernel_regularizer=regularizers.l2(decay/2),
activation=activation_2))
elif optimizer == 'AdamW':
model.add(Dense(hn_1, input_dim=np.shape(X_new)[1], kernel_initializer=init,
kernel_regularizer=regularizers.l2(decay), activation=activation_1))
model.add(Dense(hn_2, kernel_initializer=init, kernel_regularizer=regularizers.l2(decay),
activation=activation_2))
model.add(Dense(1, kernel_initializer=init, activation='sigmoid'))
if optimizer == 'SGD':
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=["accuracy"])
if optimizer == 'AdamW':
model.compile(loss='binary_crossentropy', optimizer=AdamW(), metrics=["accuracy"])
return model
tuned_parameters = dict(
ANOVA__percentile = [20, 40, 60, 80],
NN__optimizer = ['SGD', 'AdamW'],
NN__init = ['glorot_normal', 'glorot_uniform'],
NN__activation_1 = ['relu', 'sigmoid'],
NN__activation_2 = ['relu', 'sigmoid'],
NN__batch_size = [32, 64, 128, 256],
NN__decay = [10.0**i for i in range(-10,-0) if i%2 == 1]
)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=2)
for train_indices, test_indices in kfold.split(data, labels):
# Ensure models from last iteration have been cleared.
clear_session()
# Learning Rate
clr = CyclicLR(mode='triangular', base_lr=0.001, max_lr=0.6, step_size=5)
# Split data
X_train = [data[idx] for idx in train_indices]
y_train = [labels[idx] for idx in train_indices]
X_test = [data[idx] for idx in test_indices]
y_test = [labels[idx] for idx in test_indices]
# Apply mean and variance center based on training fold
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
# Memory handling
cachedir = tempfile.mkdtemp()
mem = Memory(location=cachedir, verbose=0)
f_classif = mem.cache(f_classif)
# Build and train model
ANOVA = ANOVASelection(percentile=5)
NN = KerasClassifier(build_fn=create_model, epochs=1000, verbose=0)
clf = Pipeline([('ANOVA', ANOVA), ('NN', NN)])
clf = GridSearchCV(clf, tuned_parameters, scoring='balanced_accuracy', n_jobs=28, cv=kfold)
clf.fit(X_train, y_train, NN__callbacks=[clr])
# Test model
y_true, y_pred = y_test, clf.predict(X_test)
答案 1 :(得分:1)
对我有用的另一种解决方案是从 KerasClassifier
继承并在调用 input_dim
之前在 fit 函数中设置 set_params
{1}}。这适用于 scikit-learn 0.24.0 和 keras 2.4.3。
这是一个完整的例子:
首先是继承类。这是主要必须添加到正常用法中的内容:
super().fit(X, y)
正常使用,然后使用类 from keras.wrappers.scikit_learn import KerasClassifier
class InputDimPredictingKerasClassifier(KerasClassifier):
def fit(self, X, y):
super().set_params(**{"input_dim": X.shape[1]})
return super().fit(X, y)
构建模型:
InputDimPredictingKerasClassifier