【TensorFlow】InternalError:Dst tensor is not initialized

在VScode中使用jupyter notebook进行深度学习的训练过程中出现了如题所示问题,问题的完整描述为:

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.

在网上搜索了一段时间之后,发现几种已有的解决方式:

1.减小batch_size和样本数量:
这种解决方式个人觉得是有点治标不治本,减小样本数量也许还情有可原,因为这样确实会减小输入张量的大小,减小batch_size,张量并没有减小,但是同一时间输入GPU或是CPU的张量维度减小,可以减少负载,通过训练。
但是减少样本数量会导致训练效果变差,在我自己实验过程中发现减少batch_size同样没有解决问题。

2.改换tensorflow和tensorflow-gpu的版本:
tensorflow.python.framework.errors_impl.InternalError: ...... Dst tensor is not initialized._哒啦嘟噜cool的博客-CSDN博客问题:tensorflow.python.framework.errors_impl.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.我这个是运行代码的中间https://blog.csdn.net/weixin_43522665/article/details/124751925这个方法我真的差一点就去做了,因为我之前那篇博文也已经说了,本身对于我这样的tensorflow的组合就存疑,但是我还是想试试别的方法,这个作为最后的最后的方法(说实话,就是因为懒)。

3.释放GPU的内存:
当我看到这个的时候眼前一亮,因为他的描述很符合我当前的情况,就是长期使用jupyter notebook进行深度学习的训练才导致的,因此我决定试试,原文链接如下:
【TensorFlow】InternalError: Failed copying input tensor - harman-chen - 博客园⚠ TensorFlow-GPU 执行模型训练时报错: InternalError: Failed copying input tensor from /job:localhost/replica:0https://www.cnblogs.com/harmanchen/p/15517645.html代码我也贴到此处:

from keras.backend import set_session
from keras.backend import clear_session
from keras.backend import get_session
import tensorflow as tf
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))

然后就按照原文中的使用方式,将reset_keras()函数加到模型训练和模型评估之前:

reset_keras()
model.fit(x_train, y_train, epochs=4, batch_size=512)


reset_keras()
results = model.evaluate(x_test, y_test)
results

最后的输出结果前确有数字显示,且第一次运行时数字很大,约有8800,之后没对项目本身代码做任何改动,可以正常运行。因此我认为这是一个较优的解决方式,写一篇博客记录一下,方便日后寻找。


 

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值