code: def plot_losses(
      losses: Union[nn.Module, List[nn.Module]],
      visdom_server: Optional["visdom.Visdom"] = None,
      env: Optional[str] = None,
      win: Optional[str] = None,
      title: str = "",
  ) -> Any:
      """Constructs a plot of specified losses as function of y * f(x). The losses
      are a list of nn.Module losses. Optionally, the environment, window handle,
      and title for the visdom plot can be specified.
      """
  
      if visdom_server is None and visdom_connected():
          visdom_server = vis[-1]
  
      # return if we are not connected to visdom server:
      if not visdom_server or not visdom_server.check_connection():
          print("WARNING: Not connected to visdom. Skipping plotting.")
          return
  
      # assertions:
      if isinstance(losses, nn.Module):
          losses = [losses]
      assert type(losses) == list
      assert all(isinstance(loss, nn.Module) for loss in losses)
      if any(isinstance(loss, UNSUPPORTED_LOSSES) for loss in losses):
          raise NotImplementedError("loss function not supported")
  
      # loop over all loss functions:
      for idx, loss in enumerate(losses):
          # construct scores and targets:
          score = torch.arange(-5.0, 5.0, 0.005)
          if idx == 0:
              loss_val = torch.FloatTensor(score.size(0), len(losses))
          if isinstance(loss, REGRESSION_LOSSES):
              target = torch.FloatTensor(score.size()).fill_(0.0)
          else:
              target = torch.LongTensor(score.size()).fill_(1)
  
          # compute loss values:
          for n in range(0, score.nelement()):
              loss_val[n][idx] = loss(
                  score.narrow(0, n, 1), target.narrow(0, n, 1)
              ).item()
  
      # show plot:
      title = str(loss) if title == "" else title
      legend = [str(loss) for loss in losses]
      opts = {
          "title": title,
          "xlabel": "Score",
          "ylabel": "Loss",
          "legend": legend,
      }
      win = visdom_server.line(
          loss_val, score, env=env, win=win, opts=opts
      )
      return win
  
file_path: ClassyVision/classy_vision/generic/visualize.py
imports: import math
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn.modules
  from classy_vision.generic.util import get_model_dummy_input, is_pos_int
  from classy_vision.models import ClassyModel
  from PIL import Image
label_desc: Use image node_id 4 for calling a global function with example usage: plot_losses(losses, visdom_server, env, win, title) and returns: win