tensorflow_probability/python/mcmc/nuts.py
def generate_efficient_write_read_instruction(instruction_array):
  """Statically generate a memory efficient write/read instruction."""
  nsteps_within_tree = np.max(instruction_array) + 1
  instruction_mat = np.zeros((nsteps_within_tree, nsteps_within_tree))
  for previous_step, current_step in instruction_array:
    instruction_mat[previous_step, current_step] = 1
  instruction_mat_cumsum = np.cumsum(instruction_mat, axis=1)
  max_to_retain = instruction_mat_cumsum[:, -1][..., np.newaxis]

  # Generate a sparse matrix that represents the memory footprint:
  #   -1 : no need to save to memory (these are odd steps)
  #    1 : needed for check u turn (either already in memory or will be saved)
  #    0 : still in memory but not needed for check u turn
  instruction_mat2 = np.zeros(instruction_mat.shape)
  instruction_mat2[instruction_mat == 0] = -1
  instruction_mat2[(instruction_mat_cumsum < max_to_retain)
                   & (instruction_mat_cumsum > 0)] = 0
  instruction_mat2[instruction_mat == 1] = 1
  np.fill_diagonal(instruction_mat2, (max_to_retain > 0) - 1)
  # plt.imshow(instruction_mat2, interpolation='None')

  # Note that we only write at even step. Oberserved that this is actually
  # squence A000120 (https://oeis.org/A000120)
  write_instruction = np.sum(
      instruction_mat2 > -1, axis=0)[range(0, nsteps_within_tree, 2)] - 1
  # Note that we only read at odd step.
  read_instruction = []
  for i in range(nsteps_within_tree):
    temp_instruction = instruction_mat2[:, i]
    if np.sum(temp_instruction == 1) > 0:
      r = np.where(temp_instruction[temp_instruction >= 0] == 1)[0][0]
      read_instruction.append([r, r + np.sum(temp_instruction == 1)])
  return write_instruction, np.asarray(read_instruction)
Statically generate a memory efficient write/read instruction.
Statically generate a memory efficient write/read instruction.
Statically generate a memory efficient write / read instruction .
def generate_efficient_write_read_instruction(instruction_array):
  nsteps_within_tree = np.max(instruction_array) + 1
  instruction_mat = np.zeros((nsteps_within_tree, nsteps_within_tree))
  for previous_step, current_step in instruction_array:
    instruction_mat[previous_step, current_step] = 1
  instruction_mat_cumsum = np.cumsum(instruction_mat, axis=1)
  max_to_retain = instruction_mat_cumsum[:, -1][..., np.newaxis]
  instruction_mat2 = np.zeros(instruction_mat.shape)
  instruction_mat2[instruction_mat == 0] = -1
  instruction_mat2[(instruction_mat_cumsum < max_to_retain)
                   & (instruction_mat_cumsum > 0)] = 0
  instruction_mat2[instruction_mat == 1] = 1
  np.fill_diagonal(instruction_mat2, (max_to_retain > 0) - 1)
  write_instruction = np.sum(
      instruction_mat2 > -1, axis=0)[range(0, nsteps_within_tree, 2)] - 1
  read_instruction = []
  for i in range(nsteps_within_tree):
    temp_instruction = instruction_mat2[:, i]
    if np.sum(temp_instruction == 1) > 0:
      r = np.where(temp_instruction[temp_instruction >= 0] == 1)[0][0]
      read_instruction.append([r, r + np.sum(temp_instruction == 1)])
  return write_instruction, np.asarray(read_instruction)
def generate_efficient_write_read_instruction ( instruction_array ) : nsteps_within_tree = np . max ( instruction_array ) + 1 instruction_mat = np . zeros ( ( nsteps_within_tree , nsteps_within_tree ) ) for previous_step , current_step in instruction_array : instruction_mat [ previous_step , current_step ] = 1 instruction_mat_cumsum = np . cumsum ( instruction_mat , axis = 1 ) max_to_retain = instruction_mat_cumsum [ : , - 1 ] [ ... , np . newaxis ] instruction_mat2 = np . zeros ( instruction_mat . shape ) instruction_mat2 [ instruction_mat == 0 ] = - 1 instruction_mat2 [ ( instruction_mat_cumsum < max_to_retain ) & ( instruction_mat_cumsum > 0 ) ] = 0 instruction_mat2 [ instruction_mat == 1 ] = 1 np . fill_diagonal ( instruction_mat2 , ( max_to_retain > 0 ) - 1 ) write_instruction = np . sum ( instruction_mat2 > - 1 , axis = 0 ) [ range ( 0 , nsteps_within_tree , 2 ) ] - 1 read_instruction = [ ] for i in range ( nsteps_within_tree ) : temp_instruction = instruction_mat2 [ : , i ] if np . sum ( temp_instruction == 1 ) > 0 : r = np . where ( temp_instruction [ temp_instruction >= 0 ] == 1 ) [ 0 ] [ 0 ] read_instruction . append ( [ r , r + np . sum ( temp_instruction == 1 ) ] ) return write_instruction , np . asarray ( read_instruction )
Statically generate a memory efficient write/read instruction.
Statically generate a memory efficient write / read instruction .
"""Statically generate a memory efficient write/read instruction.""" # Generate a sparse matrix that represents the memory footprint: #   -1 : no need to save to memory (these are odd steps) #    1 : needed for check u turn (either already in memory or will be saved) #    0 : still in memory but not needed for check u turn # plt.imshow(instruction_mat2, interpolation='None') # Note that we only write at even step. Oberserved that this is actually # squence A000120 (https://oeis.org/A000120) # Note that we only read at odd step.
instruction_array