(probability_mass_function)
return idxs
Sample from a categorical distribution.

  Args:
    probability_mass_function: Output of a softmax over categories.
      Array of shape [batch_size, number of categories]. Rows sum to 1.

  Returns:
    idxs: Array of size [batch_size, 1]. Integer of category sampled.
Sample from a categorical distribution.
Sample from a categorical distribution .
def sample_categorical(probability_mass_function):
  """Sample from a categorical distribution.

  Args:
    probability_mass_function: Output of a softmax over categories.
      Array of shape [batch_size, number of categories]. Rows sum to 1.

  Returns:
    idxs: Array of size [batch_size, 1]. Integer of category sampled.
  """
  if probability_mass_function.ndim == 1:
    probability_mass_function = np.expand_dims(probability_mass_function, 0)
  batch_size = probability_mass_function.shape[0]
  cumulative_density_function = np.cumsum(probability_mass_function, axis=1)
  rand_vals = np.random.rand(batch_size)
  idxs = np.zeros([batch_size, 1])
  for i in range(batch_size):
    idxs[i] = cumulative_density_function[i].searchsorted(rand_vals[i])
  return idxs
def sample_categorical ( probability_mass_function ) : if probability_mass_function . ndim == 1 : probability_mass_function = np . expand_dims ( probability_mass_function , 0 ) batch_size = probability_mass_function . shape [ 0 ] cumulative_density_function = np . cumsum ( probability_mass_function , axis = 1 ) rand_vals = np . random . rand ( batch_size ) idxs = np . zeros ( [ batch_size , 1 ] ) for i in range ( batch_size ) : idxs [ i ] = cumulative_density_function [ i ] . searchsorted ( rand_vals [ i ] ) return idxs