我想将RandomizedSearchCV(或GridSearchCV)应用于我的Keras模型(TensorFlow后端)。但是,在使用不同的超参数集训练几次后,发生了OOM错误。
以下是我的代码和错误消息。我怎么解决这个问题?提前谢谢。
def build_model(num_filters = 10,
num_classes = 6,
sequence_max_length=512,
vocab_size=71,
embedding_size=16,
learning_rate=0.001,
dropout = 0.2,
top_k=3,
embedding_matrix = None,
model_path=None):
... do something
return model
keras_model = KerasClassifier(build_fn=build_model,
embedding_matrix = embedding_matrix)
random_search_model = RandomizedSearchCV(keras_model,
n_iter = 5,
param_distributions = hparm_dist,
refit = True,
n_jobs = 1)
错误讯息:
ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[471512,300]
更新
通过将keras.backend.clear_session()添加到sklearn.cross_validation._fit_and_score的末尾来解决。
答案 0 :(得分:0)
您可以尝试将'pre_dispatch'参数更改为1. By default it is 2*n_jobs
random_search_model = RandomizedSearchCV(keras_model,
n_iter = 5,
param_distributions = hparm_dist,
refit = True,
pre_dispatch=1,
n_jobs = 1)