tf.contrib.layers.layer_norm
Stay organized with collections
Save and categorize content based on your preferences.
Adds a Layer Normalization layer.
tf.contrib.layers.layer_norm(
inputs, center=True, scale=True, activation_fn=None, reuse=None,
variables_collections=None, outputs_collections=None, trainable=True,
begin_norm_axis=1, begin_params_axis=-1, scope=None
)
Based on the paper:
"Layer Normalization"
Jimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton
https://arxiv.org/abs/1607.06450
Can be used as a normalizer function for conv2d and fully_connected.
Given a tensor inputs
of rank R
, moments are calculated and normalization
is performed over axes begin_norm_axis ... R - 1
. Scaling and centering,
if requested, is performed over axes begin_params_axis .. R - 1
.
By default, begin_norm_axis = 1
and begin_params_axis = -1
,
meaning that normalization is performed over all but the first axis
(the HWC
if inputs
is NHWC
), while the beta
and gamma
trainable
parameters are calculated for the rightmost axis (the C
if inputs
is
NHWC
). Scaling and recentering is performed via broadcast of the
beta
and gamma
parameters with the normalized tensor.
The shapes of beta
and gamma
are inputs.shape[begin_params_axis:]
,
and this part of the inputs' shape must be fully defined.
Args |
inputs
|
A tensor having rank R . The normalization is performed over axes
begin_norm_axis ... R - 1 and centering and scaling parameters are
calculated over begin_params_axis ... R - 1 .
|
center
|
If True, add offset of beta to normalized tensor. If False, beta
is ignored.
|
scale
|
If True, multiply by gamma . If False, gamma is not used. When the
next layer is linear (also e.g. nn.relu ), this can be disabled since the
scaling can be done by the next layer.
|
activation_fn
|
Activation function, default set to None to skip it and
maintain a linear activation.
|
reuse
|
Whether or not the layer and its variables should be reused. To be
able to reuse the layer scope must be given.
|
variables_collections
|
Optional collections for the variables.
|
outputs_collections
|
Collections to add the outputs.
|
trainable
|
If True also add variables to the graph collection
GraphKeys.TRAINABLE_VARIABLES (see tf.Variable).
|
begin_norm_axis
|
The first normalization dimension: normalization will be
performed along dimensions begin_norm_axis : rank(inputs)
|
begin_params_axis
|
The first parameter (beta, gamma) dimension: scale and
centering parameters will have dimensions
begin_params_axis : rank(inputs) and will be broadcast with the
normalized inputs accordingly.
|
scope
|
Optional scope for variable_scope .
|
Returns |
A Tensor representing the output of the operation, having the same
shape and dtype as inputs .
|
Raises |
ValueError
|
If the rank of inputs is not known at graph build time,
or if inputs.shape[begin_params_axis:] is not fully defined at
graph build time.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.contrib.layers.layer_norm\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/layers/python/layers/layers.py#L2203-L2333) |\n\nAdds a Layer Normalization layer. \n\n tf.contrib.layers.layer_norm(\n inputs, center=True, scale=True, activation_fn=None, reuse=None,\n variables_collections=None, outputs_collections=None, trainable=True,\n begin_norm_axis=1, begin_params_axis=-1, scope=None\n )\n\n#### Based on the paper:\n\n\"Layer Normalization\"\n\nJimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton\n\n\u003chttps://arxiv.org/abs/1607.06450\u003e\n\nCan be used as a normalizer function for conv2d and fully_connected.\n\nGiven a tensor `inputs` of rank `R`, moments are calculated and normalization\nis performed over axes `begin_norm_axis ... R - 1`. Scaling and centering,\nif requested, is performed over axes `begin_params_axis .. R - 1`.\n\nBy default, `begin_norm_axis = 1` and `begin_params_axis = -1`,\nmeaning that normalization is performed over all but the first axis\n(the `HWC` if `inputs` is `NHWC`), while the `beta` and `gamma` trainable\nparameters are calculated for the rightmost axis (the `C` if `inputs` is\n`NHWC`). Scaling and recentering is performed via broadcast of the\n`beta` and `gamma` parameters with the normalized tensor.\n\nThe shapes of `beta` and `gamma` are `inputs.shape[begin_params_axis:]`,\nand this part of the inputs' shape must be fully defined.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `inputs` | A tensor having rank `R`. The normalization is performed over axes `begin_norm_axis ... R - 1` and centering and scaling parameters are calculated over `begin_params_axis ... R - 1`. |\n| `center` | If True, add offset of `beta` to normalized tensor. If False, `beta` is ignored. |\n| `scale` | If True, multiply by `gamma`. If False, `gamma` is not used. When the next layer is linear (also e.g. [`nn.relu`](/api_docs/python/tf/nn/relu)), this can be disabled since the scaling can be done by the next layer. |\n| `activation_fn` | Activation function, default set to None to skip it and maintain a linear activation. |\n| `reuse` | Whether or not the layer and its variables should be reused. To be able to reuse the layer scope must be given. |\n| `variables_collections` | Optional collections for the variables. |\n| `outputs_collections` | Collections to add the outputs. |\n| `trainable` | If `True` also add variables to the graph collection `GraphKeys.TRAINABLE_VARIABLES` (see tf.Variable). |\n| `begin_norm_axis` | The first normalization dimension: normalization will be performed along dimensions `begin_norm_axis : rank(inputs)` |\n| `begin_params_axis` | The first parameter (beta, gamma) dimension: scale and centering parameters will have dimensions `begin_params_axis : rank(inputs)` and will be broadcast with the normalized inputs accordingly. |\n| `scope` | Optional scope for `variable_scope`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` representing the output of the operation, having the same shape and dtype as `inputs`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|----------------------------------------------------------------------------------------------------------------------------------------------|\n| `ValueError` | If the rank of `inputs` is not known at graph build time, or if `inputs.shape[begin_params_axis:]` is not fully defined at graph build time. |\n\n\u003cbr /\u003e"]]