8

So I am new to machine learning and was trying out the TensorFlow Linear Model Tutorial given here: 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'

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

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

Ank
  • 1,864
  • 4
  • 31
  • 51
  • On top of the answer by @Mingxing , check for possible presence of both `tensorflow` and `tensorflow-gpu` in your `pip list`. – desertnaut Oct 23 '17 at 11:51
  • Well yeah, I have both `tensorflow (1.3.0)` and `tensorflow-gpu (1.3.0)` What to do now? – Ank Oct 23 '17 at 13:09
  • https://stackoverflow.com/questions/46771019/tensorflow-keras-do-not-use-all-available-resources/46772433#46772433 - although, since both are v1.3.0, does not seem related to your reported issue – desertnaut Oct 23 '17 at 13:11
  • so like you suggested, I uninstalled both and then installed `tensorflow-gpu (1.3.0)` but it still didn't help. I am still getting that error – Ank Oct 23 '17 at 14:18

4 Answers4

3

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

import tensorflow as tf
print(tf.__version__)
print(dir(tf.feature_column))
Max
  • 1,014
  • 6
  • 7
  • I got `1.3.0` and `['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_allowed_symbols', 'bucketized_column', 'categorical_column_with_hash_bucket', 'categorical_column_with_identity', 'categorical_column_with_vocabulary_file', 'categorical_column_with_vocabulary_list', 'crossed_column', 'embedding_column', 'indicator_column', 'input_layer', 'linear_model', 'make_parse_example_spec', 'numeric_column', 'weighted_categorical_column']` – Ank Oct 23 '17 at 13:10
  • 1
    If I write your code in Anaconda prompt, then it works fine and prints what I posted in the above comment. But if I copy the same code in a python script and try to run it, then I get that attribute error. – Ank Oct 23 '17 at 14:26
  • What is the output of print(tf.__version__) after run your python script? – Max Oct 23 '17 at 19:46
  • I get `1.3.0` for `print(tf.__version__)` – Ank Oct 24 '17 at 05:15
  • I have 0.12.0. Is that the latest version? – codingPerson Oct 24 '18 at 19:52
2

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

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 do this:

import tensorflow as tf

from keras.backend.tensorflow_backend import set_session
from keras.models import Sequential
from keras import applications
Sam
  • 11,799
  • 9
  • 49
  • 68
2

Upgrading your tensorflow might help.

pip install --upgrade tensorflow
Shikhar Omar
  • 354
  • 2
  • 7
1

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

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.

Manas
  • 888
  • 10
  • 20