AttributeError:模块“tensorflow”没有属性“get_variable”

问题描述 投票:0回答:4

我正在尝试运行这行代码:

var_init_1 = tf.get_variable("var_init_1", [1, 2], dtype=tf.int32,  initializer=tf.zeros_initializer)
print(var_init_1.shape)

它应该给出零张量形状的输出。

但是为什么我会收到这样的错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-37-3cc73aa1818e> in <module>
----> 1 var_init_1 = tf.get_variable("var_init_1", [1, 2], dtype=tf.int32,  initializer=tf.zeros_initializer)
      2 print(var_init_1.shape)

AttributeError: module 'tensorflow' has no attribute 'get_variable'
python python-3.x tensorflow deep-learning
4个回答
14
投票

将 tf.get_variable 替换为 tf.Variable。


3
投票

为社区的利益提及解决方案。

降级到 Tensorflow

1.X Version (1.14 or 1.15)
已经解决了这个问题,因为
Tensorflow
版本
2.0
不支持
get_variable()
.


3
投票

tf.Variable 不适用于启动器。用这个代替

tf.compat.v1.get_variable
代替
tf.Variable
。这适用于 tensorflow 2.0 及更高版本。


0
投票

我遇到了这个问题和 tf v1 与 tf v2 的其他问题。我发现最好的解决方案是在这个积压链接上提出的:Module 'tensorflow' has no attribute 'contrib'

$tf_upgrade_v2 \
--intree my_project/ \
--outtree my_project_v2/ \
--reportfile report.txt

基本上你只需将 tf v1 项目转换为 tf v2。大多数时候它会起作用。您确实需要检查 get_variable 转换并确保它是您想要的。检查 report.txt 是如何转换的。

© www.soinside.com 2019 - 2024. All rights reserved.