class_docstr: Group normalization module.
  
  This applies group normalization to the inputs. This involves splitting the
  channels into groups before calculating the mean and variance. The default
  behaviour is to compute the mean and variance over the spatial dimensions and
  the grouped channels. The mean and variance will never be computed over the
  created groups axis.
  
  It transforms the input ``x`` into:
  
  .. math::
  
     \d{outputs} = \d{scale} \dfrac{x - \mu}{\sigma + \epsilon} + \d{offset}
  
  Where :math:`\mu` and :math:`\sigma` are respectively the mean and standard
  deviation of ``x``.
  
  There are many different variations for how users want to manage scale and
  offset if they require them at all. These are:
  
    - No ``scale``/``offset`` in which case ``create_*`` should be set to
      ``False`` and ``scale``/``offset`` aren't passed when the module is
      called.
    - Trainable ``scale``/``offset`` in which case create_* should be set to
      ``True`` and again ``scale``/``offset`` aren't passed when the module is
      called. In this case this module creates and owns the scale/offset
      variables.
    - Externally generated ``scale``/``offset``, such as for conditional
      normalization, in which case ``create_*`` should be set to ``False`` and
      then the values fed in at call time.
  
  Attributes:
    scale: If ``create_scale=True``, a trainable :tf:`Variable` holding the
      current scale.
    offset: If ``create_offset=True``, a trainable :tf:`Variable` holding the
      current offset.
class_docstr_tok: ["Group","normalization","module",".","This","applies","group","normalization","to","the","inputs",".","This","involves","splitting","the","channels","into","groups","before","calculating","the","mean","and","variance",".","The","default","behaviour","is","to","compute","the","mean","and","variance","over","the","spatial","dimensions","and","the","grouped","channels",".","The","mean","and","variance","will","never","be","computed","over","the","created","groups","axis",".","It","transforms","the","input","``","x","``","into",":","..","math",":",":","\\d","{","outputs","}","=","\\d","{","scale","}","\\dfrac","{","x","-","\\mu","}","{","\\sigma","+","\\epsilon","}","+","\\d","{","offset","}","Where",":","math",":","`","\\mu","`","and",":","math",":","`","\\sigma","`","are","respectively","the","mean","and","standard","deviation","of","``","x","``",".","There","are","many","different","variations","for","how","users","want","to","manage","scale","and","offset","if","they","require","them","at","all",".","These","are",":","-","No","``","scale","``","\/","``","offset","``","in","which","case","``","create_","*","``","should","be","set","to","``","False","``","and","``","scale","``","\/","``","offset","``","are","n't","passed","when","the","module","is","called",".","-","Trainable","``","scale","``","\/","``","offset","``","in","which","case","create_","*","should","be","set","to","``","True","``","and","again","``","scale","``","\/","``","offset","``","are","n't","passed","when","the","module","is","called",".","In","this","case","this","module","creates","and","owns","the","scale\/offset","variables",".","-","Externally","generated","``","scale","``","\/","``","offset","``",",","such","as","for","conditional","normalization",",","in","which","case","``","create_","*","``","should","be","set","to","``","False","``","and","then","the","values","fed","in","at","call","time",".","Attributes",":","scale",":","If","``","create_scale=True","``",",","a","trainable",":","tf",":","`","Variable","`","holding","the","current","scale",".","offset",":","If","``","create_offset=True","``",",","a","trainable",":","tf",":","`","Variable","`","holding","the","current","offset","."]
docstr: Constructs a ``GroupNorm`` module.
  
  Args:
    groups: number of groups to divide the channels by. The number of channels
      must be divisible by this.
    axis: ``int``, ``slice`` or sequence of ints representing the axes which
      should be normalized across. By default this is all but the first
      dimension. For time series data use `slice(2, None)` to average over the
      none Batch and Time data.
    create_scale: whether to create a trainable scale per channel applied
      after the normalization.
    create_offset: whether to create a trainable offset per channel applied
      after normalization and scaling.
    eps: Small epsilon to add to the variance to avoid division by zero.
      Defaults to ``1e-5``.
    scale_init: Optional initializer for the scale variable. Can only be set
      if ``create_scale=True``. By default scale is initialized to ``1``.
    offset_init: Optional initializer for the offset variable. Can only be set
      if ``create_offset=True``. By default offset is initialized to ``0``.
    data_format: The data format of the input. Can be either
      ``channels_first``, ``channels_last``, ``N...C`` or ``NC...``. By
      default it is ``channels_last``.
    name: Name of the module.
