简体   繁体   English

当输出变量是连续的时,我们如何调整神经网络的超参数?

[英]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:不支持连续”

  1. I'm not sure if GridSearch works when the output variable is continuous, but even if it doesn't, there must be some other functions that can help me optimize my parameters?当输出变量是连续的时,我不确定 GridSearch 是否工作,但即使它没有,也必须有一些其他函数可以帮助我优化我的参数?
  2. Do we need to normalize input as well as output variables in this scenario?在这种情况下,我们是否需要标准化输入和输出变量? What is the best way of doing so for continuous input and output variables?对于连续输入和输出变量,这样做的最佳方法是什么?
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM