Skip to content Skip to sidebar Skip to footer

While_loop Error In Tensorflow

I tried to use while_loop in Tensorflow, but when I try to return the target output from callable in while loop, it gives me an error because the shape is increased every time. The

Solution 1:

The tf.while_loop() function requires that the following four lists have the same length, and the same type for each element:

  • The list of arguments to the cond function (c in this case).
  • The list of arguments to the body function (b in this case).
  • The list of return values from the body function.
  • The list of loop_vars representing the loop variables.

Therefore, if your loop body has two outputs, you must add a corresponding argument to b and c, and a corresponding element to loop_vars:

c = lambda i, _: tf.less(i, 30)

defb(i, _):
  i = tf.add(i, 1)
  cond = tf.cond(tf.greater(data[i-1], tf.constant(5.)),
                 lambda: tf.constant(1.0),
                 lambda: tf.constant([0.0]))

  # NOTE: This line fails with a shape error, because the output of `cond` has# a rank of either 0 or 1, but axis may be as large as 28.
  output = tf.expand_dims(cond, axis=i-1)
  return i, output

# NOTE: Use a shapeless `tf.placeholder_with_default()` because the shape# of the output will vary from one iteration to the next.
r, out = tf.while_loop(c, b, [i, tf.placeholder_with_default(0., None)])

As noted in the comments, the body of the loop (specifically the call to tf.expand_dims()) seems to be incorrect and this program won't work as-is, but hopefully this is enough to get you started.

Post a Comment for "While_loop Error In Tensorflow"