module'tensorflow'has no attribute'placeholder'

Asked 2 years ago, Updated 2 years ago, 59 views

I'm a beginner at python, but I can't get rid of the error.

The code is

import tensorflow as tf

a = tf.constant (3.0, tf.float32)
b = tf.constant (2.0, tf.float32)

x = tf.placeholder(tf.float32)

y = a * x + b

sess=tf.Session()
print(sess.run(y, {x:[0,1,2,3]}))

The error content is

module'tensorflow'has no attribute'placeholder'

I am using a mac and python is 3.7

tensorflow will be 2.0.0

python tensorflow

2022-09-29 22:31

1 Answers

Possibility 1: You are importing and executing v2 grammar code.

Workaround 1: Import v1 and disable v2.

import tensorflow.compat.v1astf
tf.disable_v2_behavior()

Workaround 2: Replace with v2 code.

import tensorflow as tf

a = tf.constant (3.0, tf.float32)
b = tf.constant (2.0, tf.float32)

def(x):
    return a*x+b

print(f([0,1,2,3]))

対策Measures 1 and 2 only apply one or the other.

Possibility 2: The file name you created is "tensorflow.py".

Workaround: Replace the filename.

 mv tensorflow.py example.py

Ref: https://stackoverflow.com/questions/37383812/tensorflow-module-object-has-no-attribute-placeholder


2022-09-29 22:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.