Sort Tensorflow Hashtable By Value 2

Find the data you need here

We provide programming data of 20 most popular languages, hope to help you!

Previous PostNext Post

Sort Tensorflow HashTable by value

Find centralized, trusted content and collaborate around the technologies you use most. Learn more

h_table = tf.lookup.StaticHashTable(
      initializer=tf.lookup.KeyValueTensorInitializer(
          keys=[0, 1, 2, 3, 4, 5],
          values=[12.3, 11.1, 51.5, 34.3, 87.3, 57.8]
      ),
      default_value=tf.constant(-1),
      name="h_table"
    )
h_table = { 0: 12.3, 1: 11.1, 2: 57.8, 3: 34.3, 4: 87.3, 5: 57.8}
h_sorted = dict(sorted(h_table.items(), key=lambda x: x[1], reverse=True))
import tensorflow as tf

h_table = tf.lookup.StaticHashTable(
      initializer=tf.lookup.KeyValueTensorInitializer(
          keys=[0, 1, 2, 3, 4, 5],
          values=[12.3, 11.1, 51.5, 34.3, 87.3, 57.8]
      ),
      default_value=tf.constant(-1.),
      name="h_table"
    )

keys = h_table._initializer._keys
values = h_table._initializer._values

value_indices = tf.argsort(tf.reverse(values, axis=[0]), -1)
keys = tf.gather(keys, value_indices)

new_h_table = tf.lookup.StaticHashTable(
      initializer=tf.lookup.KeyValueTensorInitializer(
          keys=keys,
          values=h_table.lookup(keys)
      ),
      default_value=tf.constant(-1.),
      name="new_h_table"
    )

print(new_h_table._initializer._keys)
print(new_h_table._initializer._values)
tf.Tensor([4 5 2 3 0 1], shape=(6,), dtype=int32)
tf.Tensor([87.3 57.8 51.5 34.3 12.3 11.1], shape=(6,), dtype=float32)

How to Sort Hashtable in Java?

2 second. 3 third. Approach 1: TreeMap stores the data in sorted order. We can use the TreeMap constructor and convert the Hashtable object into a TreeMap object. Now the resultant TreeMap object is in sorted order. Syntax: TreeMap<K, V> tm = new TreeMap<K, V>(Map m); Parameters: m is the Hashtable in our program. Example

3 orange
2 mango
1 apple
TreeMap<K, V> tm = new TreeMap<K, V>(Map m);
1 apple
2 mango
3 orange
LinkedHashMap<K, V> lhm = new LinkedHashMap<K, V>();
2 mango
3 orange
1 apple

Sorting a Hashmap according to values

Solution: The idea is to store the entry set in a list and sort the list on the basis of values. Then fetch values and keys from the list and put them in a new hashmap. Thus, a new hashmap is sorted according to values. Key = Operating System, Value = 79 Key = Networking, Value = 80 Key = Data Structure, Value = 85 Key = Database, Value = 91

Input : Key = Math, Value = 98
        Key = Data Structure, Value = 85
        Key = Database, Value = 91
        Key = Java, Value = 95
        Key = Operating System, Value = 79
        Key = Networking, Value = 80

Output : Key = Operating System, Value = 79
         Key = Networking, Value = 80
         Key = Data Structure, Value = 85
         Key = Database, Value = 91
         Key = Java, Value = 95
         Key = Math, Value = 98
Key = Operating System, Value = 79
Key = Networking, Value = 80
Key = Data Structure, Value = 85
Key = Database, Value = 91
Key = Java, Value = 95
Key = Math, Value = 98
Key = Operating System, Value = 79
Key = Networking, Value = 80
Key = Data Structure, Value = 85
Key = Database, Value = 91
Key = Java, Value = 95
Key = Math, Value = 98
Key = Operating System, Value = 79
Key = Networking, Value = 80
Key = Data Structure, Value = 85
Key = Database, Value = 91
Key = Java, Value = 95
Key = Math, Value = 98

Tensorflow.contrib.lookup.HashTable

