127

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?

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
user3023715
  • 1,539
  • 2
  • 11
  • 12

23 Answers23

187

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()
u2gilles
  • 6,888
  • 7
  • 51
  • 75
  • 7
    thats exactly what i did and i am getting this error – Xitcod13 May 20 '19 at 04:13
  • 12
    That worked for me for 'placeholder', but failed once I got to 'contrib' in the tutorial I was trying to follow. Guess I need to try to find a tutorial that uses v2. – Joe Dec 05 '19 at 13:19
  • Didn't work! Perhaps it works only for `tensorflow`? not for `tensorflow-gpu`? – Scott Oct 23 '20 at 03:04
  • 1
    This should be marked as accepted solution – Ashwin Balani Mar 17 '21 at 18:23
  • @Joe for `contrib` you should downgrade to tf1 or instead of contrib and downgrading to tf1, searching for similar named function in tf2's compat v1 and changing function is better. – Celuk Aug 10 '22 at 14:27
  • https://www.datasciencelearner.com/attributeerror-module-tensorflow-has-no-attribute-placeholder/ – GlamSelva1297934 Jun 10 '23 at 06:20
43

Solution: Do not use "tensorflow" as your filename.

Notice that you use tensorflow.py as your filename. And I guess you write code like:

import tensorflow as tf

Then you are actually importing the script file "tensorflow.py" that is under your current working directory, rather than the "real" tensorflow module from Google.

Here is the order in which a module will be searched when importing:

  1. The directory containing the input script (or the current directory when no file is specified).

  2. PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).

  3. The installation-dependent default.

Lifu Huang
  • 11,930
  • 14
  • 55
  • 77
35

It happened to me too. I had tensorflow and it was working pretty well, but when I install tensorflow-gpu along side the previous tensorflow this error arose then I did these 3 steps and it started working with no problem:

  1. I removed tensorflow-gpu, tensorflow, tensorflow-base packages from Anaconda. Using.

conda remove tensorflow-gpu tensorflow tensorflow-base

  1. re-installed tensorflow. Using

conda install tensorflow

Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43
farnaz jazayeri
  • 625
  • 6
  • 7
  • 4
    awesome man! solved my problem!! didn't expect that it's caused by conda – Ferico Samuel May 01 '18 at 03:43
  • 3
    Great! It was helpful – ElvinM Oct 09 '18 at 05:45
  • Thank you so much, but i am not able to use GPU even after installed "Tensorflow-gpu 1.2.1-Py36cuda8.0cudnn6.0_0". keep on executing with CPU not with GPU. If i specify with tf.device('/gpu:0') then i got exception saying you have only CPU device. – user1531248 Nov 09 '18 at 18:53
  • 5
    I'm pretty sure the last step should be conda install tensorflow-gpu, otherwise you are stuck with cpu (only mention cause the comment before me). – user3023715 Sep 16 '19 at 12:45
32

Instead of tf.placeholder(shape=[None, 2], dtype=tf.float32) use something like tf.compat.v1.placeholder(shape=[None, 2], dtype=tf.float32) if you don't want to disable v2 completely.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
kanishka vatsa
  • 2,074
  • 18
  • 8
23
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior() 

works. I am using Python 3.7 and tensorflow 2.0.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Sooraj S
  • 399
  • 4
  • 5
12

It appears that .placeholder() , .reset_default_graph() , and others were removed with version 2. I ran into this issue using Docker image: tensorflow/tensorflow:latest-gpu-py3 which automatically pulls the latest version. I was working in 1.13.1 and was 'upgraded to 2' automatically and started getting the error messages. I fixed this by being more specific with my image: tensorflow/tensorflow:1.13.1-gpu-py3.

More info can be found here: https://www.tensorflow.org/alpha/guide/effective_tf2

Adrian Hood Sr
  • 405
  • 5
  • 18
10

Avoid using the below striked out statement in tensorflow=2.0

i̶m̶p̶o̶r̶t̶ ̶t̶e̶n̶s̶o̶r̶f̶l̶o̶w̶ ̶a̶s̶ ̶t̶f̶ ̶x̶ ̶=̶ ̶t̶f̶.̶p̶l̶a̶c̶e̶h̶o̶l̶d̶e̶r̶(̶s̶h̶a̶p̶e̶=̶[̶N̶o̶n̶e̶,̶ ̶2̶]̶,̶ ̶d̶t̶y̶p̶e̶=̶t̶f̶.̶f̶l̶o̶a̶t̶3̶2̶)̶

You can disable the v2 behavior by using the following code

This one is perfectly working for me.

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
x = tf.placeholder(shape=[None, 2], dtype=tf.float32)

8

I also got the same error. May be because of the version of tensorflow. After installing tensorflow 1.4.0, I got relief from the error.

pip install tensorflow==1.4.0
mahbubcseju
  • 2,200
  • 2
  • 16
  • 21
5

If you are using TensorFlow 2.0, then some code developed for tf 1.x may not code work. Either you can follow the link : https://www.tensorflow.org/guide/migrate

