class_docstr: ## Top-k Sampler
class_docstr_tok: ["#","#","Top-k","Sampler"]
docstr: Sample from logits
docstr_tok: ["Sample","from","logits"]
code: def __call__(self, logits: torch.Tensor):
      """
      Sample from logits
      """
      # New logits filled with $-\infty$; i.e. zero probability
      zeros = logits.new_ones(logits.shape) * float("-inf")
      # Pick the largest $k$ logits and their indices
      values, indices = torch.topk(logits, self.k, dim=-1)
      # Set the values of the top-k selected indices to actual logits.
      # Logits of other tokens remain $-\infty$
      zeros.scatter_(-1, indices, values)
  
      # Sample from the top-k logits with the specified sampler.
      return self.sampler(zeros)
  
imports: import torch
  from labml_nn.sampling import Sampler
label_desc: Use image node_id 2 for calling the TopKSampler obj's underlying member method code with example usage: obj.__call__(logits) and returns: self