Here are the examples of the python api tensorflow.contrib.lookup.HashTable taken from open source projects. By voting up you can indicate which examples are most useful and appropriate. By voting up you can indicate which examples are most useful and appropriate.

  def test_make_initializable_iterator_with_hashTable(self):

    def graph_fn():
      keys = [1, 0, -1]
      dataset = tf.data.Dataset.from_tensor_slices([[1, 2, -1, 5]])
      try:
        # Dynamically try to load the tf v2 lookup, falling back to contrib
        lookup = tf.compat.v2.lookup
        hash_table_class = tf.compat.v2.lookup.StaticHashTable
      except AttributeError:
        lookup = contrib_lookup
        hash_table_class = contrib_lookup.HashTable
      table = hash_table_class(
          initializer=lookup.KeyValueTensorInitializer(
              keys=keys, values=list(reversed(keys))),
          default_value=100)
      dataset = dataset.map(table.lookup)
      return dataset_builder.make_initializable_iterator(dataset).get_next()

    result = self.execute(graph_fn, [])
    self.assertAllEqual(result, [-1, 100, 1, 100])

  def test_read_dataset_sample_from_datasets_weights_equal_weight(self):
  def __init__(self,
               tensor_key,
               label_map_proto_file,
               shape_keys=None,
               shape=None,
               default_value=''):
    """Initializes the LookupTensor handler.

    Simply calls a vocabulary (most often, a label mapping) lookup.

    Args:
      tensor_key: the name of the `TFExample` feature to read the tensor from.
      label_map_proto_file: File path to a text format LabelMapProto message
        mapping class text to id.
      shape_keys: Optional name or list of names of the TF-Example feature in
        which the tensor shape is stored. If a list, then each corresponds to
        one dimension of the shape.
      shape: Optional output shape of the `Tensor`. If provided, the `Tensor` is
        reshaped accordingly.
      default_value: The value used when the `tensor_key` is not found in a
        particular `TFExample`.

    Raises:
      ValueError: if both `shape_keys` and `shape` are specified.
    """
    name_to_id = label_map_util.get_label_map_dict(
        label_map_proto_file, use_display_name=False)
    # We use a default_value of -1, but we expect all labels to be contained
    # in the label map.
    try:
      # Dynamically try to load the tf v2 lookup, falling back to contrib
      lookup = tf.compat.v2.lookup
      hash_table_class = tf.compat.v2.lookup.StaticHashTable
    except AttributeError:
      lookup = contrib_lookup
      hash_table_class = contrib_lookup.HashTable
    name_to_id_table = hash_table_class(
        initializer=lookup.KeyValueTensorInitializer(
            keys=tf.constant(list(name_to_id.keys())),
            values=tf.constant(list(name_to_id.values()), dtype=tf.int64)),
        default_value=-1)
    display_name_to_id = label_map_util.get_label_map_dict(
        label_map_proto_file, use_display_name=True)
    # We use a default_value of -1, but we expect all labels to be contained
    # in the label map.
    display_name_to_id_table = hash_table_class(
        initializer=lookup.KeyValueTensorInitializer(
            keys=tf.constant(list(display_name_to_id.keys())),
            values=tf.constant(
                list(display_name_to_id.values()), dtype=tf.int64)),
        default_value=-1)

    self._name_to_id_table = name_to_id_table
    self._display_name_to_id_table = display_name_to_id_table
    super(_ClassTensorHandler, self).__init__(tensor_key, shape_keys, shape,
                                              default_value)

  def tensors_to_item(self, keys_to_tensors):
  def __init__(self,
               load_instance_masks=False,
               instance_mask_type=input_reader_pb2.NUMERICAL_MASKS,
               label_map_proto_file=None,
               use_display_name=False,
               dct_method='',
               num_keypoints=0,
               num_additional_channels=0,
               load_multiclass_scores=False,
               load_context_features=False,
               expand_hierarchy_labels=False,
               load_dense_pose=False,
               load_track_id=False,
               load_keypoint_depth_features=False,
               use_keypoint_label_map=False):
    """Constructor sets keys_to_features and items_to_handlers.

    Args:
      load_instance_masks: whether or not to load and handle instance masks.
      instance_mask_type: type of instance masks. Options are provided in
        input_reader.proto. This is only used if `load_instance_masks` is True.
      label_map_proto_file: a file path to a
        object_detection.protos.StringIntLabelMap proto. If provided, then the
        mapped IDs of 'image/object/class/text' will take precedence over the
        existing 'image/object/class/label' ID.  Also, if provided, it is
        assumed that 'image/object/class/text' will be in the data.
      use_display_name: whether or not to use the `display_name` for label
        mapping (instead of `name`).  Only used if label_map_proto_file is
        provided.
      dct_method: An optional string. Defaults to None. It only takes
        effect when image format is jpeg, used to specify a hint about the
        algorithm used for jpeg decompression. Currently valid values
        are ['INTEGER_FAST', 'INTEGER_ACCURATE']. The hint may be ignored, for
        example, the jpeg library does not have that specific option.
      num_keypoints: the number of keypoints per object.
      num_additional_channels: how many additional channels to use.
      load_multiclass_scores: Whether to load multiclass scores associated with
        boxes.
      load_context_features: Whether to load information from context_features,
        to provide additional context to a detection model for training and/or
        inference.
      expand_hierarchy_labels: Expands the object and image labels taking into
        account the provided hierarchy in the label_map_proto_file. For positive
        classes, the labels are extended to ancestor. For negative classes,
        the labels are expanded to descendants.
      load_dense_pose: Whether to load DensePose annotations.
      load_track_id: Whether to load tracking annotations.
      load_keypoint_depth_features: Whether to load the keypoint depth features
        including keypoint relative depths and weights. If this field is set to
        True but no keypoint depth features are in the input tf.Example, then
        default values will be populated.
      use_keypoint_label_map: If set to True, the 'image/object/keypoint/text'
        field will be used to map the keypoint coordinates (using the label
        map defined in label_map_proto_file) instead of assuming the ordering
        in the tf.Example feature. This is useful when training with multiple
        datasets while each of them contains different subset of keypoint
        annotations.

    Raises:
      ValueError: If `instance_mask_type` option is not one of
        input_reader_pb2.DEFAULT, input_reader_pb2.NUMERICAL, or
        input_reader_pb2.PNG_MASKS.
      ValueError: If `expand_labels_hierarchy` is True, but the
        `label_map_proto_file` is not provided.
    """

    # TODO(rathodv): delete unused `use_display_name` argument once we change
    # other decoders to handle label maps similarly.
    del use_display_name
    self.keys_to_features = {
        'image/encoded':
            tf.FixedLenFeature((), tf.string, default_value=''),
        'image/format':
            tf.FixedLenFeature((), tf.string, default_value='jpeg'),
        'image/filename':
            tf.FixedLenFeature((), tf.string, default_value=''),
        'image/key/sha256':
            tf.FixedLenFeature((), tf.string, default_value=''),
        'image/source_id':
            tf.FixedLenFeature((), tf.string, default_value=''),
        'image/height':
            tf.FixedLenFeature((), tf.int64, default_value=1),
        'image/width':
            tf.FixedLenFeature((), tf.int64, default_value=1),
        # Image-level labels.
        'image/class/text':
            tf.VarLenFeature(tf.string),
        'image/class/label':
            tf.VarLenFeature(tf.int64),
        'image/neg_category_ids':
            tf.VarLenFeature(tf.int64),
        'image/not_exhaustive_category_ids':
            tf.VarLenFeature(tf.int64),
        'image/class/confidence':
            tf.VarLenFeature(tf.float32),
        # Object boxes and classes.
        'image/object/bbox/xmin':
            tf.VarLenFeature(tf.float32),
        'image/object/bbox/xmax':
            tf.VarLenFeature(tf.float32),
        'image/object/bbox/ymin':
            tf.VarLenFeature(tf.float32),
        'image/object/bbox/ymax':
            tf.VarLenFeature(tf.float32),
        'image/object/class/label':
            tf.VarLenFeature(tf.int64),
        'image/object/class/text':
            tf.VarLenFeature(tf.string),
        'image/object/area':
            tf.VarLenFeature(tf.float32),
        'image/object/is_crowd':
            tf.VarLenFeature(tf.int64),
        'image/object/difficult':
            tf.VarLenFeature(tf.int64),
        'image/object/group_of':
            tf.VarLenFeature(tf.int64),
        'image/object/weight':
            tf.VarLenFeature(tf.float32),

    }
    # We are checking `dct_method` instead of passing it directly in order to
    # ensure TF version 1.6 compatibility.
    if dct_method:
      image = slim_example_decoder.Image(
          image_key='image/encoded',
          format_key='image/format',
          channels=3,
          dct_method=dct_method)
      additional_channel_image = slim_example_decoder.Image(
          image_key='image/additional_channels/encoded',
          format_key='image/format',
          channels=1,
          repeated=True,
          dct_method=dct_method)
    else:
      image = slim_example_decoder.Image(
          image_key='image/encoded', format_key='image/format', channels=3)
      additional_channel_image = slim_example_decoder.Image(
          image_key='image/additional_channels/encoded',
          format_key='image/format',
          channels=1,
          repeated=True)
    self.items_to_handlers = {
        fields.InputDataFields.image:
            image,
        fields.InputDataFields.source_id: (
            slim_example_decoder.Tensor('image/source_id')),
        fields.InputDataFields.key: (
            slim_example_decoder.Tensor('image/key/sha256')),
        fields.InputDataFields.filename: (
            slim_example_decoder.Tensor('image/filename')),
        # Image-level labels.
        fields.InputDataFields.groundtruth_image_confidences: (
            slim_example_decoder.Tensor('image/class/confidence')),
        fields.InputDataFields.groundtruth_verified_neg_classes: (
            slim_example_decoder.Tensor('image/neg_category_ids')),
        fields.InputDataFields.groundtruth_not_exhaustive_classes: (
            slim_example_decoder.Tensor('image/not_exhaustive_category_ids')),
        # Object boxes and classes.
        fields.InputDataFields.groundtruth_boxes: (
            slim_example_decoder.BoundingBox(['ymin', 'xmin', 'ymax', 'xmax'],
                                             'image/object/bbox/')),
        fields.InputDataFields.groundtruth_area:
            slim_example_decoder.Tensor('image/object/area'),
        fields.InputDataFields.groundtruth_is_crowd: (
            slim_example_decoder.Tensor('image/object/is_crowd')),
        fields.InputDataFields.groundtruth_difficult: (
            slim_example_decoder.Tensor('image/object/difficult')),
        fields.InputDataFields.groundtruth_group_of: (
            slim_example_decoder.Tensor('image/object/group_of')),
        fields.InputDataFields.groundtruth_weights: (
            slim_example_decoder.Tensor('image/object/weight')),

    }

    self._keypoint_label_map = None
    if use_keypoint_label_map:
      assert label_map_proto_file is not None
      self._keypoint_label_map = label_map_util.get_keypoint_label_map_dict(
          label_map_proto_file)
      # We use a default_value of -1, but we expect all labels to be
      # contained in the label map.
      try:
        # Dynamically try to load the tf v2 lookup, falling back to contrib
        lookup = tf.compat.v2.lookup
        hash_table_class = tf.compat.v2.lookup.StaticHashTable
      except AttributeError:
        lookup = contrib_lookup
        hash_table_class = contrib_lookup.HashTable
      self._kpts_name_to_id_table = hash_table_class(
          initializer=lookup.KeyValueTensorInitializer(
              keys=tf.constant(list(self._keypoint_label_map.keys())),
              values=tf.constant(
                  list(self._keypoint_label_map.values()), dtype=tf.int64)),
          default_value=-1)

      self.keys_to_features[_KEYPOINT_TEXT_FIELD] = tf.VarLenFeature(
          tf.string)
      self.items_to_handlers[_KEYPOINT_TEXT_FIELD] = (
          slim_example_decoder.ItemHandlerCallback(
              [_KEYPOINT_TEXT_FIELD], self._keypoint_text_handle))

    if load_multiclass_scores:
      self.keys_to_features[
          'image/object/class/multiclass_scores'] = tf.VarLenFeature(tf.float32)
      self.items_to_handlers[fields.InputDataFields.multiclass_scores] = (
          slim_example_decoder.Tensor('image/object/class/multiclass_scores'))

    if load_context_features:
      self.keys_to_features[
          'image/context_features'] = tf.VarLenFeature(tf.float32)
      self.items_to_handlers[fields.InputDataFields.context_features] = (
          slim_example_decoder.ItemHandlerCallback(
              ['image/context_features', 'image/context_feature_length'],
              self._reshape_context_features))

      self.keys_to_features[
          'image/context_feature_length'] = tf.FixedLenFeature((), tf.int64)
      self.items_to_handlers[fields.InputDataFields.context_feature_length] = (
          slim_example_decoder.Tensor('image/context_feature_length'))

    if num_additional_channels > 0:
      self.keys_to_features[
          'image/additional_channels/encoded'] = tf.FixedLenFeature(
              (num_additional_channels,), tf.string)
      self.items_to_handlers[
          fields.InputDataFields.
          image_additional_channels] = additional_channel_image
    self._num_keypoints = num_keypoints
    if num_keypoints > 0:
      self.keys_to_features['image/object/keypoint/x'] = (
          tf.VarLenFeature(tf.float32))
      self.keys_to_features['image/object/keypoint/y'] = (
          tf.VarLenFeature(tf.float32))
      self.keys_to_features['image/object/keypoint/visibility'] = (
          tf.VarLenFeature(tf.int64))
      self.items_to_handlers[fields.InputDataFields.groundtruth_keypoints] = (
          slim_example_decoder.ItemHandlerCallback(
              ['image/object/keypoint/y', 'image/object/keypoint/x'],
              self._reshape_keypoints))
      kpt_vis_field = fields.InputDataFields.groundtruth_keypoint_visibilities
      self.items_to_handlers[kpt_vis_field] = (
          slim_example_decoder.ItemHandlerCallback(
              ['image/object/keypoint/x', 'image/object/keypoint/visibility'],
              self._reshape_keypoint_visibilities))
      if load_keypoint_depth_features:
        self.keys_to_features['image/object/keypoint/z'] = (
            tf.VarLenFeature(tf.float32))
        self.keys_to_features['image/object/keypoint/z/weights'] = (
            tf.VarLenFeature(tf.float32))
        self.items_to_handlers[
            fields.InputDataFields.groundtruth_keypoint_depths] = (
                slim_example_decoder.ItemHandlerCallback(
                    ['image/object/keypoint/x', 'image/object/keypoint/z'],
                    self._reshape_keypoint_depths))
        self.items_to_handlers[
            fields.InputDataFields.groundtruth_keypoint_depth_weights] = (
                slim_example_decoder.ItemHandlerCallback(
                    ['image/object/keypoint/x',
                     'image/object/keypoint/z/weights'],
                    self._reshape_keypoint_depth_weights))

    if load_instance_masks:
      if instance_mask_type in (input_reader_pb2.DEFAULT,
                                input_reader_pb2.NUMERICAL_MASKS):
        self.keys_to_features['image/object/mask'] = (
            tf.VarLenFeature(tf.float32))
        self.items_to_handlers[
            fields.InputDataFields.groundtruth_instance_masks] = (
                slim_example_decoder.ItemHandlerCallback(
                    ['image/object/mask', 'image/height', 'image/width'],
                    self._reshape_instance_masks))
      elif instance_mask_type == input_reader_pb2.PNG_MASKS:
        self.keys_to_features['image/object/mask'] = tf.VarLenFeature(tf.string)
        self.items_to_handlers[
            fields.InputDataFields.groundtruth_instance_masks] = (
                slim_example_decoder.ItemHandlerCallback(
                    ['image/object/mask', 'image/height', 'image/width'],
                    self._decode_png_instance_masks))
      else:
        raise ValueError('Did not recognize the `instance_mask_type` option.')
      self.keys_to_features['image/object/mask/weight'] = (
          tf.VarLenFeature(tf.float32))
      self.items_to_handlers[
          fields.InputDataFields.groundtruth_instance_mask_weights] = (
              slim_example_decoder.Tensor('image/object/mask/weight'))
    if load_dense_pose:
      self.keys_to_features['image/object/densepose/num'] = (
          tf.VarLenFeature(tf.int64))
      self.keys_to_features['image/object/densepose/part_index'] = (
          tf.VarLenFeature(tf.int64))
      self.keys_to_features['image/object/densepose/x'] = (
          tf.VarLenFeature(tf.float32))
      self.keys_to_features['image/object/densepose/y'] = (
          tf.VarLenFeature(tf.float32))
      self.keys_to_features['image/object/densepose/u'] = (
          tf.VarLenFeature(tf.float32))
      self.keys_to_features['image/object/densepose/v'] = (
          tf.VarLenFeature(tf.float32))
      self.items_to_handlers[
          fields.InputDataFields.groundtruth_dp_num_points] = (
              slim_example_decoder.Tensor('image/object/densepose/num'))
      self.items_to_handlers[fields.InputDataFields.groundtruth_dp_part_ids] = (
          slim_example_decoder.ItemHandlerCallback(
              ['image/object/densepose/part_index',
               'image/object/densepose/num'], self._dense_pose_part_indices))
      self.items_to_handlers[
          fields.InputDataFields.groundtruth_dp_surface_coords] = (
              slim_example_decoder.ItemHandlerCallback(
                  ['image/object/densepose/x', 'image/object/densepose/y',
                   'image/object/densepose/u', 'image/object/densepose/v',
                   'image/object/densepose/num'],
                  self._dense_pose_surface_coordinates))
    if load_track_id:
      self.keys_to_features['image/object/track/label'] = (
          tf.VarLenFeature(tf.int64))
      self.items_to_handlers[
          fields.InputDataFields.groundtruth_track_ids] = (
              slim_example_decoder.Tensor('image/object/track/label'))

    if label_map_proto_file:
      # If the label_map_proto is provided, try to use it in conjunction with
      # the class text, and fall back to a materialized ID.
      label_handler = slim_example_decoder.BackupHandler(
          _ClassTensorHandler(
              'image/object/class/text', label_map_proto_file,
              default_value=''),
          slim_example_decoder.Tensor('image/object/class/label'))
      image_label_handler = slim_example_decoder.BackupHandler(
          _ClassTensorHandler(
              fields.TfExampleFields.image_class_text,
              label_map_proto_file,
              default_value=''),
          slim_example_decoder.Tensor(fields.TfExampleFields.image_class_label))
    else:
      label_handler = slim_example_decoder.Tensor('image/object/class/label')
      image_label_handler = slim_example_decoder.Tensor(
          fields.TfExampleFields.image_class_label)
    self.items_to_handlers[
        fields.InputDataFields.groundtruth_classes] = label_handler
    self.items_to_handlers[
        fields.InputDataFields.groundtruth_image_classes] = image_label_handler

    self._expand_hierarchy_labels = expand_hierarchy_labels
    self._ancestors_lut = None
    self._descendants_lut = None
    if expand_hierarchy_labels:
      if label_map_proto_file:
        ancestors_lut, descendants_lut = (
            label_map_util.get_label_map_hierarchy_lut(label_map_proto_file,
                                                       True))
        self._ancestors_lut = tf.constant(ancestors_lut, dtype=tf.int64)
        self._descendants_lut = tf.constant(descendants_lut, dtype=tf.int64)
      else:
        raise ValueError('In order to expand labels, the label_map_proto_file '
                         'has to be provided.')

  def decode(self, tf_example_string_tensor):
  def __init__(self,
               tensor_key,
               label_map_proto_file,
               shape_keys=None,
               shape=None,
               default_value=''):
    """Initializes the LookupTensor handler.

    Simply calls a vocabulary (most often, a label mapping) lookup.

    Args:
      tensor_key: the name of the `TFExample` feature to read the tensor from.
      label_map_proto_file: File path to a text format LabelMapProto message
        mapping class text to id.
      shape_keys: Optional name or list of names of the TF-Example feature in
        which the tensor shape is stored. If a list, then each corresponds to
        one dimension of the shape.
      shape: Optional output shape of the `Tensor`. If provided, the `Tensor` is
        reshaped accordingly.
      default_value: The value used when the `tensor_key` is not found in a
        particular `TFExample`.

    Raises:
      ValueError: if both `shape_keys` and `shape` are specified.
    """
    name_to_id = label_map_util.get_label_map_dict(
        label_map_proto_file, use_display_name=False)
    # We use a default_value of -1, but we expect all labels to be contained
    # in the label map.
    try:
      # Dynamically try to load the tf v2 lookup, falling back to contrib
      lookup = tf.compat.v2.lookup
      hash_table_class = tf.compat.v2.lookup.StaticHashTable
    except AttributeError:
      lookup = contrib_lookup
      hash_table_class = contrib_lookup.HashTable
    name_to_id_table = hash_table_class(
        initializer=lookup.KeyValueTensorInitializer(
            keys=tf.constant(list(name_to_id.keys())),
            values=tf.constant(list(name_to_id.values()), dtype=tf.int64)),
        default_value=-1)

    self._name_to_id_table = name_to_id_table
    super(_ClassTensorHandler, self).__init__(tensor_key, shape_keys, shape,
                                              default_value)

  def tensors_to_item(self, keys_to_tensors):

