recurrent_units.py 12.0 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13
#
# 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.
Q
qijun 已提交
14

Z
zhangjinchao01 已提交
15 16 17 18 19 20 21 22 23 24
# recurrent_units.py
# Version 2.0
#
# Some recurrent units can be used in recurrent layer group, 
#   to use these units, import this module in your config_file:
#     import trainer.recurrent_units 
# 

from paddle.trainer.config_parser import *

Q
qijun 已提交
25

Z
zhangjinchao01 已提交
26 27 28 29 30 31
# long short term memory, can be used in recurrent machine
# *inputs* must be a list of Projections, for example:
#   inputs = [FullMatrixProjection("input_layer_name")],
# *para_prefix* defines parameter names, if the *para_prefix* of 
#   two LstmRecurrentUnit is same, they share same parameters
# *out_memory* can be defined outside if it's used outside
Q
qijun 已提交
32 33 34 35 36 37 38 39 40
def LstmRecurrentUnit(name,
                      size,
                      active_type,
                      state_active_type,
                      gate_active_type,
                      inputs,
                      para_prefix=None,
                      error_clipping_threshold=0,
                      out_memory=None):
Z
zhangjinchao01 已提交
41

Q
qijun 已提交
42
    if para_prefix is None:
Z
zhangjinchao01 已提交
43 44
        para_prefix = name
    if out_memory is None:
Q
qijun 已提交
45 46 47
        out_memory = Memory(name=name, size=size)

    state_memory = Memory(name=name + "_" + "state", size=size)
Z
zhangjinchao01 已提交
48 49

    Layer(
Q
qijun 已提交
50 51 52 53 54 55 56 57 58 59
        name=name + "_" + "input_recurrent",
        type="mixed",
        size=size * 4,  #(input_s, input_gate, forget_gate, output_gate)
        error_clipping_threshold=error_clipping_threshold,
        bias=Bias(
            initial_std=0, parameter_name=para_prefix + "_input_recurrent.b"),
        inputs=inputs + [
            FullMatrixProjection(
                out_memory, parameter_name=para_prefix + "_input_recurrent.w"),
        ], )
Z
zhangjinchao01 已提交
60
    LstmStepLayer(
Q
qijun 已提交
61 62 63 64 65 66 67
        name=name,
        size=size,
        bias=Bias(parameter_name=para_prefix + "_check.b"),
        inputs=[name + "_" + "input_recurrent", state_memory],
        active_type=active_type,
        active_gate_type=gate_active_type,
        active_state_type=state_active_type, )
Z
zhangjinchao01 已提交
68
    GetOutputLayer(
Q
qijun 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        name=name + "_" + "state",
        size=size,
        inputs=Input(
            name, input_layer_argument="state"), )


def LstmRecurrentUnitNaive(name,
                           size,
                           active_type,
                           state_active_type,
                           gate_active_type,
                           inputs,
                           para_prefix=None,
                           error_clipping_threshold=0,
                           out_memory=None):

    if para_prefix is None:
Z
zhangjinchao01 已提交
86 87
        para_prefix = name
    if out_memory is None:
Q
qijun 已提交
88 89 90
        out_memory = Memory(name=name, size=size)

    state_memory = Memory(name=name + "_" + "state", size=size)
Z
zhangjinchao01 已提交
91 92

    Layer(
Q
qijun 已提交
93 94 95 96 97 98 99 100 101 102
        name=name + "_" + "input_recurrent",
        type="mixed",
        size=size * 4,  #(input_s, input_gate, forget_gate, output_gate)
        error_clipping_threshold=error_clipping_threshold,
        bias=Bias(
            initial_std=0, parameter_name=para_prefix + "_input_recurrent.b"),
        inputs=inputs + [
            FullMatrixProjection(
                out_memory, parameter_name=para_prefix + "_input_recurrent.w"),
        ], )
Z
zhangjinchao01 已提交
103
    ExpressionLayer(
Q
qijun 已提交
104 105 106 107 108 109 110
        name=name + "_" + "input_s",
        size=size,
        active_type=active_type,
        inputs=[
            IdentityOffsetProjection(
                name + "_" + "input_recurrent", offset=0)
        ], )
Z
zhangjinchao01 已提交
111
    ExpressionLayer(
Q
qijun 已提交
112 113 114 115 116 117 118
        name=name + "_" + "input_gate",
        active_type=gate_active_type,
        inputs=[
            IdentityOffsetProjection(
                name + "_" + "input_recurrent", offset=size), DotMulProjection(
                    state_memory, parameter_name=para_prefix + "_input_check.w")
        ], )
