[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

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

4.5 16 votes
Article Rating
Notify of
newest most voted
Inline Feedbacks
View all comments
5 months ago

It worked for me thanks.

1 month ago

Again got error as,

module 'tensorflow' has no attribute 'disable_v2_behaviour'