TensorFlow, "'module' object has no attribute 'placeholder'"

user3023715 picture user3023715 · May 23, 2016 · Viewed 167.9k times · Source

I've been trying to use tensorflow for two days now installing and reinstalling it over and over again in python2.7 and 3.4. No matter what I do, I get this error message when trying to use tensorflow.placeholder()

It's very boilerplate code:

tf_in = tf.placeholder("float", [None, A]) # Features

No matter what I do I always get the trace back:

Traceback (most recent call last):
  File "/home/willim/PycharmProjects/tensorflow/tensorflow.py", line 2, in <module>
    import tensorflow as tf
  File "/home/willim/PycharmProjects/tensorflow/tensorflow.py", line 53, in <module>
    tf_in = tf.placeholder("float", [None, A]) # Features
AttributeError: 'module' object has no attribute 'placeholder'

Anyone know how I can fix this?

Answer

u2gilles picture u2gilles · Apr 8, 2019

If you have this error after an upgrade to TensorFlow 2.0, you can still use 1.X API by replacing:

import tensorflow as tf

by

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()