Skip to content

Commit 333d81c

Browse files
committed
[layers] LayerNormLayer
1 parent 03326d2 commit 333d81c

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

docs/modules/layers.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Layer list
294294
BatchNormLayer
295295
LocalResponseNormLayer
296296
InstanceNormLayer
297+
LayerNormLayer
297298

298299
ROIPoolingLayer
299300

@@ -568,6 +569,9 @@ Instance Normalization
568569
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
569570
.. autoclass:: InstanceNormLayer
570571

572+
Layer Normalization
573+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
574+
.. autoclass:: LayerNormLayer
571575

572576
Object Detection
573577
-------------------

tensorlayer/layers.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,6 +3538,56 @@ def __init__(
35383538
self.all_layers.extend( [self.outputs] )
35393539
self.all_params.extend( variables )
35403540

3541+
class LayerNormLayer(Layer):
3542+
"""
3543+
The :class:`LayerNormLayer` class is for layer normalization, see `tf.contrib.layers.layer_norm <https://www.tensorflow.org/api_docs/python/tf/contrib/layers/layer_norm>`_.
3544+
3545+
Parameters
3546+
----------
3547+
layer : a :class:`Layer` instance
3548+
The `Layer` class feeding into this layer.
3549+
act : activation function
3550+
The function that is applied to the layer activations.
3551+
others : see `tf.contrib.layers.layer_norm <https://www.tensorflow.org/api_docs/python/tf/contrib/layers/layer_norm>`_
3552+
"""
3553+
def __init__(self,
3554+
layer=None,
3555+
center=True,
3556+
scale=True,
3557+
act=tf.identity,
3558+
reuse=None,
3559+
variables_collections=None,
3560+
outputs_collections=None,
3561+
trainable=True,
3562+
begin_norm_axis=1,
3563+
begin_params_axis=-1,
3564+
name='layernorm'
3565+
):
3566+
3567+
if tf.__version__ < "1.3":
3568+
raise Exception("Please use TF 1.3+")
3569+
3570+
Layer.__init__(self, name=name)
3571+
self.inputs = layer.outputs
3572+
print(" [TL] LayerNormLayer %s: act:%s" %
3573+
(self.name, act.__name__))
3574+
self.outputs = tf.contrib.layers.layer_norm(self.inputs,
3575+
center=center,
3576+
scale=scale,
3577+
activation_fn=act,
3578+
reuse=reuse,
3579+
variables_collections=variables_collections,
3580+
outputs_collections=outputs_collections,
3581+
trainable=trainable,
3582+
begin_norm_axis=begin_norm_axis,
3583+
begin_params_axis=begin_params_axis,
3584+
scope=name
3585+
)
3586+
self.all_layers = list(layer.all_layers)
3587+
self.all_params = list(layer.all_params)
3588+
self.all_drop = dict(layer.all_drop)
3589+
self.all_layers.extend( [self.outputs] )
3590+
35413591

35423592
## Pooling layer
35433593
class PoolLayer(Layer):

0 commit comments

Comments
 (0)