TensorFlow, “'module' object has no attrib

2020-02-02 12:00发布

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?

15条回答
戒情不戒烟
2楼-- · 2020-02-02 12:05

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
查看更多
我只想做你的唯一
3楼-- · 2020-02-02 12:05

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

查看更多
女痞
4楼-- · 2020-02-02 12:05
run the tensorflow as tf
tf.disable_v2_behavior()

use once tf.disable_v2_behavior() after that below error not come again.

AttributeError: 'module' object has no attribute 'placeholder'
查看更多
孤傲高冷的网名
5楼-- · 2020-02-02 12:09

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
查看更多
地球回转人心会变
6楼-- · 2020-02-02 12:11

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.

查看更多
劳资没心,怎么记你
7楼-- · 2020-02-02 12:14

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'

查看更多
登录 后发表回答