训练lstm模型后,内存未初始化

问题描述

def LstmSingle(name,train,vaild,test,idx,epoch=500,layer_size=32,time_stap=10,learning_rate=5e-3,batch_size=8):
    idx_text = ["news","sns","sq"]
    wgh_path = "./weight/" + name + "_" + idx_text[idx] + ".wgh"
    
    K.clear_session()
    model = Sequential()
    model.add(LSTM(layer_size,input_shape = (time_stap,2),return_sequences = True))
    model.add(LSTM(layer_size,2)))
    model.add(Dense(layer_size,activation='relu'))
    model.add(Dense(layer_size,activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1,activation='sigmoid'))
    model.compile(loss='mse',optimizer=Adam(learning_rate=learning_rate))
    
    history = None
    if not os.path.isfile(wgh_path):
        checkpoint = ModelCheckpoint(filepath=wgh_path,monitor='val_loss',verbose=0,save_best_only=True)
        history = model.fit(
            train["x"][idx],train["y"],validation_data = (vaild["x"][idx],vaild["y"]),epochs=epoch,batch_size=batch_size,callbacks=[checkpoint]
        )

        #garbege collect
        del checkpoint
        gc.collect()
    #end if

    model.load_weights(wgh_path)

    #Print learning histroy
    if history != None:
        plt.xlabel('Epoch')
        plt.ylabel('Loss')
        plt.title('Learning progress')
        plt.plot(history.history['loss'],color="black",label="loss")
        plt.plot(history.history['val_loss'],color="blue",label="val_loss")
        plt.legend()
        plt.show()
    #end if
    
    predict_train = model.predict(train["x"][idx])
    predict_vaild = model.predict(vaild["x"][idx])
    predict_test = model.predict(test["x"][idx])
    K.clear_session()
    model.reset_states()
    
    #garbege collect
    del model
    gc.collect()
    
    return {
        "train" : predict_train,"vaild" : predict_vaild,"test" : predict_test
    }
#end def

我训练了上面的模型,但是当我训练下一个训练数据时,先前使用的内存仍然没有初始化。

根据任务管理器。我的模型内存使用量最大为10.3GB。 当我拟合模型时,内存使用量正在增加。拟合模型后的内存仍然不会减少。

我该如何解决?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...