【最简单解决办法】:module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘

这篇具有很好参考价值的文章主要介绍了【最简单解决办法】:module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

出现错误界面

1.问题原因

2.解决办法

(1)在调用之前首先添加如下代码块并执行

(2)查找响应函数对应的函数调用前缀。

注:若出现错误也是LSTM预测,可直接复制如下代码:

最后问题解决,代码最后成功运行


anaconda下:

tensorflow版本:tensorflow2.3.0

python版本:python3.7

出现错误界面

【最简单解决办法】:module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘ 

1.问题原因

原因是tensorflow 2.0版本之后把contrib这个库取消了

2.解决办法

简单说法:到tensorflow官网查询相对应的函数调用方式即可。

(1)在调用之前首先添加如下代码块并执行

# import tensorflow as tf
# tf.compat.v1.reset_default_graph()

import tensorflow.compat.v1 as tf
tf.reset_default_graph()

tf.compat.v1.disable_eager_execution()

(2)查找响应函数对应的函数调用前缀。

tensorflow官网TensorFlow (google.cn)

在搜索框直接输入对应函数即可。【最简单解决办法】:module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘

以我的函数 DropoutWrapper为例:

【最简单解决办法】:module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘

直接输入红色框住部分的函数调用即可。

亲测 tf.compat.v1. 对绝大多数部分的函数都可以使用

涉及到tf调用函数的部分全部加上tf.compat.v1.  

最终问题解决,代码执行成功。

注:若出现错误也是LSTM预测,可直接复制如下代码:

def lstm_cell(size_layer):
    return tf.compat.v1.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)

    rnn_cells = tf.compat.v1.nn.rnn_cell.MultiRNNCell(
            [lstm_cell(size_layer) for _ in range(num_layers)],
            state_is_tuple = False,
        )
    self.X = tf.compat.v1.placeholder(tf.compat.v1.float32, (None, None, size))
    self.Y = tf.compat.v1.placeholder(tf.compat.v1.float32, (None, output_size))
    drop = tf.compat.v1.nn.rnn_cell.DropoutWrapper(
            rnn_cells, output_keep_prob = forget_bias
        )
    self.hidden_layer = tf.compat.v1.placeholder(
            tf.compat.v1.float32, (None, num_layers * 2 * size_layer)
        )
    self.outputs, self.last_state = tf.compat.v1.nn.dynamic_rnn(
            drop, self.X, initial_state = self.hidden_layer, dtype = tf.compat.v1.float32
        )
    self.logits = tf.compat.v1.layers.dense(self.outputs[-1], output_size)
    self.cost = tf.compat.v1.reduce_mean(tf.square(self.Y - self.logits))
    self.optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(
            self.cost
        )
        
def forecast():#此处为部分代码
    tf.compat.v1.reset_default_graph()
    modelnn = Model(
        learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate
    )
    sess = tf.compat.v1.InteractiveSession()
    sess.run(tf.compat.v1.global_variables_initializer())
    date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()

最后问题解决,代码最后成功运行

【最简单解决办法】:module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘ 

 文章来源地址https://www.toymoban.com/news/detail-424334.html