Tensorflow.sort

Here are the examples of the python api tensorflow.sort taken from open source projects. By voting up you can indicate which examples are most useful and appropriate. By voting up you can indicate which examples are most useful and appropriate.

def compute_wass_dist(y_true, y_pred, p):
    y, x = tf.sort(y_true, axis=1), tf.sort(y_pred, axis=1)
    p_fn = get_p_fn(p)
    wass_dist = tf.reduce_mean(p_fn(y - x), axis=1)
    return tf.reduce_mean(wass_dist, axis=-1)


def compute_mean_loss(y_true, y_pred, p):
    def _compute_weight_mask(self, w):
        input_shape, input_type = K.int_shape(w), K.dtype(w)
        reshaped = K.abs(K.reshape(w, (-1, input_shape[-1])))
        index = int(K.get_value(K.minimum(
            round(self.target_rate * K.int_shape(reshaped)[0]),
            K.int_shape(reshaped)[0] - 1,
        )))
        threshold = tf.sort(reshaped, axis=0)[index:index + 1]
        target_mask = K.reshape(K.cast(reshaped   <   threshold, dtype=input_type), input_shape)
        return target_mask

    def _compute_unit_mask(self, w):
    def _compute_unit_mask(self, w):
        input_shape, input_type = K.int_shape(w), K.dtype(w)
        reshaped = K.reshape(w, (-1, input_shape[-1]))
        norm = K.sqrt(K.sum(K.square(reshaped), axis=0) + K.epsilon())
        index = int(K.get_value(K.minimum(
            round(self.target_rate * K.int_shape(norm)[0]),
            K.int_shape(norm)[0] - 1,
        )))
        threshold = tf.sort(norm)[index]
        target_mask = K.cast(norm   <   threshold, dtype=input_type)
        while K.ndim(target_mask)  <  K.ndim(w):
            target_mask = K.expand_dims(target_mask, axis=0)
        return target_mask

    def get_config(self):
