Keras: Out of memory when doing hyper parameter grid search

后端 未结 2 865
甜味超标
甜味超标 2020-12-28 18:58

I\'m running multiple nested loops to do hyper parameter grid search. Each nested loop runs through a list of hyper parameter values and inside the innermost loop, a Keras s

2条回答
  •  梦谈多话
    2020-12-28 19:23

    Using the tip given by indraforyou, I added the code to clear the TensorFlow session inside the function I pass to GridSearchCV, like this:

    def create_model():
        # cleanup
        K.clear_session()
    
        inputs = Input(shape=(4096,))
        x = Dense(2048, activation='relu')(inputs)
        p = Dense(2, activation='sigmoid')(x)
        model = Model(input=inputs, outputs=p)
        model.compile(optimizer='SGD',
                  loss='mse',
                  metrics=['accuracy'])
        return model
    

    And then I can invoke the grid search:

    model = KerasClassifier(build_fn=create_model)
    grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1)
    

    It should work.

    Cheers!

提交回复
热议问题