or you can install a previous version of tf by pip3 install tensorflow==version

5

Please take a look at the Migrate your TensorFlow 1 code to TensorFlow 2.

These codes:

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

need to be migrated in TensorFlow 2 as below:

import tensorflow as tf
import tensorflow.compat.v1 as v1
tf_in = vi.placeholder("float", [None, A]) # Features
Dharman
  • 30,962
  • 25
  • 85
  • 135
eQ19
  • 9,880
  • 3
  • 65
  • 77
3

Import the old version of tensorflow instead of the new version

[https://inneka.com/ml/tf/tensorflow-module-object-has-no-attribute-placeholder/][1]

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

Pradeep
  • 31
  • 2
3

You need to use the keras model with tensorflow 2, as here

import tensorflow as tf
from tensorflow.python.keras.layers import  Input, Embedding, Dot, Reshape, Dense
from tensorflow.python.keras.models import Model
Shaina Raza
  • 1,474
  • 17
  • 12
2

Recent version 2.0 does not support placeholder. I uninstalled 2.0 using command: conda remove tensorflow. then I installed 1.15.0 using command: conda install -c conda-forge tensorflow=1.15.0. 1.15 is latest in version 1 series. You can change as per you wish and requirement. For seeing all version, use command: conda search tensorflow. It worked for Anaconda3 in Windows.

Nawin K Sharma
  • 704
  • 2
  • 6
  • 14
2

Try this:

pip install tensorflow==1.14

or this (if you have GPU):

pip install tensorflow-gpu==1.14
Scott
  • 4,974
  • 6
  • 35
  • 62
1

Faced same issue on Ubuntu 16LTS when tensor flow was installed over existing python installation.

Workaround: 1.)Uninstall tensorflow from pip and pip3

sudo pip uninstall tensorflow
sudo pip3 uninstall tensorflow

2.)Uninstall python & python3

sudo apt-get remove python-dev python3-dev python-pip python3-pip

3.)Install only a single version of python(I used python 3)

sudo apt-get install python3-dev python3-pip

4.)Install tensorflow to python3

sudo pip3 install --upgrade pip

for non GPU tensorflow, run this command

sudo pip3 install --upgrade tensorflow

for GPU tensorflow, run below command

sudo pip3 install --upgrade tensorflow-gpu

Suggest not to install GPU and vanilla version of tensorflow

1

If you get this on tensorflow 2.0.0+, it's very likely because the code isn't compatible with the newer version of tensorflow.

To fix this, run the tf_upgrade_v2 script.

tf_upgrade_v2 --infile=YOUR_SCRIPT.py --outfile=YOUR_SCRIPT.py
E. Sun
  • 1,093
  • 9
  • 15
1

The error shows up because we are using tensorflow version 2 and the command is from version 1. So if we use:

tf.compat.v1.summary.(method_name)

It'll work

Scott
  • 4,974
  • 6
  • 35
  • 62
kamma rahul
  • 137
  • 2
  • 3
0

Because you cant use placeholder in tensflow2.0version, so you need to use tensflow1*, or you need to change your code to fix tensflow2.0

M什么名字
  • 21
  • 1
  • 2
0

I had the same problem before after tried to upgrade tensorflow, I solved it by reinstalling Tensorflow and Keras.

pip uninstall tensorflow

pip uninstall keras

Then:

pip install tensorflow

pip install keras

gg1
  • 116
  • 10
0

There have been many suggestions and some of them did work. But I would rather convert v1 tf project to v2 project for better maintenance. I am surprised no one suggested it here:

$tf_upgrade_v2 \
--intree my_project/ \
--outtree my_project_v2/ \
--reportfile report.txt 

I copied pasted from this stack overflow answer: AttributeError: module 'tensorflow' has no attribute 'get_variable'

You do need to inspect the report.txt and make sure it does what you would expect.

us_david
  • 4,431
  • 35
  • 29
-1

The problem is with TensorFlow version; the one you are running is 2.0 or something above 1.5, while placeholder can only work with 1.4.

So simply uninstall TensorFlow, then install it again with version 1.4 and everything will work.

Pedram Parsian
  • 3,750
  • 3
  • 19
  • 34
Ahmed Dimah
  • 19
  • 1
  • 1
  • 4
-1

It looks like you're trying to use TensorFlow 2.x syntax with TensorFlow 1.x. The error message you're encountering indicates that TensorFlow 2.13 doesn't have a 'placeholder' attribute.

In TensorFlow 2.x, you should use the tf.Variable or tf.constant for creating tensors with specific values, and placeholders are not used anymore. Instead, you can use regular Python variables.

Here's how you can modify your code to work with TensorFlow 2.x:

import tensorflow as tf
tf_in = tf.Variable( [None, A], dtype=tf.float32)
GuChil
  • 137
  • 1
  • 6
-4

It may be the typo if you incorrectly wrote the placeholder word. In my case I misspelled it as placehoder and got the error like this: AttributeError: 'module' object has no attribute 'placehoder'

prosti
  • 42,291
  • 14
  • 186
  • 151