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. 如何在 JavaScript 中使用媒体查询

    前言 说起媒体查询想必大家最先想到的都是CSS中@media,没错,这是我们最常用的媒体查询方法,主要用来为我们的网站做适配处理. 比如: h1 { font-size: 2rem; color: g ...

  2. http 协议( 简单了解 )

    HTTP 请求协议 超文本传输协议,是一种用于分布式写作式和媒体信息系统的应用层协议. HTTP是万维网的数据通信的基础. HTTP协议概述 HTTP是一个客户端终端 (用户) 和服务器端 ( 网端) ...

  3. kotlin学习-Coroutines(协程)

    协程(又名纤程),轻量级线程(建立在线程基础上,属于用户态调用),非阻塞式编程(像同步编写一样),在用户态内进行任务调度,避免与内核态过多交互问题,提高程序快速响应.协程使用挂起当前上下文替代阻塞,被 ...

  4. centos 修改ip

    TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=staticDEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6IN ...

  5. winform 中 label透明化

    label1.BackColor = Color.Transparent;//设置背景颜色为透明 label1.Parent = pictureBox1;//将pictureBox1设为标签的父控件, ...

  6. Docker Mysql修改时区

    背景 时区是使用了世界标准时间(UTC).因为在中国使用,所以需要把时区改成东八区的 或者启动容器时设置 -e TZ=Asia/Shanghai 永久修改 进入容器 docker exec -it m ...

  7. 2022-6,flask+vue+uwsgi+nginx,线上部署完整流程打包配置文件

    uwsgi配置文件 [uwsgi] # 服务端口号,这里没有设置IP值,默认是加载服务器的IP地址 http = :8000 # flask项目地址 chdir = /home/flask_proje ...

  8. Linux中启动Docker容器报错:Error response from daemon: driver failed programming external connectivity

    在启动Docker的容器时,会出现报错:Error response from daemon: driver failed programming external connectivity on e ...

  9. CentOS 7关闭防火墙 SElinux 配ip

    屏蔽出站 iptables -t filter -A OUTPUT --dst 192.168.0.191/32 -j DROP iptables -t filter -A OUTPUT --dst ...

  10. drools规则的入门使用

    1:pom文件 <!--drools--><dependency> <groupId>org.drools</groupId> <artifact ...