def top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
    indices = tf.stack([
        tf.range(0, batch),
        # number of indices to include
        tf.maximum(tf.reduce_sum(tf.cast(cumulative_probs   <  = p, tf.int32), axis=-1) - 1, 0),
    ], axis=-1)
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(
        logits  <  min_values,
        tf.ones_like(logits) * -1e10,
        logits,
    )


def evaluate(inp_img, transformer, tokenizer, max_length=128):
def top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
    indices = tf.stack([
        tf.range(0, batch),
        # number of indices to include
        tf.maximum(tf.reduce_sum(tf.cast(cumulative_probs   <  = p, tf.int32), axis=-1) - 1, 0),
    ], axis=-1)
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(
        logits  <  min_values,
        tf.ones_like(logits) * -1e10,
        logits,
    )


def evaluate(inp_img, tokenizer, transformer, temperature, top_k, top_p, options, seed, MAX_LENGTH=128):
def top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
    indices = tf.stack([
        tf.range(0, batch),
        # number of indices to include
        tf.maximum(tf.reduce_sum(tf.cast(cumulative_probs   <  = p, tf.int32), axis=-1) - 1, 0),
    ], axis=-1)
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(
        logits  <  min_values,
        tf.ones_like(logits) * -1e10,
        logits,
    )


def sample_sequence(*, hparams, length, start_token=None, batch_size=None, context=None, temperature=1, top_k=0, top_p=1):
    def __dynamic_k_matching__(self, ious, cost):
        # dynamic_k_matching https://github.com/Megvii-BaseDetection/YOLOX/tree/master/yolox/models/yolo_head.py#L607
        top_ious = tf.sort(ious, direction="DESCENDING")[:, : self.topk_ious_max]
        dynamic_ks = tf.maximum(tf.reduce_sum(top_ious, axis=-1), 1.0)  # [???] why sum up ious

        num_picked = tf.shape(cost)[-1]
        cc = tf.concat([cost, tf.expand_dims(dynamic_ks, 1)], axis=-1)
        # matching_matrix, tf.argsort default direction = "ASCENDING"
        topk_anchors = tf.map_fn(lambda xx: tf.reduce_sum(tf.one_hot(tf.argsort(xx[:-1])[: tf.cast(xx[-1], tf.int32)], num_picked), 0), cc)
        check_cond = tf.reduce_sum(topk_anchors, axis=0) > 1
        return tf.cond(
            tf.reduce_any(check_cond),
            lambda: self.__filter_anchors_matching_multi_bboxes__(topk_anchors, cost, check_cond),
            lambda: topk_anchors,
        )

    def __call__(self, bbox_labels_true, bbox_labels_pred):
