class_docstr: Helper handler to log model's weights or gradients as scalars.
class_docstr_tok: ["Helper","handler","to","log","model","'s","weights","or","gradients","as","scalars","."]
code: def __init__(
      self,
      model: nn.Module,
      reduction: Callable[
          [torch.Tensor], Union[float, torch.Tensor]
      ] = torch.norm,
      tag: Optional[str] = None,
      whitelist: Optional[
          Union[List[str], Callable[[str, nn.Parameter], bool]]
      ] = None,
  ):
      super(BaseWeightsScalarHandler, self).__init__(
          model, tag=tag, whitelist=whitelist
      )
  
      if not callable(reduction):
          raise TypeError(
              f"Argument reduction should be callable, but given {type(reduction)}"
          )
  
      def _is_0D_tensor(t: Any) -> bool:
          return isinstance(t, torch.Tensor) and t.ndimension() == 0
  
      # Test reduction function on a tensor
      o = reduction(torch.ones(4, 2))
      if not (isinstance(o, numbers.Number) or _is_0D_tensor(o)):
          raise TypeError(
              f"Output of the reduction function should be a scalar, but got {type(o)}"
          )
  
      self.reduction = reduction
  
imports: import numbers
  import warnings
  from abc import ABCMeta, abstractmethod
  from collections import OrderedDict
  from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
  import torch
  import torch.nn
  from torch.optim import Optimizer
  from ignite.engine import Engine, Events, EventsList, State
  from ignite.engine.events import CallableEventWithFilter, RemovableEventHandle
label_desc: Use image node_id 1 to create a new BaseWeightsScalarHandler object from inherited base classes: BaseWeightsHandler with example: obj = BaseWeightsScalarHandler(model, reduction, tag, whitelist)