gpt4 book ai didi

python - 如何创建包含特征选择和 KerasClassifier 的 sklearn 管道? GridSearchCV 期间 input_dim 发生变化的问题

转载 作者:行者123 更新时间:2023-12-04 15:35:56 25 4
gpt4 key购买 nike

我创建了一个 sklearn 管道,该管道使用 SelectPercentile(f_classif) 进行特征选择并通过管道传输到 KerasClassifier。用于 SelectPercentile 的百分位数是网格搜索中的超参数。这意味着在 gridsearch 期间输入维度会有所不同,我一直没有成功设置 KerasClassifier 的 input_dim 以相应地适应这个参数。

我不认为有一种方法可以访问在 sklearn 的 GridSearchCV 中的 KerasClassifier 中传输的减少的数据维度。也许有一种方法可以在管道中的 SelectPercentile 和 KerasClassifier 之间共享一个超参数(以便百分位超参数可以确定 input_dim)?我想一个可能的解决方案是构建一个自定义分类器,将管道中的两个步骤包装成一个步骤,以便可以共享百分位超参数。

到目前为止,错误始终产生“ValueError:检查输入时出错:预期 dense_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)

最佳答案

我找到的解决方案是在 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)

关于python - 如何创建包含特征选择和 KerasClassifier 的 sklearn 管道? GridSearchCV 期间 input_dim 发生变化的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59755378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com