def top_p_sample(logits, p=0.95):
  logits_sort = tf.sort(logits, direction='DESCENDING', axis=-1)
  probs_sort = tf.nn.softmax(logits_sort)
  probs_sums = tf.cumsum(probs_sort, axis=1, exclusive=True)
  logits_masked = tf.where(probs_sums   <   p, logits_sort,
                           tf.ones_like(logits_sort) * 1000)
  min_logits = tf.reduce_min(input_tensor=logits_masked, axis=1, keepdims=True)
  logits = tf.where(
    logits  <  min_logits,
    tf.ones_like(logits, dtype=logits.dtype) * -1e10,
    logits,
  )
  return logits


def sample_from(logits, return_onehot=False, sample_method='top_p'):
    def __eq__(self, other: object) -> bool:
        """
        :param other: A search space.
        :return: Whether the search space is identical to this one.
        """
        if not isinstance(other, DiscreteSearchSpace):
            return NotImplemented
        return bool(tf.reduce_all(tf.sort(self.points, 0) == tf.sort(other.points, 0)))

    def __deepcopy__(self, memo: dict[int, object]) -> DiscreteSearchSpace:
def softsort(s, tau=1, pow=1):
    s_sorted = tf.sort(s, direction='DESCENDING', axis=1)
    pairwise_distances = -tf.pow(tf.abs(tf.transpose(s, perm=[0, 2, 1]) - s_sorted), pow)
    P_hat = tf.nn.softmax(pairwise_distances / tau, -1)
    return P_hat


