[英]How can we tune hyperparameters of neural network when the output variable is continuous?
I've been looking into resources for tuning hyperparameters of ANN models using TensorFlow in Python and by far, every solution/discussion I've come across deals with classification problems with categorical output dataset and use simple logistic regression models or GridSearch through sklearn (so far, those that deal with binary output only).我一直在寻找在 Python 中使用 TensorFlow 调整 ANN 模型超参数的资源,到目前为止,我遇到的每个解决方案/讨论都涉及分类输出数据集的分类问题,并通过 sklearn 使用简单的逻辑回归模型或 GridSearch(所以远,那些只处理二进制输出)。
My problem, however, has a continuous output and I was trying to implement GridSearch, but I ran into an error:然而,我的问题有一个连续的输出,我试图实现 GridSearch,但我遇到了一个错误:
"ValueError: continuous is not supported" “ValueError:不支持连续”
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
def build_classifier(optimizer):
classifier = Sequential()
classifier.add(Dense(10, input_dim = 5, activation = 'relu', kernel_initializer="uniform"))
classifier.add(Dense(5, activation = 'relu', kernel_initializer = 'uniform'))
classifier.add(Dense(1, activation = 'linear', kernel_initializer = 'uniform'))
classifier.compile(optimizer = optimizer, loss = 'mse', metrics = ['accuracy'])
return classifier
classifier = KerasClassifier(build_fn = build_classifier)
parameters = {'batch_size': [25, 32],
'epochs': [100, 500],
'optimizer': ['adam','rmsprop']}
grid_search = GridSearchCV(estimator = classifier, param_grid = parameters, scoring = 'accuracy', cv = 3)
grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_
First, define your model in a function(you could also define as a class with the keras subclassing API), then, since your output is a continuous value, call keras scikit-learn wrapper KerasRegressor
in order to work with GridSearchCV or RandomizedSearchCV.首先,在函数中定义您的模型(您也可以使用 keras 子类化 API 定义为一个类),然后,由于您的输出是一个连续值,因此调用 keras scikit-learn 包装器KerasRegressor
以便与 GridSearchCV 或 RandomizedSearchCV 一起使用。
Read this to better understand the differences between classification and regression problems.阅读本文以更好地理解分类和回归问题之间的差异。
def build_model(n_neurons=30, learning_rate=1e-3, input_shape=[8]):
model = keras.models.Sequential()
model.add(keras.layers.InputLayer(input_shape))
model.add(keras.layers.Dense(n_neurons, activation="relu"))
model.add(keras.layers.Dense(1))
optimizer = keras.optimizers.SGD(lr=learning_rate)
model.compile(loss="mse", optimizer=optimizer)
return model
keras_reg = keras.wrappers.scikit_learn.KerasRegressor(build_model)
params_distrib = { # params you want try with }
rnd_search_cv = RandomizedSearchCV(keras_reg, params_distrib, n_iter=5, cv=3)
rnd_search_cv.fit(x_train, y_train, epochs=50,
validation_data=(x_valid, y_valid))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.