tensorflow踩坑记录

博主在寒假期间学习TensorFlow并搭建网络进行训练,分享了ResourceExhaustedError和ValueError的解决经验,特别是在搭建双向LSTM网络时遇到的变量重复问题及其解决方法。
部署运行你感兴趣的模型镜像

为了下学期的实验做准备,我在寒假学习了tensorflow,边学边搭建网络进行训练。在学习的路上踩了不少坑,但是总体还算顺利。这些坑中,除了网络本身的bug和tf语法错误,还有几个特别要注意的异常。

ResourceExhaustedError

这个异常表示资源耗尽,通常指在为参数变量申请内存(显存)时,剩余可用空间不足。解决办法主要有三条思路

  1. 使用CPU训练
  2. 减少网络的参数变量
    减小batch_size、词向量维度、网络层数、神经元个数
  3. 使用更好的GPU

ValueError: Variable rnn/basic_lstm_cell/kernel already exists, disallowed.

我在搭建双向lstm网络时遇到此问题。
forward_lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
backward_lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)
在网上查了异常,坑友都指出此异常与变量作用域有关,但是解决方案都不一样,包括使用with tf.variable_scope(name):pass来定义作用域,还有在代码中添加t f.reset_default_graph()。
具体的原理我暂时没有探究。我试着为这两层网络分别添加名称,问题解决。
forward_lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size, name = 'forward_lstm_cell')
backward_lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size, name = 'backward_lstm_cell')

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

为了解决AttributeError: module 'tensorflow.compat.v1' has no attribute 'contrib'的问题,您可以尝试以下步骤: 1. 确保您的TensorFlow版本是兼容的。在较新的版本中,tensorflow.compat.v1.contrib已被弃用。您可以尝试使用其他方法或功能来替代。 2. 检查您的导入语句是否正确。确保您正在导入正确的模块和属性。 3. 如果您正在使用旧版本的TensorFlow,请考虑升级到最新版本。新版本的TensorFlow通常会修复一些已知的问题和错误。 对于第二个问题,ImportError: DLL load failed: 找不到指定的模块,您可以尝试以下解决方法: 1. 确保您已经正确安装了TensorFlow以及相关的依赖项。 2. 检查您的环境变量是否正确设置。确保您已经将TensorFlow所在的目录添加到了您的环境变量中。 3. 如果您使用的是Anaconda环境,请确保您已经正确激活了您的环境。您可以使用命令"conda activate tensorFlow"来激活tensorFlow环境。 对于AttributeError: module '_pywrap_tensorflow_internal' has no attribute 'TF_ListPhysicalDevices'的问题,这通常是由于TensorFlow版本不兼容或安装错误引起的。您可以尝试以下解决方法: 1. 确保您的TensorFlow版本是兼容的,并且已经正确安装了所有依赖项。 2. 检查您的导入语句是否正确,并且没有错误或拼写错误。 3. 如果您使用的是Anaconda环境,请确保您已经正确激活了tensorFlow环境,并且已经正确安装了所有需要的包。 如果上述方法都无法解决问题,您可以尝试重新安装TensorFlow,或者参考TensorFlow官方文档或社区来获取更多解决方案和帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.youkuaiyun.com/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [安装TensorFlow记录](https://blog.youkuaiyun.com/bolatu_youshang/article/details/108929639)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值