我正在对神经网络进行超参数调整。我尝试了很多手动调整,但对我一直使用的数据集的预测能力仍然很差。我一直选择使用 GridSearch 来测试我的模型的所有可能的参数组合。
类似的事情是否可能(参见下面的代码)或者是否有更智能/更好的参数调整方法?代码能够运行;当然这需要一些时间,但确实有效。
我没有特别的错误,我只是在寻找一些好的见解来知道这是否合适。
数据框示例:
sequence target expression
-AQSVPWGISRVQAPAAH-NRGLRGSGVKVAVLDTGI-STHPDLNI... 0.00 50.0
-AQQVPYGVSQIKAPALH-EQGYTGQNVKVAVIDTGIDSSHPDLKV... 0.46 42.0
-AQSVPWGIRRVQAPAAH-NRGLTGSGVKVAVLDTGI-STHPDLNI... 0.34 46.0
-AQTVPWGISRVQAPAAH-NRGLTGAGVKVSVLDTGI-STHPDLNI... 0.95 45.0
-AQSVPYGVSQIKAPALH-SQGYTGSNVKVAVIDTGIDSSHPDLKV... 0.60 50.0
数据形状:3000行和3840个特征
请注意,特征数很高,因为所有这些序列都是一次热编码的。
代码:
'Hyperparameter Tuning for Neurons, Batch_Size, Epochs and Learning Rate'
def build_regressor(n_neurons=1, learning_rate=0.01):
regressor = Sequential()
regressor.add(Dense(n_neurons, activation = 'relu', input_shape = (x_train.shape[1],)))
#regressor.add(Dense(n_neurons, activation = 'relu'))
regressor.add(Dense(units=1))
optimizer = Adam(lr = learning_rate)
regressor.compile(optimizer= optimizer, loss='mean_squared_error', metrics=['mae','mse'])
return regressor
#Create Model
model = KerasRegressor(build_fn=build_regressor, verbose=0)
# define the grid search parameters
batch_size = [10, 25, 50, 100, 150]
epochs = [5, 10, 25, 50]
n_neurons = [1, 32, 64, 128, 256, 512]
learning_rate = [0.001, 0.01, 0.1, 0.2, 0.3]
param_grid = dict(batch_size=batch_size, epochs=epochs, n_neurons=n_neurons, learning_rate = learning_rate)
#implement grid_search
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3, scoring = 'r2')
grid_result = grid.fit(x_train, y_train)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
12345678_0001
当年话下
相关分类