TensorFlow-GPU 执行模型训练时报错:

InternalError: Failed copying input tensor from /job:localhost/replica:0/task:0/device:CPU:0 to /job:localhost/replica:0/task:0/device:GPU:0 in order to run _EagerConst: Dst tensor is not initialized.

解决方案:『TensorFlow: Dst tensor is not initialized - Stack Overflow』

主要原因在于 batch_size 太大,内存无法负载,将 batch_size 适当调小即可正常运行。

【注】默认情况下,TF 会尽可能地多分配占用 GPU 内存,通过调整 GPUConfig 可以设置为按需分配内存,参考 TensorFlow 文档和 TensorFlow 代码


另外,使用 Jupyter Notebook 进行长期模型训练时,可能由于 GPU 内存无法及时释放导致该报错。参考此答案可以解决此问题,定义如下函数:

from keras.backend import set_session
from keras.backend import clear_session
from keras.backend import get_session
import gc # Reset Keras Session
def reset_keras():
sess = get_session()
clear_session()
sess.close()
sess = get_session() try:
del classifier # this is from global space - change this as you need
except:
pass print(gc.collect()) # if it does something you should see a number as output # use the same config as you used to create the session
config = tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 1
config.gpu_options.visible_device_list = "0"
set_session(tf.compat.v1.Session(config=config))

需要清除 GPU 内存时,直接调用 reset_keras 函数即可。例如:

dense_layers = [0, 1, 2]
layer_sizes = [32, 64, 128]
conv_layers = [1, 2, 3] for dense_layer in dense_layers:
for layer_size in layer_sizes:
for conv_layer in conv_layers:
reset_keras()
# training your model here

【TensorFlow】InternalError: Failed copying input tensor的更多相关文章

  1. 【Tensorflow】tf.nn.depthwise_conv2d如何实现深度卷积?

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/mao_xiao_feng/article/ ...

  2. 【Tensorflow】tf.nn.atrous_conv2d如何实现空洞卷积?膨胀卷积

    介绍关于空洞卷积的理论可以查看以下链接,这里我们不详细讲理论: 1.Long J, Shelhamer E, Darrell T, et al. Fully convolutional network ...

  3. 【TensorFlow】tf.nn.max_pool实现池化操作

    max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow]tf.nn.conv2d是怎样实现卷积的? tf.nn.max_pool(va ...

  4. 【TensorFlow】自主实现包含全节点Cell的LSTM层 Cell

    0x00 前言 常用的LSTM,或是双向LSTM,输出的结果通常是以下两个:1) outputs,包括所有节点的hidden2) 末节点的state,包括末节点的hidden和cell大部分任务有这些 ...

  5. 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

    在计算loss的时候,最常见的一句话就是 tf.nn.softmax_cross_entropy_with_logits ,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化 ...

  6. 【TensorFlow】:解决TensorFlow的ImportError: DLL load failed: 动态链接库(DLL)初始化例程失败

    [背景] 在scikit-learn基础上系统结合数学和编程的角度学习了机器学习后(我的github:https://github.com/wwcom614/machine-learning),意犹未 ...

  7. 【转载】 【TensorFlow】static_rnn 和dynamic_rnn的区别

    原文地址: https://blog.csdn.net/qq_20135597/article/details/88980975 ----------------------------------- ...

  8. 【TensorFlow】tf.nn.conv2d是怎样实现卷积的?

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

  9. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  10. 【tensorflow】1.安装Tensorflow开发环境,安装Python 的IDE--PyCharm

    ================================================== 安装Tensorflow开发环境,安装Python 的IDE--PyCharm 1.PyCharm ...

随机推荐

  1. 18 网路进阶设定:Bridge、LACP、VLAN

    18 网路进阶设定:Bridge.LACP.VLAN 18.1 建立第二网路桥接装置(Bridge) 在预设安装完的情况下,PVE会使用其中一个连接埠桥接至[vmbr0]这个预设的网路桥接装置,所有的 ...

  2. Unity的超大开放世界解决方案

    https://blog.csdn.net/u011105442/article/details/104614043

  3. 在stm32中使用printf

    记录使用printf的方法 1.配置GPIO GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ...

  4. SVN的安装和使用手册2

    转载:http://www.cnblogs.com/armyfai/p/3985660.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需 ...

  5. Head_First_Python(中文版)值得花时间读的一本书

    Head_First_Python(中文版)书籍免费下载 提取码:u6z5 内容简介  · · · · · · 你想过可以通过一本书就学会Python吗?<Head First Python(中 ...

  6. linux系统过滤文件,并且通过时间对过滤的文件排序

    命令如下所示: find /home/deep/tf/20220601/study -name '*.h5' |xargs ls -lta

  7. python 非阻塞线程对话框,非qt(解决qt MessageBox使用线程时候卡死问题)

    def msg_okbox(self, strinfo, isYesno=False): if isYesno: return win32api.MessageBox(None, strinfo, & ...

  8. Using ML.NET in Jupyter notebooks 在jupyter notebook中使用ML.NET ——No design time or full build available

    题外话:标题直接用了微软开发博客的标题.近期需要调研一个项目的可行性,github上有个相似度很高的方案,但项目是写在jupyter里的C#代码.为了能跑起来看看效果,不得已搭了套相关的环境.本来以为 ...

  9. QT debug/moc_frmalarminfo.o:(.data.rel.ro._ZTV12FrmAlarmInfo[_ZTV12FrmAlarmInfo]+0x1c0): undefined reference to `non-virtual thunk to FrmAlarmInfo::~FrmAlarmInfo()'解决方法

    这个报错很具有迷惑性,,,我在网上还看见了ZTI12的报错,但是仔细一看发现是.o文件报错. 简单解释下.o文件(此解释来自百度): o 就是object, 也就相当于windows下编译的obj文件 ...

  10. Qt构造函数和析构函数报错undefinedreferenceto`vtable for 。。。。'

    百度了一堆,说各种的都有,无非就是说什么构造函数没有实现父类的纯虚函数,但是我明明继承并实现了,,,最后发现可能就是单纯的Qtcreater抽风了,一开始在构造里面清理了项目,发现不行. 于是一不做二 ...