[SOLVED] – AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’

[SOLVED] – AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’

The cause of the mentioned problem is incompatibile code with installed tensorflow library. In this case you have code compatible with tensorflow 1.0 version but installed tensorflow 2.0 or higher. Let’s see what you can do to solve the problem.

Solution 1. Follow tensorflow migration guide

Migrate your code following this guide. Solution for the title problem is to use variables instead of placeholders. Let’s see the following example:

#tensorflow 1.x
self._states = tf.placeholder(shape=[None, self._num_states], dtype=tf.float32)

#tensorflow 2.x
self._states = tf.Variable(tf.ones(shape=[None, self._num_states]), dtype=tf.float32)

Solution 2. Use tensorflow 1.x compatibility mode

The second approach is to use tensorflow v1 copatiblity mode. To dot his you have to use import tensorflow.compat.v1 as tf instead of import tensorflow as tf and add tf.disable_v2_behavior().

import tensorflow as tf

x = tf.placeholder(shape=[None, 2], dtype=tf.float32)
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
x = tf.placeholder(shape=[None, 2], dtype=tf.float32)
At the end… May I ask you for something?

If I helped you solve your problem, please share this post. Thanks to this, I will have the opportunity to reach a wider group of readers. Thank You

5 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments