我正在尝试运行这行代码:
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'
将 tf.get_variable 替换为 tf.Variable。
为社区的利益提及解决方案。
降级到 Tensorflow
1.X Version (1.14 or 1.15)
已经解决了这个问题,因为 Tensorflow
版本 2.0
不支持 get_variable()
.
tf.Variable 不适用于启动器。用这个代替
tf.compat.v1.get_variable
代替tf.Variable
。这适用于 tensorflow 2.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 是如何转换的。