How to replace tf.placeholder in eager execution

Question:

I am using a tf1 code as a reference for developing a model. In the reference code, they used tf.placeholder to give input to the model and the code is

class model(object):
    def __init__(self, lstm_size, batch_len, output_nodes, keep_prob, learning_rate=0.001):
        self.inputs_ = tf.compat.v1.placeholder(tf.float32, shape=[batch_len, None, 512], name='lstm_inputs')
        self.targets_ = tf.compat.v1.placeholder(tf.int32, [batch_len], name='lstm_targets')

The execution mode of this model is Graph Execution(tf.Graph).

Since I want to convert and run the code in eager execution. But, the tf.placeholder will not work in eager execution. How can I replace the tf.placeholder without affecting the input name , dtype, and shape of the input value for sake of eager execution.!

Again I need it to convert for eager execution.

Asked By: shrivas sp

||

Answers:

You can use tf.keras.Input() in the place of tf.compat.v1.placeholder() to run the code in eager execution mode.

Also, please check this migration guide to convert TensorFlow 1.x code to TensorFlow 2.x.

Answered By: TFer2