ffn.py 2.8 KB
Newer Older
L
lifuchen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
L
lifuchen 已提交
14 15 16 17 18 19 20 21 22
import paddle.fluid.dygraph as dg
import paddle.fluid.layers as layers
import paddle.fluid as fluid
import math
from parakeet.modules.customized import Conv1D


class PositionwiseFeedForward(dg.Layer):
    ''' A two-feed-forward-layer module '''
L
lifuchen 已提交
23 24 25 26 27 28 29 30

    def __init__(self,
                 d_in,
                 num_hidden,
                 filter_size,
                 padding=0,
                 use_cudnn=True,
                 dropout=0.1):
L
lifuchen 已提交
31 32 33 34 35 36
        super(PositionwiseFeedForward, self).__init__()
        self.num_hidden = num_hidden
        self.use_cudnn = use_cudnn
        self.dropout = dropout

        k = math.sqrt(1 / d_in)
L
lifuchen 已提交
37 38 39 40 41 42 43 44 45 46
        self.w_1 = Conv1D(
            num_channels=d_in,
            num_filters=num_hidden,
            filter_size=filter_size,
            padding=padding,
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.XavierInitializer()),
            bias_attr=fluid.ParamAttr(initializer=fluid.initializer.Uniform(
                low=-k, high=k)),
            use_cudnn=use_cudnn)
L
lifuchen 已提交
47
        k = math.sqrt(1 / num_hidden)
L
lifuchen 已提交
48 49 50 51 52 53 54 55 56 57
        self.w_2 = Conv1D(
            num_channels=num_hidden,
            num_filters=d_in,
            filter_size=filter_size,
            padding=padding,
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.XavierInitializer()),
            bias_attr=fluid.ParamAttr(initializer=fluid.initializer.Uniform(
                low=-k, high=k)),
            use_cudnn=use_cudnn)
L
lifuchen 已提交
58 59 60 61 62 63 64 65 66 67 68
        self.layer_norm = dg.LayerNorm(d_in)

    def forward(self, input):
        """
        Feed Forward Network.
        
        Args:
            input (Variable): Shape(B, T, C), dtype: float32. The input value.
        Returns:
            output (Variable), Shape(B, T, C), the result after FFN.
        """
L
lifuchen 已提交
69
        x = layers.transpose(input, [0, 2, 1])
L
lifuchen 已提交
70
        #FFN Networt
L
lifuchen 已提交
71
        x = self.w_2(layers.relu(self.w_1(x)))
L
lifuchen 已提交
72

L
lifuchen 已提交
73
        # dropout
74 75
        x = layers.dropout(
            x, self.dropout, dropout_implementation='upscale_in_train')
L
lifuchen 已提交
76

L
lifuchen 已提交
77
        x = layers.transpose(x, [0, 2, 1])
L
lifuchen 已提交
78 79
        # residual connection
        x = x + input
L
lifuchen 已提交
80

L
lifuchen 已提交
81 82 83
        #layer normalization
        output = self.layer_norm(x)

L
lifuchen 已提交
84
        return output