class_docstr: Salience map clustering.
class_docstr_tok: ["Salience","map","clustering","."]
docstr: Compute a fixed-length representation from the variable-length salience.
  
  The representation is a simple vocabulary vector with salience weights as
  values. Every resulting vector is normalized to unit length.
  
  When a token occurs multiple times in the input we keep the value whose
  absolute value is largest.
  
  Args:
    token_saliencies: List of mappings from salience field to TokenSaliency
      objects. This is the result of a post-hoc explanation method, such as
      gradient l2.
    vocab_lookup: Mapping from word type to its index in the vocabulary.
    top_k: Number of tokens to be considered to represent each sentence. They
      will be determined based on their saliency scores.
  
  Returns:
    List of one mapping per example. Every element maps a salience field to
    its fixed-length representation.
docstr_tok: ["Compute","a","fixed-length","representation","from","the","variable-length","salience",".","The","representation","is","a","simple","vocabulary","vector","with","salience","weights","as","values",".","Every","resulting","vector","is","normalized","to","unit","length",".","When","a","token","occurs","multiple","times","in","the","input","we","keep","the","value","whose","absolute","value","is","largest",".","Args",":","token_saliencies",":","List","of","mappings","from","salience","field","to","TokenSaliency","objects",".","This","is","the","result","of","a","post-hoc","explanation","method",",","such","as","gradient","l2",".","vocab_lookup",":","Mapping","from","word","type","to","its","index","in","the","vocabulary",".","top_k",":","Number","of","tokens","to","be","considered","to","represent","each","sentence",".","They","will","be","determined","based","on","their","saliency","scores",".","Returns",":","List","of","one","mapping","per","example",".","Every","element","maps","a","salience","field","to","its","fixed-length","representation","."]
code: def _compute_fixed_length_representation(
      self,
      token_saliencies: list[JsonDict],
      vocab_lookup: dict[str, int],
      top_k: int,
  ) -> list[dict[str, np.ndarray]]:
      """Compute a fixed-length representation from the variable-length salience.
  
      The representation is a simple vocabulary vector with salience weights as
      values. Every resulting vector is normalized to unit length.
  
      When a token occurs multiple times in the input we keep the value whose
      absolute value is largest.
  
      Args:
        token_saliencies: List of mappings from salience field to TokenSaliency
          objects. This is the result of a post-hoc explanation method, such as
          gradient l2.
        vocab_lookup: Mapping from word type to its index in the vocabulary.
        top_k: Number of tokens to be considered to represent each sentence. They
          will be determined based on their saliency scores.
  
      Returns:
        List of one mapping per example. Every element maps a salience field to
        its fixed-length representation.
      """
      representations = []
      for instance in token_saliencies:
          per_field_results = {}
  
          for salience_field, token_salience in instance.items():
              token_weights = {}
  
              for token, score in zip(
                  token_salience.tokens, token_salience.salience
              ):
                  token_weights[token] = max(
                      [token_weights.get(token, score), score], key=abs
                  )
  
              representation = self._convert_to_bow_vector(
                  token_weights, vocab_lookup, top_k
              )
              # Normalize to unit length.
              representation = np.asarray(
                  representation
              ) / np.linalg.norm(representation)
              per_field_results[salience_field] = representation
          representations.append(per_field_results)
      return representations
  
file_path: lit/lit_nlp/components/salience_clustering.py
imports: from collections.abc import Sequence
  from typing import Optional
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  import numpy
  from sklearn import cluster
label_desc: Use image node_id 4 for calling the SalienceClustering obj's underlying member method code with example usage: obj._compute_fixed_length_representation(token_saliencies, vocab_lookup, top_k) and returns: representations