0

我搜索了可能的解决方案,并在任何地方都建议使用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 建议的将 TensorFlow 1 代码迁移到 TensorFlow 2中的建议:

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

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

我不明白这个解决方案。有人可以解释转换为 Python 对象的含义吗?

4

1 回答 1

-1

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

由于tf.variable_scope()TensorFlow 2.0 中没有,您将不得不迁移您的代码。谷歌提供了Tensorflow 的迁移指南。我建议你仔细阅读它。

所以你需要迁移你的代码,或者使用旧版本的 TF(不推荐)。

于 2020-08-06T10:43:21.013 回答