code: def measure_perplexity(
      predicted_indices: torch.Tensor, num_centroids: int
  ) -> Tuple[torch.Tensor, torch.Tensor]:
      # src: https://github.com/karpathy/deep-vector-quantization/blob/main/model.py
      # eval cluster perplexity. when perplexity == num_embeddings then all clusters are used exactly equally
      encodings = (
          F.one_hot(predicted_indices, num_centroids)
          .float()
          .reshape(-1, num_centroids)
      )
      avg_probs = encodings.mean(0)
      perplexity = (
          -(avg_probs * torch.log(avg_probs + 1e-10)).sum()
      ).exp()
      cluster_use = torch.sum(avg_probs > 0)
      return perplexity, cluster_use
  
file_path: generative-models/sgm/modules/autoencoding/regularizers/base.py
imports: from abc import abstractmethod
  from typing import Any, Tuple
  import torch
  import torch.nn.functional
  from torch import nn
label_desc: Use image node_id 1 for calling a global function with example usage: measure_perplexity(predicted_indices, num_centroids) and returns: perplexity,  cluster_use