class_docstr: Base Class for implementing AdaptiveModel with frameworks like PyTorch and ONNX.
class_docstr_tok: ["Base","Class","for","implementing","AdaptiveModel","with","frameworks","like","PyTorch","and","ONNX","."]
docstr: Format predictions for inference.
  
  :param logits: model logits
  :type logits: torch.tensor
  :param kwargs: placeholder for passing generic parameters
  :type kwargs: object
  :return: predictions in the right format
code: def formatted_preds(self, logits, **kwargs):
      """
      Format predictions for inference.
  
      :param logits: model logits
      :type logits: torch.tensor
      :param kwargs: placeholder for passing generic parameters
      :type kwargs: object
      :return: predictions in the right format
      """
      n_heads = len(self.prediction_heads)
  
      if n_heads == 0:
          # just return LM output (e.g. useful for extracting embeddings at inference time)
          preds_final = self.language_model.formatted_preds(
              logits=logits, **kwargs
          )
  
      elif n_heads == 1:
          preds_final = []
          # This try catch is to deal with the fact that sometimes we collect preds before passing it to
          # formatted_preds (see Inferencer._get_predictions_and_aggregate()) and sometimes we don't
          # (see Inferencer._get_predictions())
          try:
              preds = kwargs["preds"]
              temp = [y[0] for y in preds]
              preds_flat = [
                  item for sublist in temp for item in sublist
              ]
              kwargs["preds"] = preds_flat
          except KeyError:
              kwargs["preds"] = None
          head = self.prediction_heads[0]
          logits_for_head = logits[0]
          preds = head.formatted_preds(logits=logits_for_head, **kwargs)
          # TODO This is very messy - we need better definition of what the output should look like
          if type(preds) == list:
              preds_final += preds
          elif type(preds) == dict and "predictions" in preds:
              preds_final.append(preds)
  
      # This case is triggered by Natural Questions
      else:
          preds_final = [list() for _ in range(n_heads)]
          preds = kwargs.get("preds")
          if preds is not None:
              preds_for_heads = stack(preds)
              logits_for_heads = [None] * n_heads
              del kwargs["preds"]
          else:
              preds_for_heads = [None] * n_heads
              logits_for_heads = logits
          preds_final = [list() for _ in range(n_heads)]
  
          if not "samples" in kwargs:
              samples = [
                  s for b in kwargs["baskets"] for s in b.samples
              ]
              kwargs["samples"] = samples
  
          for i, (head, preds_for_head, logits_for_head) in enumerate(
              zip(
                  self.prediction_heads,
                  preds_for_heads,
                  logits_for_heads,
              )
          ):
              preds = head.formatted_preds(
                  logits=logits_for_head, preds=preds_for_head, **kwargs
              )
              preds_final[i].append(preds)
  
          # Look for a merge() function amongst the heads and if a single one exists, apply it to preds_final
          merge_fn = pick_single_fn(
              self.prediction_heads, "merge_formatted_preds"
          )
          if merge_fn:
              preds_final = merge_fn(preds_final)
  
      return preds_final
  
imports: import copy
  import json
  import logging
  import os
  from pathlib import Path
  import multiprocessing
  import numpy
  import torch
  from torch import nn
  from transformers import AutoConfig
  from transformers.convert_graph_to_onnx import convert, quantize
  from farm.data_handler.processor import Processor
  from farm.modeling.language_model import LanguageModel
  from farm.modeling.prediction_head import PredictionHead, pick_single_fn
  from farm.utils import MLFlowLogger, stack
  import farm.conversion.transformers
label_desc: Use image node_id 5 for calling the BaseAdaptiveModel obj's underlying member method code with example usage: obj.formatted_preds(logits) and returns: preds_final