Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x tf.keras.Model.load_weights()捕获的资源排风机错误_Python 3.x_Tensorflow_Keras_Resources - Fatal编程技术网

Python 3.x tf.keras.Model.load_weights()捕获的资源排风机错误

Python 3.x tf.keras.Model.load_weights()捕获的资源排风机错误,python-3.x,tensorflow,keras,resources,Python 3.x,Tensorflow,Keras,Resources,我有两个ipynb文件:train.ipynb和predict.ipynb。我在train.ipynb中使用fit generator(批次大小为64)训练了一个模型,并在尝试在predict.ipynb中加载权重时捕获了ResourceExhausterRor 我在tensorflow v1.9和tensorflow docker图像中使用keras # train.ipynb def network(): #[ A normal model] return model mo

我有两个ipynb文件:
train.ipynb
predict.ipynb
。我在
train.ipynb
中使用fit generator(批次大小为64)训练了一个模型,并在尝试在
predict.ipynb中加载权重时捕获了
ResourceExhausterRor
我在tensorflow v1.9和tensorflow docker图像中使用keras

# train.ipynb

def network():
    #[ A normal model]
    return model
model = network()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit_generator(seq,shuffle=True,
                    epochs = 10, verbose=1
                   )
# save the model and weight after training
with open('model.json','w') as json_file:
    json_file.write(model.to_json())
model.save_weights('model.h5')
clear_session() # tried to clear the session here
# saved both successfully
# model.h5(131MB)
成功保存后,我可以将其加载回
train.ipynb
中。但是,当我在
predict.ipynb
中执行相同操作时,会发现一个错误

# train.ipynb
with open('model.json','r') as json_file:
    test_model = model_from_json(json_file.read())
test_model.load_weights('model.h5')
# No error here

# predict.ipynb
with open('model.json','r') as json_file:
    test_model = model_from_json(json_file.read())
test_model.load_weights('model.h5')
# Got the following error
ResourceExhaustedError: OOM when allocating tensor with shape[28224,1024] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc

感谢您的帮助

您是否同时运行两台笔记本电脑?你的GPU内存不足。请尝试在命令行中使用nvidia smi检查GPU的资源使用情况,但请注意默认情况下TensorFlow会占用所有可用的GPU内存
keras.backend.clear_session()
也会有所帮助

没有,我是分开跑的。保存模型后,我调用了clear_session()。请参阅上面的代码片段。