相关疑难解决方法(0)

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

我正在运行多个嵌套循环来进行超参数网格搜索.每个嵌套循环遍历超级参数值列表,并且在最内层循环内部,每次使用生成器构建和评估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] …
Run Code Online (Sandbox Code Playgroud)

python memory-management out-of-memory keras grid-search

14
推荐指数
2
解决办法
4098
查看次数

Keras(TensorFlow,CPU):训练循环中的顺序模型吃内存

我试图在循环中训练1000x的Sequential模型.在每个循环中,我的程序都会泄漏内存,直到我用完并获得OOM异常.

之前我已经问了一个类似的问题(连续训练多个连续模型减速)

并且看到其他类似的问题(Keras:进行超参数网格搜索时内存不足)

并且在K.clear_session()使用完模型后,解决方案始终是添加到代码中.所以我在上一个问题中做到了这一点,我仍在泄露记忆

这是重现问题的代码.

import random
import time
from keras.models import Sequential
from keras.layers import Dense
from keras import backend as K
import tracemalloc


def run():
    tracemalloc.start()
    num_input_nodes = 12
    num_hidden_nodes = 8
    num_output_nodes = 1

    random_numbers = random.sample(range(1000), 50)
    train_x, train_y = create_training_dataset(random_numbers, num_input_nodes)

    for i in range(100):
        snapshot = tracemalloc.take_snapshot()
        for j in range(10):
            start_time = time.time()
            nn = Sequential()
            nn.add(Dense(num_hidden_nodes, input_dim=num_input_nodes, activation='relu'))
            nn.add(Dense(num_output_nodes))
            nn.compile(loss='mean_squared_error', optimizer='adam')
            nn.fit(train_x, train_y, nb_epoch=300, batch_size=2, verbose=0) …
Run Code Online (Sandbox Code Playgroud)

python-3.x keras tensorflow

9
推荐指数
1
解决办法
4035
查看次数

keras预测内存交换无限增加

我使用keras实现了一个分类程序.我有一大堆图像,我想使用for循环预测每个图像.

但是,每次计算新图像时,交换内存都会增加.我试图删除预测函数中的所有变量(我确信它在此函数内部存在问题)但内存仍然增加.

for img in images:
    predict(img, model, categ_par, gl_par)
Run Code Online (Sandbox Code Playgroud)

和相应的功能:

def predict(image_path, model, categ_par, gl_par):   
    print("[INFO] loading and preprocessing image...")

    orig = cv2.imread(image_path)  

    image = load_img(image_path, target_size=(gl_par.img_width, gl_par.img_height))  
    image = img_to_array(image)  

    # important! otherwise the predictions will be '0'  
    image = image / 255  

    image = np.expand_dims(image, axis=0)

    # build the VGG16 network
    if(categ_par.method == 'VGG16'):
        model = applications.VGG16(include_top=False, weights='imagenet')  

    if(categ_par.method == 'InceptionV3'):
        model = applications.InceptionV3(include_top=False, weights='imagenet')  

    # get the bottleneck prediction from the pre-trained VGG16 model  
    bottleneck_prediction = …
Run Code Online (Sandbox Code Playgroud)

python deep-learning keras

3
推荐指数
1
解决办法
1529
查看次数