tensorflow/cc/framework/while_gradients.cc
// Creates a loop that counts the number of iterations performed by the
// while loop associated with `while_ctx`. The returned output yields the
// iteration count.
Creates a loop that counts the number of iterations performed by the
while loop associated with `while_ctx`. The returned output yields the
iteration count.
Creates a loop that counts the number of iterations performed by the while loop associated with ` while_ctx ` . The returned output yields the iteration count .
Status AddForwardLoopCounter(WhileContext* while_ctx, const Scope& scope,
                             Output* count) {
  Output zero = ops::Const(scope, 0, {});
  CondGraphBuilderFn cond_fn = [while_ctx](const Scope& scope,
                                           const std::vector<Output>& inputs,
                                           Output* output) {
    *output = ToOutput(while_ctx->cond_output());
    return Status::OK();
  };
  BodyGraphBuilderFn body_fn = [](const Scope& scope,
                                  const std::vector<Output>& inputs,
                                  std::vector<Output>* outputs) {
    DCHECK_EQ(inputs.size(), 1);
    outputs->emplace_back(ops::Add(scope, inputs[0], 1));
    return scope.status();
  };
  std::vector<Output> outputs;
  TF_RETURN_IF_ERROR(BuildWhileLoop(scope, {zero}, cond_fn, body_fn,
                                    while_ctx->frame_name(), &outputs,
                                     false));
  *count = outputs[0];
  return Status::OK();
}
Status AddForwardLoopCounter ( WhileContext * while_ctx , const Scope & scope , Output * count ) { Output zero = ops :: Const ( scope , 0 , { } ) ; CondGraphBuilderFn cond_fn = [ while_ctx ] ( const Scope & scope , const std :: vector < Output > & inputs , Output * output ) { * output = ToOutput ( while_ctx -> cond_output ( ) ) ; return Status :: OK ( ) ; } ; BodyGraphBuilderFn body_fn = [ ] ( const Scope & scope , const std :: vector < Output > & inputs , std :: vector < Output > * outputs ) { DCHECK_EQ ( inputs . size ( ) , 1 ) ; outputs -> emplace_back ( ops :: Add ( scope , inputs [ 0 ] , 1 ) ) ; return scope . status ( ) ; } ; std :: vector < Output > outputs ; TF_RETURN_IF_ERROR ( BuildWhileLoop ( scope , { zero } , cond_fn , body_fn , while_ctx -> frame_name ( ) , & outputs , false ) ) ; * count = outputs [ 0 ] ; return Status :: OK ( ) ; }
Creates a loop that counts the number of iterations performed by the
while loop associated with `while_ctx`.
Creates a loop that counts the number of iterations performed by the while loop associated with ` while_ctx ` .
// Create while loop: //   i = 0 //   while forward loop predicate is true: //     ++i // Condition function that returns condition output from original while loop. // Body function that adds one to input. // Note that this loop runs in the same execution frame as the forward loop. /* create_while_ctx */