我是靠谱客的博主 爱听歌雨,这篇文章主要介绍TensorFlow训练报错:ResourceExhaustedError: OOM when allocating tensor device:GPU:0 by allocator GPU_0_b,现在分享给大家,希望可以做个参考。

使用TensorFlow训练某些较大模型时会发生内存溢出,如果 已经安装了TensorFlow-GPU版本,训练时会优先调用GPU版本的TensorFlow,而一般电脑上显存比较小,很容易发生溢出,就会出现如下报错:

ResourceExhaustedError: OOM when allocating tensor with shape[1024,728,1,1] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc [[node model/block13_sepconv2/separable_conv2d (defined at <ipython-input-41-425b3e9b7078>:11) ]] Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info. [Op:__inference_train_function_41706] Function call stack: train_function

 

ResourceExhaustedError:  OOM when allocating tensor with shape[1024,728,1,1] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
	 [[node model/block13_sepconv2/separable_conv2d (defined at <ipython-input-41-425b3e9b7078>:11) ]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.
 [Op:__inference_train_function_41706]

Function call stack:
train_function

解决方案:

1. 尝试使用CPU进行训练,将model.fit()代码做如下修改:

with tf.device("/cpu:0"):
    history = model.fit(替换成自己的代码)

输出:

Epoch 1/50
21/86 [======>.......................] - ETA: 16:08 - loss: 0.4574 - accuracy: 0.8438

这样就可以使用CPU进行训练了。

2. 如果是在jupyter notebook中运行代码,则尝试先运行下面的代码:

tf.keras.backend.clear_session()

如果在notebook中运行了很多代码,则会占用一定的内存,上面的代码顾名思义就是清楚掉之前运行的一些session,以释放空间。

3. 如果还是不行,则只能修改代码,将批次数batch_size改小一些,每次给模型喂入小批量的数据。

 

 

 

 

 

 

最后

以上就是爱听歌雨最近收集整理的关于TensorFlow训练报错:ResourceExhaustedError: OOM when allocating tensor device:GPU:0 by allocator GPU_0_b的全部内容,更多相关TensorFlow训练报错:ResourceExhaustedError:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部