# s: M x n x 1
# softsort_p1(s): M x n x n
def softsort_p1(s, tau=1):
def softsort_p1(s, tau=1):
    s_sorted = tf.sort(s, direction='DESCENDING', axis=1)
    pairwise_distances = -tf.abs(tf.transpose(s, perm=[0, 2, 1]) - s_sorted)
    P_hat = tf.nn.softmax(pairwise_distances / tau, -1)
    return P_hat


# s: M x n x 1
# softsort_p2(s): M x n x n
def softsort_p2(s, tau=1):
def softsort_p2(s, tau=1):
    s_sorted = tf.sort(s, direction='DESCENDING', axis=1)
    pairwise_distances = -tf.square(tf.transpose(s, perm=[0, 2, 1]) - s_sorted)
    P_hat = tf.nn.softmax(pairwise_distances / tau, -1)
    return P_hat


# Pi: M x n x n row-stochastic
def prop_any_correct(P1, P2):
    def compute(self, nlist, positions, box):
        rinv = htf.nlist_rinv(nlist)
        # closest neighbors have largest value in 1/r, take top
        top_n = tf.sort(rinv, axis=1, direction='DESCENDING')[
            :, :self.top_neighs]
        # run through NN
        # make sure shape is definite
        top_n = tf.reshape(top_n, (-1, self.top_neighs))
        x = self.dense1(top_n)
        x = self.dense2(x)
        energy = self.last(x)
        forces = htf.compute_nlist_forces(nlist, energy)
        return forces


