【问题标题】:Using tf.compat.v1.variable_scope produces an error - Tensorflow has no attribute variable_scope使用 tf.compat.v1.variable_scope 会产生错误 - Tensorflow 没有属性 variable_scope
【发布时间】:2020-08-06 10:35:57
【问题描述】:

我搜索了可能的解决方案,并且在任何地方都建议使用tf.compat.v1.variable_scope。我做了同样的事情,但我得到了错误:

module 'tensorflow' has no attribute 'variable_scope'

我正在使用谷歌 colab。

我的代码:

import tensorflow as tf #version - 2.3.0
with tf.compat.v1.variable_scope(self.name):
            self._build()

错误:

---> 45         with tf.compat.v1.variable_scope(self.name):
     46             self._build()
     47 

AttributeError: module 'tensorflow' has no attribute 'variable_scope'

关于如何摆脱这个错误的任何建议? 我的 TensorFlow 版本是 2.3.0。

编辑:

这是 Astrian_72954 建议的 Migrate your TensorFlow 1 code to TensorFlow 2 中的建议:

每个 v1.variable_scope 都应该转换为 Python 对象。 通常这将是以下之一:

tf.keras.layers.Layer 
tf.keras.Model
tf.Module

我不明白这个解决方案。有人能解释一下转换为 Python 对象是什么意思吗?

【问题讨论】:

  • 这是一个纯粹的tensorflowtensorflow2 问题,它实际上与machine-learningdeep-learning 无关——请不要编辑回标签(已删除)。跨度>

标签: python tensorflow tensorflow2


【解决方案1】:

这里的问题是 TensorFlow 2.0 不再支持某些 API。

由于 TensorFlow 2.0 中没有 tf.variable_scope(),因此您必须迁移代码。 Google 为 Tensorflow 提供了migration guide。我建议你仔细阅读。

因此您需要迁移代码,或使用旧版本的 TF(不推荐)。

【讨论】:

  • 感谢您的回答,请仔细阅读我的编辑并提出一些建议。
  • 解决方案有效吗?观察到[程序流程中的任何变化?
  • 实际上我不明白所提出的解决方案,关于转换为 python 对象的含义(在我的问题的编辑中完全怀疑)。如果可能请解释一下。谢谢。
猜你喜欢
  • 2018-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 2018-03-18
  • 2018-08-07
  • 1970-01-01
相关资源
最近更新 更多