How to fix AttributeError: module ‘tensorflow‘ has no attribute ‘placeholder‘

Issue :

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

How to fix AttributeError: module ‘tensorflow‘ has no attribute ‘placeholder‘_第1张图片

 Original Code:

# Example of TensorFlow library
# import tensorflow as tf
import tensorflow as tf
# declare two symbolic floating-point scalars
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)

# create a simple symbolic expression using the add function
add = tf.add(a, b)

# bind 1.5 to 'a', 2.5 to 'b', and evaluate 'c'
sess = tf.Session()
binding = {a: 1.5, b: 2.5}
c = sess.run(add, feed_dict=binding)
print(c)

Solution :

The code import tensorflow as tf be insteaded

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

# Example of TensorFlow library
# import tensorflow as tf
# import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# declare two symbolic floating-point scalars
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)

# create a simple symbolic expression using the add function
add = tf.add(a, b)

# bind 1.5 to 'a', 2.5 to 'b', and evaluate 'c'
sess = tf.Session()
binding = {a: 1.5, b: 2.5}
c = sess.run(add, feed_dict=binding)
print(c)

How to fix AttributeError: module ‘tensorflow‘ has no attribute ‘placeholder‘_第2张图片

 

你可能感兴趣的:(Maxwell‘s,Bug,tensorflow,python,人工智能)