class WCA(htf.SimModel):
    def compute(self, nlist, positions, training):
        rinv = htf.nlist_rinv(nlist)
        # closest neighbors have largest value in 1/r, take top
        top_n = tf.sort(rinv, axis=1, direction='DESCENDING')[
            :, :self.top_neighs]
        # run through NN
        x = self.dense1(top_n)
        x = self.dense2(x)
        energy = self.last(x)
        if training:
            energy *= 2

        forces = htf.compute_nlist_forces(nlist, energy)
        if self.output_zero:
            energy *= 0.
        return forces, tf.reduce_sum(energy)


class LJRunningMeanModel(htf.SimModel):
def sort(a: Numeric, axis: Int = -1, descending: bool = False):
    if descending:
        direction = "DESCENDING"
    else:
        direction = "ASCENDING"
    return tf.sort(a, axis=axis, direction=direction)


@dispatch
def sort_key_val(t1, t2, dim=-1):
    values = tf.sort(t1, axis=dim)
    t2 = tf.broadcast_to(t2, t1.shape)
    return values, tf.gather(t2, tf.argsort(t1, axis=dim), axis=dim)

def batched_index_select(values, indices):
    def __get_dlr(self, logit, y_input):
        """ Private function
        Get DLR loss

        Args:
            logit: (tf_tensor) Logits
            y_input: (tf_tensor) Input label

        Returns:
            loss: (tf_tensor) DLR loss
        """

        # logit
        logit_sort = tf.sort(logit, axis=1)

        # onthot_y
        y_onehot = tf.one_hot(y_input , self.num_classes, dtype=tf.float32)
        logit_y = tf.reduce_sum(y_onehot * logit, axis=1)

        # z_i
        logit_pred = tf.reduce_max(logit, axis=1)
        cond = (logit_pred == logit_y)
        z_i = tf.where(cond, logit_sort[:, -2], logit_sort[:, -1])

        # loss
        z_y = logit_y
        z_p1 =  logit_sort[:, -1]
        z_p3 = logit_sort[:, -3]

        loss = - (z_y - z_i) / (z_p1 - z_p3 + 1e-12)
        return loss


    def __get_dlr_target(self, logits, y_input, y_target):
    def __get_dlr_target(self, logits, y_input, y_target):
        """ Private function
        Get targeted version of DLR loss

        Args:
            logit: (tf_tensor) Logits
            y_input: (tf_tensor) Input label
            y_target: (tf_tensor) Input targeted label

        Returns:
            loss: (tf_tensor) Targeted DLR loss
        """

        x = logits
        x_sort = tf.sort(x, axis=1)
        y_onehot = tf.one_hot(y_input, self.num_classes)
        y_target_onehot = tf.one_hot(y_target, self.num_classes)
        loss = -(tf.reduce_sum(x * y_onehot, axis=1) - tf.reduce_sum(x * y_target_onehot, axis=1)) / (x_sort[:, -1] - .5 * x_sort[:, -3] - .5 * x_sort[:, -4] + 1e-12)

        return loss


    # function called by public API directly #
    @tf.function
def sort_1d(input):
    return tf.sort(input), tf.cast(tf.argsort(input), dtype=tf.int64)


def arange(start, stop, dtype=tf.int64, ctx=None):
    def call(self, graph, feat):
        r"""Compute sort pooling.

        Parameters
        ----------
        graph : DGLGraph
            The graph.
        feat : tf.Tensor
            The input node feature with shape :math:`(N, D)` where
            :math:`N` is the number of nodes in the graph.

        Returns
        -------
        tf.Tensor
            The output feature with shape :math:`(B, k * D)`, where
            :math:`B` refers to the batch size.
        """
        with graph.local_scope():
            # Sort the feature of each node in ascending order.
            feat = tf.sort(feat, -1)
            graph.ndata['h'] = feat
            # Sort nodes according to their last features.
            ret = tf.reshape(topk_nodes(graph, 'h', self.k, sortby=-1)[0], (
                -1, self.k * feat.shape[-1]))
            return ret


class GlobalAttentionPooling(layers.Layer):
def top_p_logits(logits, p):
    """Nucleus sampling"""
    batch, _ = logits.shape.as_list()
    sorted_logits = tf.sort(logits, direction="DESCENDING", axis=-1)
    cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
    indices = tf.stack(
        [
            tf.range(0, batch),
            # number of indices to include
            tf.maximum(
                tf.reduce_sum(tf.cast(cumulative_probs   <  = p, tf.int32), axis=-1) - 1, 0
            ),
        ],
        axis=-1,
    )
    min_values = tf.gather_nd(sorted_logits, indices)
    return tf.where(logits  <  min_values, tf.ones_like(logits) * -1e10, logits,)