Z
zhangjinchao01 已提交
119
    ExpressionLayer(
Q
qijun 已提交
120 121 122 123 124 125 126 127
        name=name + "_" + "forget_gate",
        active_type=gate_active_type,
        inputs=[
            IdentityOffsetProjection(
                name + "_" + "input_recurrent", offset=size * 2),
            DotMulProjection(
                state_memory, parameter_name=para_prefix + "_forget_check.w")
        ], )
Z
zhangjinchao01 已提交
128
    ExpressionLayer(
Q
qijun 已提交
129 130 131 132 133
        name=name + "_" + "state",
        inputs=[
            DotMulOperator([name + "_" + "input_s", name + "_" + "input_gate"]),
            DotMulOperator([state_memory, name + "_" + "forget_gate"]),
        ], )
Z
zhangjinchao01 已提交
134
    ExpressionLayer(
Q
qijun 已提交
135 136 137 138 139 140 141 142 143
        name=name + "_" + "output_gate",
        active_type=gate_active_type,
        inputs=[
            IdentityOffsetProjection(
                name + "_" + "input_recurrent", offset=size * 3),
            DotMulProjection(
                name + "_" + "state",
                parameter_name=para_prefix + "_output_check.w")
        ], )
Z
zhangjinchao01 已提交
144
    ExpressionLayer(
Q
qijun 已提交
145 146 147
        name=name + "_" + "state_atv",
        active_type=state_active_type,
        inputs=IdentityProjection(name + "_" + "state"), )
Z
zhangjinchao01 已提交
148
    ExpressionLayer(
Q
qijun 已提交
149 150 151 152
        name=name,
        inputs=DotMulOperator(
            [name + "_" + "state_atv", name + "_" + "output_gate"]), )

Z
zhangjinchao01 已提交
153 154 155

# like LstmRecurrentUnit, but it's a layer group.
# it is equivalent to LstmLayer
Q
qijun 已提交
156 157 158 159 160 161 162 163 164
def LstmRecurrentLayerGroup(name,
                            size,
                            active_type,
                            state_active_type,
                            gate_active_type,
                            inputs,
                            para_prefix=None,
                            error_clipping_threshold=0,
                            seq_reversed=False):
Z
zhangjinchao01 已提交
165 166 167

    input_layer_name = name + "_" + "transform_input"
    Layer(
Q
qijun 已提交
168 169 170 171 172 173 174 175 176 177 178 179
        name=input_layer_name,
        type="mixed",
        size=size * 4,
        active_type="",
        bias=False,
        inputs=inputs, )

    RecurrentLayerGroupBegin(
        name + "_layer_group",
        in_links=[input_layer_name],
        out_links=[name],
        seq_reversed=seq_reversed)
Z
zhangjinchao01 已提交
180 181

    LstmRecurrentUnit(
Q
qijun 已提交
182 183 184 185 186 187 188 189
        name=name,
        size=size,
        active_type=active_type,
        state_active_type=state_active_type,
        gate_active_type=gate_active_type,
        inputs=[IdentityProjection(input_layer_name)],
        para_prefix=para_prefix,
        error_clipping_threshold=error_clipping_threshold, )
Z
zhangjinchao01 已提交
190 191 192 193 194 195 196 197 198 199 200

    RecurrentLayerGroupEnd(name + "_layer_group")


# gated recurrent unit, can be used in recurrent machine
# *inputs* should be a list of Projections, for example:
#   inputs = [FullMatrixProjection("input_layer_name")],
# *para_prefix* defines parameter names, if the *para_prefix* of 
#   two GatedRecurrentUnit is same, they share same parameters
# *out_memory* can be defined outside if it's used outside

Q
qijun 已提交
201 202 203 204 205 206 207 208 209 210

def GatedRecurrentUnit(name,
                       size,
                       active_type,
                       gate_active_type,
                       inputs,
                       para_prefix=None,
                       error_clipping_threshold=0,
                       out_memory=None):
    if type_of(inputs) == str:  #only used by GatedRecurrentLayerGroup
Z
zhangjinchao01 已提交
211 212 213 214
        input_layer_name = inputs
    else:
        input_layer_name = name + "_" + "transform_input"
        Layer(
Q
qijun 已提交
215 216 217 218 219 220 221 222
            name=input_layer_name,
            type="mixed",
            size=size * 3,
            active_type="",
            bias=False,
            inputs=inputs, )

    if para_prefix is None:
Z
zhangjinchao01 已提交
223 224
        para_prefix = name
    if out_memory is None:
Q
qijun 已提交
225
        out_memory = Memory(name=name, size=size)
Z
zhangjinchao01 已提交
226 227

    GruStepLayer(
Q
qijun 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        name=name,
        size=size,
        bias=Bias(parameter_name=para_prefix + "_gate.b"),
        inputs=[
            input_layer_name, Input(
                out_memory, parameter_name=para_prefix + "_gate.w")
        ],
        active_type=active_type,
        active_gate_type=gate_active_type, )


