【问题标题】:AttributeError: module 'tensorflow' has no attribute 'Variable' "ERROR"AttributeError:模块“tensorflow”没有属性“变量”“错误”
【发布时间】:2017-07-26 04:50:47
【问题描述】:

我用了一天tensor-flow,但是遇到了一些麻烦,当我导入tensor-flow时,会出现AttributeError: 'module' object has no attribute 'variable'

我使用 Windows10、Python 3.5.3、Anaconda-3 4.4.0

这是我的测试代码:

import tensorflow as tf
my_var = tf.Variable(tf.linspace(10.0, 13.0, 4)) 
with tf.Session() as sess:
    print (sess.run(my_var))

我收到了这个错误:

【问题讨论】:

    标签: python windows tensorflow anaconda


    【解决方案1】:

    看起来您编写了一个模块random.py,它隐藏了标准库的random 模块。尝试重命名文件并检查错误是否消失。您可以在您发布的堆栈跟踪的底部看到它正在导入您的 random.py。

    【讨论】:

      【解决方案2】:

      用“变量”替换“变量”,例如以下代码给出错误:

      initial_value=tf.random.normal(shape=(2,2))
      a = tf.variable(initial_value)
      print(a)
      

      但是这段代码给出了成功的输出

      initial_value=tf.random.normal(shape=(2,2))
      a = tf.Variable(initial_value)
      print(a)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 2020-04-29
        • 1970-01-01
        • 2017-04-08
        • 2020-02-04
        • 2020-02-12
        • 2019-01-10
        相关资源
        最近更新 更多