recurrent_units.py 12.1 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
# recurrent_units.py
# Version 2.0
#
18
# Some recurrent units can be used in recurrent layer group,
Z
zhangjinchao01 已提交
19
#   to use these units, import this module in your config_file:
20 21
#     import trainer.recurrent_units
#
W
wangyang59 已提交
22 23 24
# The modules in this file are DEPRECATED.
# If you would like to use lstm/gru
# please use the functions defined in paddle.trainer_config_helpers.
Z
zhangjinchao01 已提交
25 26 27

from paddle.trainer.config_parser import *

Q
qijun 已提交
28

Z
zhangjinchao01 已提交
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")],
32
# *para_prefix* defines parameter names, if the *para_prefix* of
Z
zhangjinchao01 已提交
33 34
#   two LstmRecurrentUnit is same, they share same parameters
# *out_memory* can be defined outside if it's used outside
Q
qijun 已提交
35 36 37 38 39 40 41 42 43
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 已提交
44

Q
qijun 已提交
45
    if para_prefix is None:
Z
zhangjinchao01 已提交
46 47
        para_prefix = name
    if out_memory is None:
Q
qijun 已提交
48 49 50
        out_memory = Memory(name=name, size=size)

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

    Layer(
Q
qijun 已提交
53 54 55 56 57 58 59 60 61 62
        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 已提交
63
    LstmStepLayer(
Q
qijun 已提交
64 65 66 67 68 69 70
        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 已提交
71
    GetOutputLayer(
Q
qijun 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        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 已提交
89 90
        para_prefix = name
    if out_memory is None:
Q
qijun 已提交
91 92 93
        out_memory = Memory(name=name, size=size)

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

    Layer(
Q
qijun 已提交
96 97 98 99 100 101 102 103 104 105
        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 已提交
106
    ExpressionLayer(
Q
qijun 已提交
107 108 109 110 111 112 113
        name=name + "_" + "input_s",
        size=size,
        active_type=active_type,
        inputs=[
            IdentityOffsetProjection(
                name + "_" + "input_recurrent", offset=0)
        ], )
Z
zhangjinchao01 已提交
114
    ExpressionLayer(
Q
qijun 已提交
115 116 117 118 119 120 121
        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 已提交
122
    ExpressionLayer(
Q
qijun 已提交
123 124 125 126 127 128 129 130
        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 已提交
131
    ExpressionLayer(
Q
qijun 已提交
132 133 134 135 136
        name=name + "_" + "state",
        inputs=[
            DotMulOperator([name + "_" + "input_s", name + "_" + "input_gate"]),
            DotMulOperator([state_memory, name + "_" + "forget_gate"]),
        ], )
Z
zhangjinchao01 已提交
137
    ExpressionLayer(
Q
qijun 已提交
138 139 140 141 142 143 144 145 146
        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 已提交
147
    ExpressionLayer(
Q
qijun 已提交
148 149 150
        name=name + "_" + "state_atv",
        active_type=state_active_type,
        inputs=IdentityProjection(name + "_" + "state"), )
Z
zhangjinchao01 已提交
151
    ExpressionLayer(
Q
qijun 已提交
152 153 154 155
        name=name,
        inputs=DotMulOperator(
            [name + "_" + "state_atv", name + "_" + "output_gate"]), )

Z
zhangjinchao01 已提交
156 157 158

# like LstmRecurrentUnit, but it's a layer group.
# it is equivalent to LstmLayer
Q
qijun 已提交
159 160 161 162 163 164 165 166 167
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 已提交
168 169 170

    input_layer_name = name + "_" + "transform_input"
    Layer(
Q
qijun 已提交
171 172 173 174 175 176 177 178 179 180 181 182
        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 已提交
183 184

    LstmRecurrentUnit(
Q
qijun 已提交
185 186 187 188 189 190 191 192
        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 已提交
193 194 195 196 197 198 199

    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")],
200
# *para_prefix* defines parameter names, if the *para_prefix* of
Z
zhangjinchao01 已提交
201 202 203
#   two GatedRecurrentUnit is same, they share same parameters
# *out_memory* can be defined outside if it's used outside

Q
qijun 已提交
204 205 206 207 208 209 210 211 212 213

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 已提交
214 215 216 217
        input_layer_name = inputs
    else:
        input_layer_name = name + "_" + "transform_input"
        Layer(
Q
qijun 已提交
218 219 220 221 222 223 224 225
            name=input_layer_name,
            type="mixed",
            size=size * 3,
            active_type="",
            bias=False,
            inputs=inputs, )

    if para_prefix is None:
Z
zhangjinchao01 已提交
226 227
        para_prefix = name
    if out_memory is None:
Q
qijun 已提交
228
        out_memory = Memory(name=name, size=size)
Z
zhangjinchao01 已提交
229 230

    GruStepLayer(
Q
qijun 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        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 已提交
252 253 254 255
        input_layer_name = inputs
    else:
        input_layer_name = name + "_" + "transform_input"
        Layer(
Q
qijun 已提交
256 257 258 259 260 261 262 263
            name=input_layer_name,
            type="mixed",
            size=size * 3,
            active_type="",
            bias=False,
            inputs=inputs, )

    if para_prefix is None:
Z
zhangjinchao01 已提交
264 265
        para_prefix = name
    if out_memory is None:
Q
qijun 已提交
266
        out_memory = Memory(name=name, size=size)
Z
zhangjinchao01 已提交
267 268

    Layer(
Q
qijun 已提交
269 270 271 272 273 274 275 276 277 278 279 280
        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 已提交
281
    Layer(
Q
qijun 已提交
282 283 284 285 286 287 288 289 290 291 292 293
        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 已提交
294
    ExpressionLayer(
Q
qijun 已提交
295 296
        name=name + "_" + "reset_output",
        inputs=DotMulOperator([out_memory, name + "_" + "reset_gate"]), )
Z
zhangjinchao01 已提交
297
    Layer(
Q
qijun 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        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 已提交
321 322 323

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

    input_layer_name = name + "_" + "transform_input"
    Layer(
Q
qijun 已提交
335 336 337 338 339 340 341 342 343 344 345 346
        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 已提交
347 348

    GatedRecurrentUnit(
Q
qijun 已提交
349 350 351 352 353 354 355
        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 已提交
356 357

    RecurrentLayerGroupEnd(name + "_layer_group")