def GatedRecurrentUnitNaive(name,
                            size,
                            active_type,
                            gate_active_type,
                            inputs,
                            para_prefix=None,
                            error_clipping_threshold=0,
                            out_memory=None):

    if type_of(inputs) == str:  #only used by GatedRecurrentLayerGroup
Z
zhangjinchao01 已提交
249 250 251 252
        input_layer_name = inputs
    else:
        input_layer_name = name + "_" + "transform_input"
        Layer(
Q
qijun 已提交
253 254 255 256 257 258 259 260
            name=input_layer_name,
            type="mixed",
            size=size * 3,
            active_type="",
            bias=False,
            inputs=inputs, )

    if para_prefix is None:
Z
zhangjinchao01 已提交
261 262
        para_prefix = name
    if out_memory is None:
Q
qijun 已提交
263
        out_memory = Memory(name=name, size=size)
Z
zhangjinchao01 已提交
264 265

    Layer(
Q
qijun 已提交
266 267 268 269 270 271 272 273 274 275 276 277
        name=name + "_" + "update_gate",
        type="mixed",
        size=size,
        active_type=gate_active_type,
        error_clipping_threshold=error_clipping_threshold,
        bias=Bias(
            initial_std=0, parameter_name=para_prefix + "_update_gate.b"),
        inputs=[
            IdentityOffsetProjection(
                input_layer_name, offset=0), FullMatrixProjection(
                    out_memory, parameter_name=para_prefix + "_update_gate.w")
        ], )
Z
zhangjinchao01 已提交
278
    Layer(
Q
qijun 已提交
279 280 281 282 283 284 285 286 287 288 289 290
        name=name + "_" + "reset_gate",
        type="mixed",
        size=size,
        active_type=gate_active_type,
        error_clipping_threshold=error_clipping_threshold,
        bias=Bias(
            initial_std=0, parameter_name=para_prefix + "_reset_gate.b"),
        inputs=[
            IdentityOffsetProjection(
                input_layer_name, offset=size), FullMatrixProjection(
                    out_memory, parameter_name=para_prefix + "_reset_gate.w")
        ], )
Z
zhangjinchao01 已提交
291
    ExpressionLayer(
Q
qijun 已提交
292 293
        name=name + "_" + "reset_output",
        inputs=DotMulOperator([out_memory, name + "_" + "reset_gate"]), )
Z
zhangjinchao01 已提交
294
    Layer(
Q
qijun 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
        name=name + "_" + "output_candidate",
        type="mixed",
        size=size,
        active_type=active_type,
        error_clipping_threshold=error_clipping_threshold,
        bias=Bias(
            initial_std=0, parameter_name=para_prefix + "_output_candidate.b"),
        inputs=[
            IdentityOffsetProjection(
                input_layer_name, offset=size * 2), FullMatrixProjection(
                    name + "_" + "reset_output",
                    parameter_name=para_prefix + "_output_candidate.w")
        ], )
    ExpressionLayer(  #element-wise interpolation
        name=name,
        inputs=[
            IdentityProjection(out_memory),
            DotMulOperator(
                [out_memory, name + "_" + "update_gate"], scale=-1.0),
            DotMulOperator(
                [name + "_" + "output_candidate", name + "_" + "update_gate"]),
        ], )

Z
zhangjinchao01 已提交
318 319 320

# like GatedRecurrentUnit, but it's a layer group.
# it is equivalent to GatedRecurrentLayer.
Q
qijun 已提交
321 322 323 324 325 326 327 328
def GatedRecurrentLayerGroup(name,
                             size,
                             active_type,
                             gate_active_type,
                             inputs,
                             para_prefix=None,
                             error_clipping_threshold=0,
                             seq_reversed=False):
Z
zhangjinchao01 已提交
329 330 331

    input_layer_name = name + "_" + "transform_input"
    Layer(
Q
qijun 已提交
332 333 334 335 336 337 338 339 340 341 342 343
        name=input_layer_name,
        type="mixed",
        size=size * 3,
        active_type="",
        bias=False,
        inputs=inputs, )

    RecurrentLayerGroupBegin(
        name + "_layer_group",
        in_links=[input_layer_name],
        out_links=[name],
        seq_reversed=seq_reversed)
Z
zhangjinchao01 已提交
344 345

    GatedRecurrentUnit(
Q
qijun 已提交
346 347 348 349 350 351 352
        name=name,
        size=size,
        active_type=active_type,
        gate_active_type=gate_active_type,
        inputs=input_layer_name,  #transform outside
        para_prefix=para_prefix,
        error_clipping_threshold=error_clipping_threshold, )
Z
zhangjinchao01 已提交
353 354

    RecurrentLayerGroupEnd(name + "_layer_group")