shared_lstm.py 1.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

Y
Yu Yang 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
from paddle.trainer_config_helpers import *

settings(learning_rate=1e-4, batch_size=1000)

data_1 = data_layer(name='data_a', size=100)
data_2 = data_layer(name='data_b', size=100)

mixed_param = ParamAttr(name='mixed_param')

with mixed_layer(size=400, bias_attr=False) as m1:
    m1 += full_matrix_projection(input=data_1, param_attr=mixed_param)

with mixed_layer(size=400, bias_attr=False) as m2:
    m2 += full_matrix_projection(input=data_2, param_attr=mixed_param)

lstm_param = ParamAttr(name='lstm_param')
lstm_bias = ParamAttr(name='lstm_bias', initial_mean=0., initial_std=0.)

Q
qijun 已提交
33 34 35 36
lstm1 = lstmemory_group(
    input=m1,
    param_attr=lstm_param,
    lstm_bias_attr=lstm_bias,
37 38
    input_proj_bias_attr=False)

Q
qijun 已提交
39 40 41 42
lstm2 = lstmemory_group(
    input=m2,
    param_attr=lstm_param,
    lstm_bias_attr=lstm_bias,
43
    input_proj_bias_attr=False)
Y
Yu Yang 已提交
44 45 46

softmax_param = ParamAttr(name='softmax_param')

Q
qijun 已提交
47 48 49 50 51 52 53 54 55 56
predict = fc_layer(
    input=[last_seq(input=lstm1), last_seq(input=lstm2)],
    size=10,
    param_attr=[softmax_param, softmax_param],
    bias_attr=False,
    act=SoftmaxActivation())
outputs(
    classification_cost(
        input=predict, label=data_layer(
            name='label', size=10)))