【问题标题】:ModuleNotFoundError: No module named 'tensorflow.contrib' with tensorflow=2.0.0ModuleNotFoundError:没有名为 'tensorflow.contrib' 且 tensorflow=2.0.0 的模块
【发布时间】:2020-07-07 23:53:09
【问题描述】:

我使用的是 TensorFlow 版本=2.0.0 蟒蛇版本=3.7.3 我正在尝试导入以下语句

from tensorflow.contrib import rnn

它给出的错误是 模块“tensorflow”没有属性“contrib” 我该如何解决这个问题?

【问题讨论】:

  • 如果您调用了文件tensorflow.py,请选择其他名称。

标签: python tensorflow nlg


【解决方案1】:

来自张量流

https://www.tensorflow.org/guide/upgrade#compatibility_modules

由于 TensorFlow 2.x 模块弃用(例如,tf.flags 和 tf.contrib),某些更改无法通过切换到 compat.v1 来解决。升级此代码可能需要使用额外的库(例如 absl.flags)或切换到 tensorflow/addons 中的包。

如本帖所述

tensorflow.contrib doesn't exist in 2.0.

https://github.com/tensorflow/tensorflow/issues/31350#issuecomment-518749548

【讨论】:

  • 你能告诉..如何将这些附加库与 tensorflow 2.0 一起使用
  • 抱歉模块弃用意味着它在这个版本中消失了
  • 有什么替代品吗?我有一个使用这个模块的存储库,我必须升级它
【解决方案2】:

我没有使用过旧版本的 tensorflow。这是你要找的吗?

from tensorflow.keras.layers import RNN

关于贡献的信息:
https://www.tensorflow.org/guide/migrate#a_note_on_slim_contriblayers

【讨论】:

  • 使用这个会产生错误AttributeError: module 'tensorflow_core._api.v2.nn' has no attribute 'rnn_cell' 实际上我正在尝试做这样的事情self.cell = tf.nn.rnn_cell.BasicLSTMCell
  • 您无法导入 keras 图层吗?
  • import tensorflow.keras.layers as layers 然后... self.cell = layers.LSTMCell(units=units, activation=activation, dropout=dropout)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-28
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 2020-07-13
相关资源
最近更新 更多