11

I was able to run my python program three weeks ago but now every time I try to run it, I get the following error:

AttributeError: module 'tensorflow' has no attribute 'placeholder'

I have tensorflow installed (version '2.0.0-alpha0'). I have read a couple of posts related to this issue. They say I should uninstall TensorFlow and re-install it again. The problem is that I am running this on a cluster computer and I do not have sudo permissions.

Any idea?

2

5 Answers 5

10

In Tensorflow 2.0, there is no placeholder. You need to update your TF1.x code to TF2.0 code and then run it on your cluster. Please take a look at the official doc on converting your TF1.x code to TF2.0.

In TF1.x codes, you build tensorflow graph (static graph) with placeholders, constants, variables. Then, run the code in a session with a tf.session() command. During that session, you provide the values for the placeholder and execute the static graph.

In TF2.0, models run eagerly as you enter commands. This is more pythonic. Check more details about TF 2.0 here. Thanks!

0
10

After including the tensorflow compat v1 libraries:

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

use the v1 syntax like this:

X = tf.compat.v1.placeholder(dtype="float",shape=[None, n_H0, n_W0, n_C0])
Y = tf.compat.v1.placeholder(dtype="float",shape=[None, n_y]) 
1
  • This works, we are using version 1 tensorflow from this import right?
    – Swapnil
    Commented May 10, 2023 at 14:10
5

In addition to the @Vishnuvardhan Janapati's answer, you can update folders ("*TREE") and/or files to version 2 of TensorFlow. The upgrade tool tf_upgrade_v2 is automatically included in TensorFlow 1.13 and later.

tf_upgrade_v2 [-h] [--infile INPUT_FILE] [--outfile OUTPUT_FILE]
                   [--intree INPUT_TREE] [--outtree OUTPUT_TREE]
                   [--copyotherfiles COPY_OTHER_FILES] [--inplace]
                   [--reportfile REPORT_FILENAME] [--mode {DEFAULT,SAFETY}]
                   [--print_all]

An illustration of how the conversion fixed the "placeholder" error:

enter image description here

Note: this fixes similar complaints module 'tensorflow' has no attribute 'xxxxx' (not just the "placeholder").

0
4

Changing the library worked for me

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

If this doesn't work maybe you need you install TensorFlow again.

I hope it helps

0
3

Calling disable_v2_behavior() function is not necessary

just,

import tensorflow as tf

tf.compat.v1.placeholder()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.