How do I migrate feed_dict and placeholder to TensorFlow 2?

Viewed 464

I'm trying to convert a simple snippet from TensorFlow 1.x to TensorFlow 2:

# ########## TensorFlow 1.x code: ##########
import numpy as np
import tensorflow as tf

coefficients = np.array([[1.], [-10.], [25.]])
w = tf.Variable(0, dtype=tf.float32)
x = tf.placeholder(tf.float32, [3, 1])
cost = (x[0][0] * (w**2)) + (x[1][0]*w) + (x[2][0])
train = tf.train.GradientDescentOptimizer(0.05).minimize(cost)

if __name__ == '__main__':
    init = tf.global_variables_initializer()
    session = tf.Session()
    session.run(init)

    for i in range(100):
        session.run(train, feed_dict={x: coefficients})

    print(session.run(w))

Much of the old API has been replaced in TF2 (e.g. GradientDescentOptimizer replaced with keras.optimizers.SGD), and I was able to figure out how to refactor most of my code, but I don't know how to refactor tf.placeholder and feed_dict and how those two interact. Is the use of placeholders simply eschewed in TF2?

1 Answers

Typically you use @tf.function and turn placeholders into function arguments. sess.run then is replaced with calling that function. Things that used to return ops (like minimize) are now just called inside the function. Here's your snippet converted:

coefficients = tf.constant([[1.], [-10.], [25.]])
w = tf.Variable(0.0)

@tf.function
def train(x):
  cost = (x[0][0] * (w**2)) + (x[1][0]*w) + (x[2][0])
  tf.compat.v1.train.GradientDescentOptimizer(0.05).minimize(cost, var_list=[w])

for i in range(100):
    train(coefficients)

print(w)

As you mentioned, train.GradientDescentOptimizer is deprecated, so upgrading that part will need a few more changes.

Related