code: def measure_perplexity(predicted_indices, n_embed):
      # 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, n_embed)
          .float()
          .reshape(-1, n_embed)
      )
      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: compvis-stable-diffusion/ldm/modules/losses/vqperceptual.py
imports: import torch
  from torch import nn
  import torch.nn.functional
  from einops import repeat
  from taming.modules.discriminator.model import NLayerDiscriminator, weights_init
  from taming.modules.losses.lpips import LPIPS
  from taming.modules.losses.vqperceptual import hinge_d_loss, vanilla_d_loss
label_desc: Use image node_id 3 for calling a global function with example usage: measure_perplexity(predicted_indices, n_embed) and returns: perplexity,  cluster_use