Python TensorFlow 'module' 对象没有属性 'global_variables_initializer'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40511562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:36:57  来源:igfitidea点击:

TensorFlow 'module' object has no attribute 'global_variables_initializer'

pythontensorflowdeep-learningword2vec

提问by Le D. Thang

I'm new to Tensorflow I'm running a Deep learning Assignment from Udacity on iPython notebook. link

我是 Tensorflow 的新手,我正在 iPython notebook 上运行 Udacity 的深度学习作业。 关联

And it has an error.

它有一个错误。

AttributeError                            Traceback (most recent call last)
`<ipython-input-18-3446420b5935>` in `<module>`()
  2 
  3 with tf.Session(graph=graph) as session:
----> 4   tf.global_variables_initializer().run()

AttributeError: 'module' object has no attribute 'global_variables_initializer'

Please help! How can I fix this? Thank you.

请帮忙!我怎样才能解决这个问题?谢谢你。

回答by drpng

In older versions, it was called tf.initialize_all_variables.

在旧版本中,它被称为tf.initialize_all_variables.

回答by kmario23

Seems like you're using tensorflow 0.11or older versions. If you see this git-commit, they replaced initialize_all_variableswith global_variables_initializer.

似乎您正在使用tensorflow 0.11或更旧的版本。如果你看到这个git-commit,他们会initialize_all_variablesglobal_variables_initializer.

So, either you can use initialize_all_variablesor update to a newer version i.e. (0.12) or later.

因此,您可以使用initialize_all_variables或更新到较新的版本,即 (0.12) 或更高版本。

回答by ashwinjoseph

In Tensorflow 2.0 , it does away from session and switches to eager execution. You can still run your code using session if you refer to tf.compat library and disable eager execution. This command should do the work:

在 Tensorflow 2.0 中,它不再使用会话并切换到急切执行。如果您参考 tf.compat 库并禁用 Eager Execution,您仍然可以使用 session 运行您的代码。这个命令应该做的工作:

tf.compat.v1.global_variables_initializer()

回答by Maoz Tamir

In Tensorflow 1.11 You can use both initialize_all_variables().run() or tf.global_variables_initializer()

在 Tensorflow 1.11 中,您可以同时使用 initialize_all_variables().run() 或 tf.global_variables_initializer()

  #tf.global_variables_initializer().run()
  tf.initialize_all_variables().run()

both of then works fine on

然后两者都可以正常工作