def as_dicts(
      self,
      matrix,
      type_name,
      simplify_unitsize_minibatch = True,
      threshold = 0.0,
  ):
    """Return dicts mapping entity-names to weights for each row of a matrix.

    Args:
      matrix: numpy ndarray of shape (num_entity_sets, entity_index) with values
        being the weights.
      type_name: string name of type associated with matrix
      simplify_unitsize_minibatch: if true and as_dicts is also true, and the
        minibatch size is 1, then just return the single row dictionary.
      threshold: only values with weights above this threshold are returned.

    Returns:
      dictionary structure or list of dictionaries.
    """
    num_entity_sets = matrix.shape[0]
    result = [{} for _ in range(num_entity_sets)]
    (row_indices, entity_indices) = np.nonzero(matrix > threshold)
    weights = matrix[(row_indices, entity_indices)].tolist()
    entities = [
        self.get_entity_name(entity_index, type_name)
        for entity_index in entity_indices
    ]
    for (row, entity, weight) in zip(row_indices, entities, weights):
      result[row][entity] = weight
    if simplify_unitsize_minibatch and len(result) == 1:
      return result[0]
    else:
      return result
Return dicts mapping entity-names to weights for each row of a matrix.

    Args:
      matrix: numpy ndarray of shape (num_entity_sets, entity_index) with values
        being the weights.
      type_name: string name of type associated with matrix
      simplify_unitsize_minibatch: if true and as_dicts is also true, and the
        minibatch size is 1, then just return the single row dictionary.
      threshold: only values with weights above this threshold are returned.

    Returns:
      dictionary structure or list of dictionaries.
    
language/nql/nql/__init__.py