class_docstr: Transformer with Masked Language Model
docstr: Compute on step/epoch metrics
docstr_tok: ["Compute","on","step\/epoch","metrics"]
code: def log_metrics(
      self,
      predictions,
      targets,
      loss,
      stage,
      on_step: bool = True,
      on_epoch: bool = True,
  ):
      """Compute on step/epoch metrics"""
      loss = loss.cpu()
      cls_preds = F.softmax(predictions[1], dim=-1)
      mlm_preds = F.softmax(predictions[0], dim=-1).permute(0, 2, 1)
  
      cls_targs = targets[1]
      mlm_targs = targets[0]
  
      if stage == "train":
          self.log(
              "train/loss", loss, on_step=on_step, on_epoch=on_epoch
          )
          self.log(
              "train/perplexity",
              torch.sqrt(loss),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "train/accuracy",
              self.train_accuracy(mlm_preds, mlm_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "train/recall",
              self.train_recall(mlm_preds, mlm_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "train/precision",
              self.train_precision(mlm_preds, mlm_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "train/f1",
              self.train_f1(mlm_preds, mlm_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "train/cls_acc",
              self.train_cls_acc(cls_preds, cls_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "train/cls_f1",
              self.train_cls_f1(cls_preds, cls_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
  
      elif stage == "val":
          self.log("val/loss", loss, on_step=on_step, on_epoch=on_epoch)
          self.log(
              "val/perplexity",
              torch.sqrt(loss),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "val/accuracy",
              self.val_accuracy(mlm_preds, mlm_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "val/recall",
              self.val_recall(mlm_preds, mlm_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "val/precision",
              self.val_precision(mlm_preds, mlm_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "val/f1",
              self.val_f1(mlm_preds, mlm_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "val/cls_acc",
              self.val_cls_acc(cls_preds, cls_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
          self.log(
              "val/cls_f1",
              self.val_cls_f1(cls_preds, cls_targs),
              on_step=on_step,
              on_epoch=on_epoch,
          )
  
imports: from abc import ABC
  from base64 import decode
  from math import gamma
  import torch
  import torch.nn
  import torch.nn.functional
  import torchmetrics
  import pickle
  import pytorch_lightning
  from pytorch_lightning import seed_everything
  import os
  from pathlib import Path
  import logging
  from src.transformer.transformer_utils import *
  from src.transformer.transformer import Transformer, MaskedLanguageModel, CLS_Decoder
label_desc: Use image node_id 11 for calling the TransformerEncoder obj's underlying member method code with example usage: obj.log_metrics(predictions, targets, loss, stage, on_step, on_epoch) without return types