Python AttributeError: 模块“tensorflow”没有属性“feature_column”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46882307/
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 17:54:18  来源:igfitidea点击:

AttributeError: module 'tensorflow' has no attribute 'feature_column'

pythonmachine-learningtensorflow

提问by Ank

So I am new to machine learning and was trying out the TensorFlow Linear Model Tutorial given here: https://www.tensorflow.org/tutorials/wide

所以我是机器学习的新手,正在尝试这里给出的 TensorFlow 线性模型教程:https: //www.tensorflow.org/tutorials/wide

I literally just downloaded their tutorial and tried to run it in my computer but I got the error:

我实际上只是下载了他们的教程并尝试在我的计算机上运行它,但出现错误:

AttributeError: module 'tensorflow' has no attribute 'feature_column'

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

I searched online and got to know that this can happen on older versions of tensorflow, but I am running the latest version: 1.3.0

我在网上搜索并知道这可能发生在旧版本的 tensorflow 上,但我运行的是最新版本:1.3.0

So why am I getting this error and how to fix it?

那么为什么我会收到此错误以及如何修复它?

回答by Max

Tensorflow 1.3 should support feature_column well. You might accidentally used an old version. Try the following code to verify your version:

Tensorflow 1.3 应该很好地支持 feature_column。您可能不小心使用了旧版本。尝试使用以下代码来验证您的版本:

import tensorflow as tf
print(tf.__version__)
print(dir(tf.feature_column))

回答by Wesam

If you're importing Tensorflow in a project that uses Keras, import Keras modules first, then Tensorflow. That solved the problem for me.

如果您在使用 Keras 的项目中导入 Tensorflow,请先导入 Keras 模块,然后再导入 Tensorflow。那为我解决了问题。

Do this: (notice the order)

这样做:(注意顺序)

from keras.backend.tensorflow_backend import set_session
from keras.models import Sequential
from keras import applications

import tensorflow as tf

Do not dothis:

不要这样做

import tensorflow as tf

from keras.backend.tensorflow_backend import set_session
from keras.models import Sequential
from keras import applications

回答by Shikhar Omar

Upgrading your tensorflow might help.

升级你的 tensorflow 可能会有所帮助。

pip install --upgrade tensorflow

回答by Manas

I faced a similar error while running a session using the Tensorflow 2.0 beta. I used the following form for running a session:

我在使用 Tensorflow 2.0 beta 运行会话时遇到了类似的错误。我使用以下表单来运行会话:

import tensorflow as tf
constant = tf.constant([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
with tf.compat.v1.Session() as sess:
        print(sess.run(constant))

instead of:

代替:

import tensorflow as tf
constant = tf.constant([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
with tf.Session() as sess:
      print(sess.run(constant))

Also,

还,

tf.compat.v1.Session()

is backward compatible. You might face a similar error when using other functions in Tensorflow 2.0 beta like print, get_variable, etc. Use a similar form as shown above in the example.

向后兼容。在 Tensorflow 2.0 beta 中使用其他函数(如 print、get_variable 等)时,您可能会遇到类似的错误。使用如上例所示的类似形式。