TensorFlow, “'module' object has no attribute 'placeholder'”

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

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?

回答1:

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.



回答2:

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
  2. re-installed tensorflow. Using conda install tensorflow


回答3:

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!