Keras:进行超参数网格搜索时内存不足

Ale*_*lex 14 python memory-management out-of-memory keras grid-search

我正在运行多个嵌套循环来进行超参数网格搜索.每个嵌套循环遍历超级参数值列表,并且在最内层循环内部,每次使用生成器构建和评估Keras顺序模型.(我没有做任何训练,我只是随机初始化,然后多次评估模型,然后检索平均损失).

我的问题是,在这个过程中,Keras似乎填满了我的GPU内存,所以我最终得到了一个OOM错误.

在评估模型后,是否有人知道如何解决这个问题并释放GPU内存?

在评估之后我根本不再需要模型,我可以在内循环的下一次传递中构建一个新模型之前完全抛弃它.

我正在使用Tensorflow后端.

这是代码,尽管其中大部分与一般问题无关.该模型构建在第四个循环内,

for fsize in fsizes:
Run Code Online (Sandbox Code Playgroud)

我想有关如何构建模型的细节并不重要,但无论如何都是这样的:

model_losses = []
model_names = []

for activation in activations:
    for i in range(len(layer_structures)):
        for width in layer_widths[i]:
            for fsize in fsizes:

                model_name = "test_{}_struc-{}_width-{}_fsize-{}".format(activation,i,np.array_str(np.array(width)),fsize)
                model_names.append(model_name)
                print("Testing new model: ", model_name)

                #Structure for this network
                structure = layer_structures[i]

                row, col, ch = 80, 160, 3  # Input image format

                model = Sequential()

                model.add(Lambda(lambda x: x/127.5 - 1.,
                          input_shape=(row, col, ch),
                          output_shape=(row, col, ch)))

                for j in range(len(structure)):
                    if structure[j] == 'conv':
                        model.add(Convolution2D(width[j], fsize, fsize))
                        model.add(BatchNormalization(axis=3, momentum=0.99))
                        if activation == 'relu':
                            model.add(Activation('relu'))
                        if activation == 'elu':
                            model.add(ELU())
                            model.add(MaxPooling2D())
                    elif structure[j] == 'dense':
                        if structure[j-1] == 'dense':
                            model.add(Dense(width[j]))
                            model.add(BatchNormalization(axis=1, momentum=0.99))
                            if activation == 'relu':
                                model.add(Activation('relu'))
                            elif activation == 'elu':
                                model.add(ELU())
                        else:
                            model.add(Flatten())
                            model.add(Dense(width[j]))
                            model.add(BatchNormalization(axis=1, momentum=0.99))
                            if activation == 'relu':
                                model.add(Activation('relu'))
                            elif activation == 'elu':
                                model.add(ELU())

                model.add(Dense(1))

                average_loss = 0
                for k in range(5):
                    model.compile(optimizer="adam", loss="mse")
                    val_generator = generate_batch(X_val, y_val, resize=(160,80))
                    loss = model.evaluate_generator(val_generator, len(y_val))
                    average_loss += loss

                average_loss /= 5

                model_losses.append(average_loss)

                print("Average loss after 5 initializations: {:.3f}".format(average_loss))
                print()
Run Code Online (Sandbox Code Playgroud)

ind*_*you 18

正如Tensorflow中使用的后端所示.在Tensorflow后端,当前模型不会被破坏.所以你需要清除会话.

使用完模型之后只需:

if K.backend() == 'tensorflow':
    K.clear_session()
Run Code Online (Sandbox Code Playgroud)

包括后端:

from keras import backend as K
Run Code Online (Sandbox Code Playgroud)

您也可以使用sklearn包装器进行网格搜索.检查这个例子:这里.此外,对于更高级的超参数搜索,您可以使用hyperas.


小智 6

使用indraforyou提供的技巧,我添加了代码以清除传递给GridSearchCV的函数中的TensorFlow会话,如下所示:

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
Run Code Online (Sandbox Code Playgroud)

然后我可以调用网格搜索:

model = KerasClassifier(build_fn=create_model)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1)
Run Code Online (Sandbox Code Playgroud)

它应该工作。

干杯!