code: def calc_class_weights_from_dataloader(
      dataloader: "torch.utils.data.DataLoader",
      num_classes: int,
      data_dir: str,
  ) -> List[float]:
      """
      Calculate the weights of each class to be used for weighted loss. This is similar to the function calc_class_weights
      in text_classification_dataset, but it gets the labels from a dataloader rather than from a file.
      Args:
          dataloader: the dataloader for the training set
          num_classes: number of classes in the dataset
      """
      labels = []
      for batch in dataloader:
          labels.extend(tensor2list(batch[-1]))
      logging.info(f"Calculating label frequency stats...")
      total_sents, sent_label_freq, max_id = get_label_stats(
          labels,
          os.path.join(data_dir, "sentence_stats.tsv"),
          verbose=False,
      )
      if max_id >= num_classes:
          raise ValueError(
              f"Found an invalid label! Labels should be from [0, num_classes-1]."
          )
  
      class_weights_dict = get_freq_weights(sent_label_freq)
  
      logging.info(f"Total Sentence Pairs: {total_sents}")
      logging.info(f"Class Frequencies: {sent_label_freq}")
      logging.info(f"Class Weights: {class_weights_dict}")
      class_weights = fill_class_weights(
          weights=class_weights_dict, max_id=num_classes - 1
      )
      return class_weights
  
imports: import os
  import pickle
  from typing import Dict, List, Optional
  import torch
  from nemo.collections.common.tokenizers.tokenizer_spec import TokenizerSpec
  from nemo.collections.nlp.data.data_utils.data_preprocessing import DataProcessor, fill_class_weights, get_freq_weights, get_label_stats
  from nemo.collections.nlp.data.glue_benchmark.data_processors import InputExample
  from nemo.collections.nlp.data.glue_benchmark.glue_benchmark_dataset import GLUEDataset
  from nemo.collections.nlp.parts.utils_funcs import tensor2list
  from nemo.core.neural_types import CategoricalValuesType, ChannelType, MaskType, NeuralType
  from nemo.utils import logging
label_desc: Use image node_id 1 for calling a global function with example usage: calc_class_weights_from_dataloader(dataloader, num_classes, data_dir) and returns: class_weights