0

I am trying to get a while loop working in TensorFlow, but am struggling to understand the body parameter of tf.while_loop.

In my code, I have a neural network defined as follows:

input_placeholder = tf.placeholder(dtype=np.float32, shape=[None, 2])

hidden_layer = tf.layers.dense(inputs=input_placeholder, units=10, activation=tf.nn.relu)
prediction_op = tf.layers.dense(inputs=hidden_layer, units=2)

I then want to run this prediction operation 10 times, feeding the prediction from one iteration as the input into the next iteration. My code for this is as follows:

num_steps = 10
condition = lambda step_num, input_placeholder: tf.less(step_num, num_steps)
_, final_prediction = tf.while_loop(condition, body, [step_num, latest_prediction])

What I am confused about is what to use for the body parameter. I need the body to take the prediction of the current iteration, and set this as the input for the next iteration.

Can I just use prediction_op for body? If so, how should I tell the while loop to update the value of input_placeholder after each call of prediction_op? Or should I do this a different way?

Thanks!

1 Answer 1

1

You could have the body definition like this:

def body(step_num, latest_prediction):
    hidden_layer = tf.layers.dense(inputs=latest_prediction, units=10, activation=tf.nn.relu)
    prediction_op = tf.layers.dense(inputs=hidden_layer, units=2)
    return step_num+1, prediction_op

and you can modify the while_loop as:

step_num = 0
input_placeholder = tf.placeholder(dtype=np.float32, shape=[None, 2])
condition = lambda step_num, input_placeholder: tf.less(step_num, num_steps)
_, final_prediction = tf.while_loop(condition, body, [step_num, input_placeholder])
Sign up to request clarification or add additional context in comments.

Comments

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.