docstr_tok: ["Constructs","a","``","GroupNorm","``","module",".","Args",":","groups",":","number","of","groups","to","divide","the","channels","by",".","The","number","of","channels","must","be","divisible","by","this",".","axis",":","``","int","``",",","``","slice","``","or","sequence","of","ints","representing","the","axes","which","should","be","normalized","across",".","By","default","this","is","all","but","the","first","dimension",".","For","time","series","data","use","`","slice","(","2",",","None",")","`","to","average","over","the","none","Batch","and","Time","data",".","create_scale",":","whether","to","create","a","trainable","scale","per","channel","applied","after","the","normalization",".","create_offset",":","whether","to","create","a","trainable","offset","per","channel","applied","after","normalization","and","scaling",".","eps",":","Small","epsilon","to","add","to","the","variance","to","avoid","division","by","zero",".","Defaults","to","``","1e-5","``",".","scale_init",":","Optional","initializer","for","the","scale","variable",".","Can","only","be","set","if","``","create_scale=True","``",".","By","default","scale","is","initialized","to","``","1","``",".","offset_init",":","Optional","initializer","for","the","offset","variable",".","Can","only","be","set","if","``","create_offset=True","``",".","By","default","offset","is","initialized","to","``","0","``",".","data_format",":","The","data","format","of","the","input",".","Can","be","either","``","channels_first","``",",","``","channels_last","``",",","``","N","...","C","``","or","``","NC","...","``",".","By","default","it","is","``","channels_last","``",".","name",":","Name","of","the","module","."]
code: def __init__(
      self,
      groups: int,
      axis: types.Axis = slice(1, None),
      create_scale: bool = True,
      create_offset: bool = True,
      eps: types.FloatLike = 1e-5,
      scale_init: Optional[initializers.Initializer] = None,
      offset_init: Optional[initializers.Initializer] = None,
      data_format: str = "channels_last",
      name: Optional[str] = None,
  ):
      """Constructs a ``GroupNorm`` module.
  
      Args:
        groups: number of groups to divide the channels by. The number of channels
          must be divisible by this.
        axis: ``int``, ``slice`` or sequence of ints representing the axes which
          should be normalized across. By default this is all but the first
          dimension. For time series data use `slice(2, None)` to average over the
          none Batch and Time data.
        create_scale: whether to create a trainable scale per channel applied
          after the normalization.
        create_offset: whether to create a trainable offset per channel applied
          after normalization and scaling.
        eps: Small epsilon to add to the variance to avoid division by zero.
          Defaults to ``1e-5``.
        scale_init: Optional initializer for the scale variable. Can only be set
          if ``create_scale=True``. By default scale is initialized to ``1``.
        offset_init: Optional initializer for the offset variable. Can only be set
          if ``create_offset=True``. By default offset is initialized to ``0``.
        data_format: The data format of the input. Can be either
          ``channels_first``, ``channels_last``, ``N...C`` or ``NC...``. By
          default it is ``channels_last``.
        name: Name of the module.
      """
      super().__init__(name=name)
  
      if isinstance(axis, slice):
          self._axis = axis
      elif isinstance(axis, int):
          self._axis = [axis]
      elif isinstance(axis, collections.abc.Iterable) and all(
          isinstance(ax, int) for ax in axis
      ):
          self._axis = axis
      else:
          raise ValueError(
              "`axis` should be an int, slice or iterable of ints."
          )
  
      self._groups = groups
      self._eps = eps
  
      self._data_format = data_format
      self._channel_index = utils.get_channel_index(data_format)
  
      self._create_scale = create_scale
      self._create_offset = create_offset
  
      if self._create_scale:
          self._scale_init = (
              scale_init
              if scale_init is not None
              else initializers.Ones()
          )
      elif scale_init is not None:
          raise ValueError(
              "Cannot set `scale_init` if `create_scale=False`."
          )
      if self._create_offset:
          self._offset_init = (
              offset_init
              if offset_init is not None
              else initializers.Zeros()
          )
      elif offset_init is not None:
          raise ValueError(
              "Cannot set `offset_init` if `create_offset=False`."
          )
  
file_path: sonnet/sonnet/src/group_norm.py
imports: import collections.abc
  from typing import Optional
  from sonnet.src import base
  from sonnet.src import initializers
  from sonnet.src import once
  from sonnet.src import types
  from sonnet.src import utils
  import tensorflow
label_desc: Use image node_id 1 to create a new GroupNorm object from inherited base classes: base with example: obj = GroupNorm(groups, axis, create_scale, create_offset, eps, scale_init, offset_init, data_format, name)