0

I did not use TensorFlow for a while and now, when I was starting to use it again, I have the problem with the first basic line of my code:

X = tf.placeholder(name = 'X')

I get the following error message:

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

What I got from googling a bit, is that the placeholder method got deprecated.

So, my question is where should I start reading to figure out what was deprecated and what is the new way to use TensorFlow?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • A quick Google search points to this: https://www.tensorflow.org/guide/migrate – ForceBru Apr 11 '22 at 11:23
  • I do not want to migrate my code. – Roman Apr 11 '22 at 11:23
  • [This page](https://www.tensorflow.org/api_docs/python/tf/compat/v1/placeholder) says that one should "use `tf.keras.Input` to replace `tf.compat.v1.placeholder`", if you're looking for the "new way" of using TensorFlow – ForceBru Apr 11 '22 at 11:26
  • But this is only about placeholders. I guess there many other changes as well. Is there a systematic, comprehensive overview of all the changes? – Roman Apr 11 '22 at 11:40
  • What about [this](https://www.tensorflow.org/guide/migrate/tf1_vs_tf2) ? – Mohan Radhakrishnan Apr 11 '22 at 11:47
  • ok, this looks very relevant. Thanks. – Roman Apr 11 '22 at 11:51
  • You should check this: https://stackoverflow.com/questions/37383812/tensorflow-module-object-has-no-attribute-placeholder – coket Apr 11 '22 at 11:54

2 Answers2

0

You can still access your older TF 1.x code by selecting Tensorflow 1.x before running your code:

%tensorflow_version 1.x

import tensorflow as tf
X = tf.placeholder(tf.float32, name = 'X')
X

Output:

<tf.Tensor 'X_1:0' shape=<unknown> dtype=float32>

tf.placeholder is replaced by tf.keras.Input in tensorflow eager execution mode (later version of TensorFlow 2.0).

Please check this Migration guide for detailed understanding of converting TF 1.x code to TF 2.x.

0

If you are using scripts, Answer by @TFer2 will not work. You need to disable tfv2 behaviour, so that you can use the v1 behaviour.

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

Now you can use tf v1 things.

I would suggest seeing some examples for tfv1 vs tfv2 tasks. These will help you understanding the major differences.

For example binary classification in tf1 vs binary classification in tf2.

Ahmad Anis
  • 2,322
  • 4
  • 25
  • 54