到了这里,关于【最简单解决办法】:module ‘tensorflow.compat.v1‘ has no attribute ‘contrib‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 解决AttributeError: module ‘tensorflow.python.keras‘ has no attribute ‘Model‘

    目录 解决AttributeError: module \\\'tensorflow.python.keras\\\' has no attribute \\\'Model\\\' 引言 错误原因 解决方案 1. 升级TensorFlow版本 2. 正确导入模块 3. 检查其他依赖项 4. 重新安装TensorFlow 结论 实际应用场景: 引言 在使用TensorFlow的过程中,您可能会遇到各种错误。其中之一是​ ​AttributeError:

    2024年02月05日
    浏览(41)
  • 解决AttributeError: module tensorflow has no attribute reset_default_graph

    目录 解决AttributeError: module tensorflow has no attribute reset_default_graph 错误原因 解决方法 步骤1: 查看TensorFlow版本 步骤2: 替换过时的方法或属性 步骤3: 更新代码 步骤4: 手动重置默认图(如果适用) 结论 在使用TensorFlow进行深度学习任务时,有时会遇到类似于\\\"AttributeError: module \\\'te

    2024年02月04日
    浏览(34)
  • AttributeError: module ‘numpy‘ has no attribute ‘array‘解决办法

    NumPy是Python中重要的数值计算库,提供了强大的数组操作和数学函数。然而,有时候我们可能会在使用NumPy时遇到\\\"AttributeError: module ‘numpy’ has no attribute ‘array’\\\"的错误提示,这可能会让一些用户感到困惑。在本文中,我们将分享如何解决这个问题的方法,并帮助读者更好地

    2024年02月13日
    浏览(47)
  • AttributeError: module ‘torch.nn‘ has no attribute ‘SiLU‘问题的解决办法

    AttributeError: module \\\'torch.nn\\\' has no attribute \\\'SiLU\\\'        这个问题的原因是这个版本的torch中的torch.nn函数表里面没有这个SiLU函数,需要更高版本的torch,在官方的文档中我们就可以查看这个版本里有没有包含这个模块。 官方文档1.1.0    红框选择 torch 版本,我的版本是1.1.0,搜

    2024年02月13日
    浏览(41)
  • AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘的解决办法

    是在使用python的matplotlib库的时候发现无法绘制和老师一样的图 一开始我还以为是我的matoltlib和我的python版本不匹配后面发现真正原因其实是matplotlib 的 backend的默认渲染器是agg,agg是一个没有图形显示界面的终端,如果要图像正常显示,则需要切换为图形界面显示的终端TkA

    2024年02月06日
    浏览(31)
  • python遇到AttributeError: module ‘XXX‘ has no attribute ‘XXX‘的错误,解决办法

    错误原因: 主要的原因看可能有3个: 1、检查一下有没有拼错函数,有时候写的快了真的有可能手误; 2、如果没拼错那就检测一下该模块的函数是否是更新了,因为python的第三方库是时常会更新的,有时候函数名会有轻微的变动这也很正常,解决方法就是查看一下对应版本

    2024年04月12日
    浏览(29)
  • Python 报错 “ AttributeError: module ‘backend_interagg‘ has no attribute ‘FigureCanvas‘ “ 的解决办法 ?

    一、原因 matplotlib 的 backend的默认渲染器是agg,agg是一个没有图形显示界面的终端,如果要图像正常显示,则需要切换为图形界面显示的终端TkAgg。 二、解决办法 修改为:

    2024年02月03日
    浏览(29)
  • 解决AttributeError: module ‘keras‘ has no attribute ……

    在成功解决AttributeError: module ‘keras‘ has no attribute ‘utils‘_new1998的博客-CSDN博客这篇博客中博主有提到如何解决这一问题,其中就是要把 更改成为 而博主不知道其中原因,原因其实是在TensorFlow 2.4及以上版本中, import keras 的方式已经被弃用,取而代之的是 import tensorflow.k

    2024年02月11日
    浏览(35)
  • 解决AttributeError: module ‘serial‘ has no attribute ‘Serial‘

    最近在搞上位机时遇到了报错AttributeError: module ‘serial’ has no attribute ‘Serial’,翻译过来就是serial类没有Serial对象。然后卡了一个小时才解决,试了网上很多方法,最后才发现报错原因,这问题python也有责任。 下面说下一般的解决方法。 python3之后串口都改为pyserial,seria

    2024年02月02日
    浏览(36)
  • AttributeError: module ‘numpy‘ has no attribute ‘bool‘解决

    问题原因:在numpy的1.24版本已经弃用了np.bool这个名称,取而代之的是np.bool_ 解决方法: 1.点击出错文件 2.将np.bool更改为np.bool_

    2024年02月12日
    浏览(81)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包