'How to switch from the old to the new version of TensorFlow?
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?
Solution 1:[1]
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
.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | TFer2 |
Solution 2 | Ahmad Anis |