0

I’m learning TensorFlow 2.x and I have some code from TF 1.x:

X = tf.placeholder(tf.float32, shape=[None, 784])
Y = tf.placeholder(tf.float32, shape=[None, 10])

When I run this in TF 2.x, I get:

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

I understand that placeholders are removed in TF 2.x. I want to know the correct way to replace this code using TF 2.x tensors or Keras, keeping the same behavior for training a neural network.

I want to replace placeholders with TensorFlow 2.x style tensors so I can feed batches of data without using deprecated compat.v1 code.

5
  • 1
    Please share a sample code on how you're using placeholder. Commented Sep 14 at 10:31
  • I was using tf.placeholder like this in TensorFlow 1.x: X = tf.placeholder(tf.float32, shape=[None, 784]) Y = tf.placeholder(tf.float32, shape=[None, 10]) Then ran a session with sess.run(..., feed_dict={X: x_sample, Y: y_sample}). But in TF 2.x, I get: AttributeError: module 'tensorflow' has no attribute 'placeholder'. Commented Sep 14 at 11:06
  • 1
    In TF 2, you don't need session, it will run eagerly. My point was, if you can provide minimal code with tf.placeholder, someone may suggest you how to update it. Then you can inject the approach to your main pipeline. Commented Sep 14 at 15:20
  • This question is similar to: Replacing placeholder for tensorflow v2. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Sep 14 at 17:15
  • @A A, One of the replacement for tf.placeholder is tf.keras.Input, which explicitly defines your model's entry point, such as inputs = tf.keras.Input(shape=(784,)). You then build a model with the Keras Functional API and pass your data directly to model.fit(), which completely replaces the old feed_dict and session logic. Commented Oct 16 at 8:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.