def sample_sequence(
def mask_tokens(tokens_ragged, mask_idx, do_audio_span=None, audio_token_length=6, text_span_start_counter=0,
                num_groups=1, padded_seq_len=None, do_audio_mask=False):
    """
    Masks tokens in a ragged representation.
    :param tokens_ragged: a [N, L] representation of tokens
                          you can do this conversion by e.g.
                          tokens_ragged = tf.RaggedTensor.from_value_rowids(tokens, segment_idx)

    :param mask_idx_onehot: a [N] vector for the rows we should mask
    :param do_audio_span: Optional: a [N] vector for whether to turn the row into an audio span.
    :param text_span_start_counter: An integer >= 0. basically if we have other text spans from a different masker,
                                    this means we won't cross-over into them
    :param num_groups: How many partitions to split the tokens into
    :param padded_seq_len: Length to pad things to
    :param do_audio_mask: Whether to mask audio

    :return: A [sum(do_mask), L] representation of text spans, in ragged form
             A list of tokens of size [  <  =L, 3]. The columns are [token_id, audio_span, text_span]
    """
    N = tokens_ragged.bounding_shape()[0]
    mask_idx = tf.sort(mask_idx, 0)
    text_spans = tf.gather(tokens_ragged, mask_idx)
    mask_idx_onehot = _one_hot(mask_idx, N)

    if do_audio_span is not None:
        do_audio_span = tf.logical_and(do_audio_span, tf.logical_not(mask_idx_onehot))

        audio_span_full = tf.fill([N, audio_token_length], AUDIOSPAN)

        tokens_ragged = tf.compat.v1.where(do_audio_span, audio_span_full, tokens_ragged)

    # Replace with mask token
    mask_tok = tf.fill([N, 1], MASK)
    if do_audio_mask:
        mask_tok = tf.concat([mask_tok, tf.fill([N, 1], MASKAUDIO)], 1)

    tokens_ragged = tf.compat.v1.where(mask_idx_onehot, mask_tok, tokens_ragged)

    # Replace each token with the corresponding index into text_spans if it's a MASK token, else -1
    text_ptr = tf.cumsum(tf.cast(mask_idx_onehot, dtype=tf.int32)) - 1 + text_span_start_counter
    text_ptr = tf.where(mask_idx_onehot, text_ptr, tf.fill([N], -1))

    # split into groups
    grp_size = N // num_groups

    output_grouped = []
    for i in range(num_groups):
        tokens_ragged_i = tokens_ragged[i * grp_size:(i + 1) * grp_size]
        idxs_i = tf.cast(tf.where(tokens_ragged_i)[:, 0], dtype=tf.int32)

        audio_ptr_i = idxs_i + i * grp_size

        # text pointer -- grab all tokens if it's a mask token
        text_ptr_i = text_ptr[i * grp_size:(i + 1) * grp_size]
        text_ptr_i = tf.gather(text_ptr_i, idxs_i)

        # Do the formatting thing with the audio pointers and text pointers (for MASK tokens)
        output_i = tf.stack([tokens_ragged_i.values, audio_ptr_i, text_ptr_i], -1)
        if padded_seq_len is not None:

            is_over_budget = get_shape_list(output_i, 2)[0] > padded_seq_len
            output_i = tf.cond(is_over_budget,
                               lambda: select_tokens(output_i, padded_seq_len, num_segments=N),
                               lambda: pad_tokens_to_fixed_size(output_i, padded_seq_len))
        output_grouped.append(output_i)
    return text_spans, output_grouped


def shift_ragged_tokens_at_positions(tokens_ragged, positions, right_to_left=True):
def test_build_vanilla_deep_gp_returns_correct_model(
    num_layers: int,
    likelihood_variance: float,
    trainable_likelihood: bool,
    inner_layer_sqrt_factor: bool,
    keras_float: None,
) -> None:
    num_data = 10
    x = np.arange(num_data).reshape(-1, 1).astype(np.double)
    data = mk_dataset(x, quadratic(x))
    search_space = Box([0.0], [10.0])

    num_inducing = num_data

    vanilla_deep_gp = build_vanilla_deep_gp(
        data,
        search_space,
        num_layers,
        num_inducing,
        inner_layer_sqrt_factor=inner_layer_sqrt_factor,
        likelihood_variance=likelihood_variance,
        trainable_likelihood=trainable_likelihood,
    )

    # check likelihood
    npt.assert_allclose(vanilla_deep_gp.likelihood_layer.likelihood.variance, likelihood_variance)
    assert vanilla_deep_gp.likelihood_layer.likelihood.variance.trainable == trainable_likelihood

    # comparison to the gpflux builder
    config = Config(
        num_inducing,
        inner_layer_sqrt_factor,
        likelihood_variance,
    )
    ref_deep_gp = build_constant_input_dim_deep_gp(x, num_layers=num_layers, config=config)
    npt.assert_equal(len(vanilla_deep_gp.f_layers), len(ref_deep_gp.f_layers))
    for i, layer in enumerate(vanilla_deep_gp.f_layers):
        ref_layer = ref_deep_gp.f_layers[i]
        npt.assert_allclose(
            tf.sort(layer.inducing_variable.inducing_variable.Z, axis=0),
            tf.sort(ref_layer.inducing_variable.inducing_variable.Z, axis=0),
        )
        npt.assert_allclose(layer.q_sqrt, ref_layer.q_sqrt)
    npt.assert_allclose(
        vanilla_deep_gp.likelihood_layer.likelihood.variance,
        ref_deep_gp.likelihood_layer.likelihood.variance,
    )


def test_build_vanilla_deep_gp_raises_for_incorrect_args(keras_float: None) -> None:

Previous PostNext Post

Related code examples