class_docstr: Normalizes inputs along the given axes.
  
  This is a generic implementation of normalization along specific axes of the
  input. :class:`InstanceNorm` is a subclass of this module, it normalizes over
  the spatial dimensions.
  
  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.
docstr: Constructs an ``LayerNorm`` module.
  
  Args:
    axis: An ``int``, ``slice`` or sequence of ``int``\s representing the axes
      which should be normalized across. Typical usages are: ``1`` or ``-1``
      for normalization over just the channels and ``slice(1, None)``,
      ``slice(2, None)`` for normalization over the spatial and channel
      dimensions whilst avoiding the batch and/or time dimensions.
    create_scale: ``bool`` representing whether to create a trainable scale
      per channel applied after the normalization.
    create_offset: ``bool`` representing whether to create a trainable offset
      per channel applied after normalization and scaling.
    eps: Small epsilon to avoid division by zero variance. 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","an","``","LayerNorm","``","module",".","Args",":","axis",":","An","``","int","``",",","``","slice","``","or","sequence","of","``","int","``","\\s","representing","the","axes","which","should","be","normalized","across",".","Typical","usages","are",":","``","1","``","or","``","-1","``","for","normalization","over","just","the","channels","and","``","slice","(","1",",","None",")","``",",","``","slice","(","2",",","None",")","``","for","normalization","over","the","spatial","and","channel","dimensions","whilst","avoiding","the","batch","and\/or","time","dimensions",".","create_scale",":","``","bool","``","representing","whether","to","create","a","trainable","scale","per","channel","applied","after","the","normalization",".","create_offset",":","``","bool","``","representing","whether","to","create","a","trainable","offset","per","channel","applied","after","normalization","and","scaling",".","eps",":","Small","epsilon","to","avoid","division","by","zero","variance",".","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,
      axis: types.Axis,
      create_scale: bool,
      create_offset: bool,
      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,
  ):
      r"""Constructs an ``LayerNorm`` module.
  
      Args:
        axis: An ``int``, ``slice`` or sequence of ``int``\s representing the axes
          which should be normalized across. Typical usages are: ``1`` or ``-1``
          for normalization over just the channels and ``slice(1, None)``,
          ``slice(2, None)`` for normalization over the spatial and channel
          dimensions whilst avoiding the batch and/or time dimensions.
        create_scale: ``bool`` representing whether to create a trainable scale
          per channel applied after the normalization.
        create_offset: ``bool`` representing whether to create a trainable offset
          per channel applied after normalization and scaling.
        eps: Small epsilon to avoid division by zero variance. 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._eps = eps
  
      self._data_format = data_format
      self._channel_index = utils.get_channel_index(data_format)
  
      self._rank = None
  
      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/axis_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 LayerNorm object from inherited base classes: base with example: obj = LayerNorm(axis, create_scale, create_offset, eps, scale_init, offset_init, data_format, name)