tensorflow_probability/python/mcmc/nuts.py
def has_not_u_turn_at_odd_step(read_indexes, direction, momentum_state_memory,
                               momentum_right, state_right,
                               no_u_turns_within_tree, log_prob_rank):
  """Check u turn for early stopping."""

  def _get_left_state_and_check_u_turn(left_current_index, no_u_turns_last):
    """Check U turn on a single index."""
    momentum_left = [
        tf.gather(x, left_current_index, axis=0)
        for x in momentum_state_memory.momentum_swap
    ]
    state_left = [
        tf.gather(x, left_current_index, axis=0)
        for x in momentum_state_memory.state_swap
    ]
    state_diff = [s1 - s2 for s1, s2 in zip(state_right, state_left)]
    if not GENERALIZED_UTURN:
      state_diff = [tf.where(d, m, -m) for d, m in zip(direction, state_diff)]

    no_u_turns_current = has_not_u_turn(
        state_diff,
        momentum_left,
        momentum_right,
        log_prob_rank)
    return left_current_index + 1, no_u_turns_current & no_u_turns_last

  _, no_u_turns_within_tree = tf.while_loop(
      cond=lambda i, no_u_turn: i < tf.gather(read_indexes, 1),
      body=_get_left_state_and_check_u_turn,
      loop_vars=(tf.gather(read_indexes, 0), no_u_turns_within_tree),
      parallel_iterations=TF_WHILE_PARALLEL_ITERATIONS)
  return no_u_turns_within_tree
Check u turn for early stopping.
Check u turn for early stopping.
Check u turn for early stopping .
def has_not_u_turn_at_odd_step(read_indexes, direction, momentum_state_memory,
                               momentum_right, state_right,
                               no_u_turns_within_tree, log_prob_rank):
  def _get_left_state_and_check_u_turn(left_current_index, no_u_turns_last):
    momentum_left = [
        tf.gather(x, left_current_index, axis=0)
        for x in momentum_state_memory.momentum_swap
    ]
    state_left = [
        tf.gather(x, left_current_index, axis=0)
        for x in momentum_state_memory.state_swap
    ]
    state_diff = [s1 - s2 for s1, s2 in zip(state_right, state_left)]
    if not GENERALIZED_UTURN:
      state_diff = [tf.where(d, m, -m) for d, m in zip(direction, state_diff)]
    no_u_turns_current = has_not_u_turn(
        state_diff,
        momentum_left,
        momentum_right,
        log_prob_rank)
    return left_current_index + 1, no_u_turns_current & no_u_turns_last
  _, no_u_turns_within_tree = tf.while_loop(
      cond=lambda i, no_u_turn: i < tf.gather(read_indexes, 1),
      body=_get_left_state_and_check_u_turn,
      loop_vars=(tf.gather(read_indexes, 0), no_u_turns_within_tree),
      parallel_iterations=TF_WHILE_PARALLEL_ITERATIONS)
  return no_u_turns_within_tree
def has_not_u_turn_at_odd_step ( read_indexes , direction , momentum_state_memory , momentum_right , state_right , no_u_turns_within_tree , log_prob_rank ) : def _get_left_state_and_check_u_turn ( left_current_index , no_u_turns_last ) : """Check U turn on a single index.""" momentum_left = [ tf . gather ( x , left_current_index , axis = 0 ) for x in momentum_state_memory . momentum_swap ] state_left = [ tf . gather ( x , left_current_index , axis = 0 ) for x in momentum_state_memory . state_swap ] state_diff = [ s1 - s2 for s1 , s2 in zip ( state_right , state_left ) ] if not GENERALIZED_UTURN : state_diff = [ tf . where ( d , m , - m ) for d , m in zip ( direction , state_diff ) ] no_u_turns_current = has_not_u_turn ( state_diff , momentum_left , momentum_right , log_prob_rank ) return left_current_index + 1 , no_u_turns_current & no_u_turns_last _ , no_u_turns_within_tree = tf . while_loop ( cond = lambda i , no_u_turn : i < tf . gather ( read_indexes , 1 ) , body = _get_left_state_and_check_u_turn , loop_vars = ( tf . gather ( read_indexes , 0 ) , no_u_turns_within_tree ) , parallel_iterations = TF_WHILE_PARALLEL_ITERATIONS ) return no_u_turns_within_tree
Check u turn for early stopping.
Check u turn for early stopping .
"""Check u turn for early stopping.""" """Check U turn on a single index."""
read_indexes
direction
momentum_state_memory
momentum_right
state_right
no_u_turns_within_tree
log_prob_rank