[英]fit: passing multiple inputs with sklearn.model_selection.GridSearchCV
as explained in https://keras.io/models/model , Keras' fit
function x
can be如https://keras.io/models/model 中所述,Keras 的
fit
函数x
可以是
Numpy array of training data (if the model has a single input), or list of Numpy arrays (if the model has multiple inputs).
训练数据的 Numpy 数组(如果模型有单个输入),或 Numpy 数组列表(如果模型有多个输入)。 If input layers in the model are named, you can also pass a dictionary mapping input names to Numpy arrays.
如果模型中的输入层已命名,您还可以将字典映射输入名称传递给 Numpy 数组。
but it doesn't seem to be possible with sklearn.model_selection.GridSearchCV fit
.但 sklearn.model_selection.GridSearchCV
fit
似乎不可能。 Is that true?真的吗? Is there any workaround?
有什么解决方法吗?
Thanks!谢谢!
If you want to just use grid search as in sci-kit learn, you can use the KerasClassifier wrapper.如果您只想像在 sci-kit learn 中那样使用网格搜索,则可以使用 KerasClassifier 包装器。 You just create your model in your function.
您只需在函数中创建模型。
def create_model(dropout_rate):
#...
model = KerasClassifier(build_fn=create_model)
param_grid = dict(dropout_rate=[0.2,0.3,0.5])
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(X, Y)
https://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/ https://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/
Unfortunately, this approach is kind of limited, as you don´t have a validation after each epoch.不幸的是,这种方法有一定的局限性,因为您没有在每个 epoch 之后进行验证。 To solve this, you need to implement your own KerasClassifier.
要解决这个问题,您需要实现自己的 KerasClassifier。
https://github.com/keras-team/keras/issues/4278 https://github.com/keras-team/keras/issues/4278
The short answers are yes and yes.简短的回答是肯定的。
GridSearchCV checks that X and y have the same length, I assume this is required for splitting the data to do cross-validation. GridSearchCV 检查 X 和 y 的长度是否相同,我认为这是拆分数据以进行交叉验证所必需的。 If you have multiple inputs and the first dimension of X represents this number of inputs this check wil fail.
如果您有多个输入并且 X 的第一个维度表示输入的数量,则此检查将失败。
There is an issue to support multiple input layers: https://github.com/keras-team/keras/issues/9001 .支持多个输入层存在一个问题: https : //github.com/keras-team/keras/issues/9001 。 Also check this issue for a possible workaround.
另请检查此问题以获取可能的解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.