test_label_semantic_roles.py 16.1 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
import contextlib
Q
Qiao Longfei 已提交
16
import numpy as np
Y
Yu Yang 已提交
17 18 19
import os
import time
import unittest
20
import tempfile
Y
Yu Yang 已提交
21

22 23
import paddle
import paddle.dataset.conll05 as conll05
24
import paddle.fluid as fluid
Q
Qiao Longfei 已提交
25

P
pangyoki 已提交
26 27
paddle.enable_static()

Q
Qiao Longfei 已提交
28 29 30
word_dict, verb_dict, label_dict = conll05.get_dict()
word_dict_len = len(word_dict)
label_dict_len = len(label_dict)
L
Liu Yiqun 已提交
31
pred_dict_len = len(verb_dict)
Q
Qiao Longfei 已提交
32 33 34 35 36 37 38 39 40

mark_dict_len = 2
word_dim = 32
mark_dim = 5
hidden_dim = 512
depth = 8
mix_hidden_lr = 1e-3

IS_SPARSE = True
41
PASS_NUM = 2
42
BATCH_SIZE = 10
Q
Qiao Longfei 已提交
43 44 45 46 47 48 49 50 51 52

embedding_name = 'emb'


def load_parameter(file_name, h, w):
    with open(file_name, 'rb') as f:
        f.read(16)  # skip header.
        return np.fromfile(f, dtype=np.float32).reshape(h, w)


Y
Yu Yang 已提交
53 54
def db_lstm(word, predicate, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2, mark,
            **ignored):
Q
Qiao Longfei 已提交
55
    # 8 features
56 57 58 59 60 61 62 63 64 65
    predicate_embedding = fluid.layers.embedding(input=predicate,
                                                 size=[pred_dict_len, word_dim],
                                                 dtype='float32',
                                                 is_sparse=IS_SPARSE,
                                                 param_attr='vemb')

    mark_embedding = fluid.layers.embedding(input=mark,
                                            size=[mark_dict_len, mark_dim],
                                            dtype='float32',
                                            is_sparse=IS_SPARSE)
Q
Qiao Longfei 已提交
66 67 68

    word_input = [word, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2]
    emb_layers = [
69 70 71 72 73
        fluid.layers.embedding(size=[word_dict_len, word_dim],
                               input=x,
                               param_attr=fluid.ParamAttr(name=embedding_name,
                                                          trainable=False))
        for x in word_input
Q
Qiao Longfei 已提交
74 75 76 77 78
    ]
    emb_layers.append(predicate_embedding)
    emb_layers.append(mark_embedding)

    hidden_0_layers = [
J
jshower 已提交
79
        fluid.layers.fc(input=emb, size=hidden_dim) for emb in emb_layers
Q
Qiao Longfei 已提交
80 81
    ]

82
    hidden_0 = fluid.layers.sums(input=hidden_0_layers)
Q
Qiao Longfei 已提交
83

84 85 86 87 88
    lstm_0 = fluid.layers.dynamic_lstm(input=hidden_0,
                                       size=hidden_dim,
                                       candidate_activation='relu',
                                       gate_activation='sigmoid',
                                       cell_activation='sigmoid')
Q
Qiao Longfei 已提交
89 90 91 92 93

    # stack L-LSTM and R-LSTM with direct edges
    input_tmp = [hidden_0, lstm_0]

    for i in range(1, depth):
94
        mix_hidden = fluid.layers.sums(input=[
J
jshower 已提交
95 96
            fluid.layers.fc(input=input_tmp[0], size=hidden_dim),
            fluid.layers.fc(input=input_tmp[1], size=hidden_dim)
Q
Qiao Longfei 已提交
97 98
        ])

99 100 101 102 103 104
        lstm = fluid.layers.dynamic_lstm(input=mix_hidden,
                                         size=hidden_dim,
                                         candidate_activation='relu',
                                         gate_activation='sigmoid',
                                         cell_activation='sigmoid',
                                         is_reverse=((i % 2) == 1))
Q
Qiao Longfei 已提交
105 106 107

        input_tmp = [mix_hidden, lstm]

108
    feature_out = fluid.layers.sums(input=[
109 110
        fluid.layers.fc(input=input_tmp[0], size=label_dict_len, act='tanh'),
        fluid.layers.fc(input=input_tmp[1], size=label_dict_len, act='tanh')
Q
Qiao Longfei 已提交
111 112 113 114 115
    ])

    return feature_out


武毅 已提交
116
def train(use_cuda, save_dirname=None, is_local=True):
Q
Qiao Longfei 已提交
117
    # define network topology
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    word = fluid.layers.data(name='word_data',
                             shape=[1],
                             dtype='int64',
                             lod_level=1)
    predicate = fluid.layers.data(name='verb_data',
                                  shape=[1],
                                  dtype='int64',
                                  lod_level=1)
    ctx_n2 = fluid.layers.data(name='ctx_n2_data',
                               shape=[1],
                               dtype='int64',
                               lod_level=1)
    ctx_n1 = fluid.layers.data(name='ctx_n1_data',
                               shape=[1],
                               dtype='int64',
                               lod_level=1)
    ctx_0 = fluid.layers.data(name='ctx_0_data',
                              shape=[1],
                              dtype='int64',
                              lod_level=1)
    ctx_p1 = fluid.layers.data(name='ctx_p1_data',
                               shape=[1],
                               dtype='int64',
                               lod_level=1)
    ctx_p2 = fluid.layers.data(name='ctx_p2_data',
                               shape=[1],
                               dtype='int64',
                               lod_level=1)
    mark = fluid.layers.data(name='mark_data',
                             shape=[1],
                             dtype='int64',
                             lod_level=1)
Y
Yu Yang 已提交
150
    feature_out = db_lstm(**locals())
151 152 153 154 155 156 157 158 159
    target = fluid.layers.data(name='target',
                               shape=[1],
                               dtype='int64',
                               lod_level=1)
    crf_cost = fluid.layers.linear_chain_crf(input=feature_out,
                                             label=target,
                                             param_attr=fluid.ParamAttr(
                                                 name='crfw',
                                                 learning_rate=mix_hidden_lr))
160
    avg_cost = paddle.mean(crf_cost)
Q
Qiao Longfei 已提交
161

Q
Qiao Longfei 已提交
162
    # TODO(qiao)
Q
Qiao Longfei 已提交
163
    # check other optimizers and check why out will be NAN
164
    sgd_optimizer = fluid.optimizer.SGD(
165 166 167 168
        learning_rate=fluid.layers.exponential_decay(learning_rate=0.01,
                                                     decay_steps=100000,
                                                     decay_rate=0.5,
                                                     staircase=True))
W
Wu Yi 已提交
169
    sgd_optimizer.minimize(avg_cost)
Q
Qiao Longfei 已提交
170

Q
Qiao Longfei 已提交
171 172 173
    # TODO(qiao)
    # add dependency track and move this config before optimizer
    crf_decode = fluid.layers.crf_decoding(
Q
Qiao Longfei 已提交
174 175
        input=feature_out, param_attr=fluid.ParamAttr(name='crfw'))

176 177 178
    train_data = paddle.batch(paddle.reader.shuffle(
        paddle.dataset.conll05.test(), buf_size=8192),
                              batch_size=BATCH_SIZE)
179 180

    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
181 182 183 184
    feeder = fluid.DataFeeder(feed_list=[
        word, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2, predicate, mark, target
    ],
                              place=place)
185
    exe = fluid.Executor(place)
Q
Qiao Longfei 已提交
186

武毅 已提交
187 188 189 190 191 192 193 194 195 196
    def train_loop(main_program):
        exe.run(fluid.default_startup_program())
        embedding_param = fluid.global_scope().find_var(
            embedding_name).get_tensor()
        embedding_param.set(
            load_parameter(conll05.get_embedding(), word_dict_len, word_dim),
            place)

        start_time = time.time()
        batch_id = 0
197
        for pass_id in range(PASS_NUM):
武毅 已提交
198
            for data in train_data():
199 200 201 202
                cost = exe.run(main_program,
                               feed=feeder.feed(data),
                               fetch_list=[avg_cost])
                cost = cost[0]
武毅 已提交
203 204

                if batch_id % 10 == 0:
205
                    print("avg_cost:" + str(cost))
武毅 已提交
206
                    if batch_id != 0:
207 208
                        print("second per batch: " +
                              str((time.time() - start_time) / batch_id))
武毅 已提交
209
                    # Set the threshold low to speed up the CI test
210
                    if float(cost) < 80.0:
武毅 已提交
211 212
                        if save_dirname is not None:
                            # TODO(liuyiqun): Change the target to crf_decode
213 214 215 216 217 218
                            fluid.io.save_inference_model(
                                save_dirname, [
                                    'word_data', 'verb_data', 'ctx_n2_data',
                                    'ctx_n1_data', 'ctx_0_data', 'ctx_p1_data',
                                    'ctx_p2_data', 'mark_data'
                                ], [feature_out], exe)
武毅 已提交
219 220 221 222
                        return

                batch_id = batch_id + 1

223 224 225 226
        raise RuntimeError(
            "This model should save_inference_model and return, but not reach here, please check!"
        )

武毅 已提交
227 228 229
    if is_local:
        train_loop(fluid.default_main_program())
    else:
G
gongweibao 已提交
230 231
        port = os.getenv("PADDLE_PSERVER_PORT", "6174")
        pserver_ips = os.getenv("PADDLE_PSERVER_IPS")  # ip,ip...
武毅 已提交
232 233 234 235
        eplist = []
        for ip in pserver_ips.split(","):
            eplist.append(':'.join([ip, port]))
        pserver_endpoints = ",".join(eplist)  # ip:port,ip:port...
G
gongweibao 已提交
236
        trainers = int(os.getenv("PADDLE_TRAINERS"))
武毅 已提交
237
        current_endpoint = os.getenv("POD_IP") + ":" + port
G
gongweibao 已提交
238 239
        trainer_id = int(os.getenv("PADDLE_TRAINER_ID"))
        training_role = os.getenv("PADDLE_TRAINING_ROLE", "TRAINER")
武毅 已提交
240
        t = fluid.DistributeTranspiler()
Y
Yancey1989 已提交
241
        t.transpile(trainer_id, pservers=pserver_endpoints, trainers=trainers)
武毅 已提交
242 243 244 245 246 247 248 249
        if training_role == "PSERVER":
            pserver_prog = t.get_pserver_program(current_endpoint)
            pserver_startup = t.get_startup_program(current_endpoint,
                                                    pserver_prog)
            exe.run(pserver_startup)
            exe.run(pserver_prog)
        elif training_role == "TRAINER":
            train_loop(t.get_trainer_program())
Q
Qiao Longfei 已提交
250 251


252 253 254 255 256 257 258
def infer(use_cuda, save_dirname=None):
    if save_dirname is None:
        return

    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
    exe = fluid.Executor(place)

259 260 261
    inference_scope = fluid.core.Scope()
    with fluid.scope_guard(inference_scope):
        # Use fluid.io.load_inference_model to obtain the inference program desc,
T
tianshuo78520a 已提交
262
        # the feed_target_names (the names of variables that will be fed
263 264 265 266 267
        # data using feed operators), and the fetch_targets (variables that
        # we want to obtain data from using fetch operators).
        [inference_program, feed_target_names,
         fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)

268
        # Setup input by creating LoDTensor to represent sequence of words.
269 270
        # Here each word is the basic element of the LoDTensor and the shape of
        # each word (base_shape) should be [1] since it is simply an index to
K
Kexin Zhao 已提交
271
        # look up for the corresponding word vector.
272
        # Suppose the recursive_sequence_lengths info is set to [[3, 4, 2]],
273 274 275 276
        # which has only one level of detail. Then the created LoDTensor will have only
        # one higher level structure (sequence of words, or sentence) than the basic
        # element (word). Hence the LoDTensor will hold data for three sentences of
        # length 3, 4 and 2, respectively.
277 278
        # Note that recursive_sequence_lengths should be a list of lists.
        recursive_seq_lens = [[3, 4, 2]]
K
Kexin Zhao 已提交
279
        base_shape = [1]
K
Kexin Zhao 已提交
280
        # The range of random integers is [low, high]
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        word = fluid.create_random_int_lodtensor(recursive_seq_lens,
                                                 base_shape,
                                                 place,
                                                 low=0,
                                                 high=word_dict_len - 1)
        pred = fluid.create_random_int_lodtensor(recursive_seq_lens,
                                                 base_shape,
                                                 place,
                                                 low=0,
                                                 high=pred_dict_len - 1)
        ctx_n2 = fluid.create_random_int_lodtensor(recursive_seq_lens,
                                                   base_shape,
                                                   place,
                                                   low=0,
                                                   high=word_dict_len - 1)
        ctx_n1 = fluid.create_random_int_lodtensor(recursive_seq_lens,
                                                   base_shape,
                                                   place,
                                                   low=0,
                                                   high=word_dict_len - 1)
        ctx_0 = fluid.create_random_int_lodtensor(recursive_seq_lens,
                                                  base_shape,
                                                  place,
                                                  low=0,
                                                  high=word_dict_len - 1)
        ctx_p1 = fluid.create_random_int_lodtensor(recursive_seq_lens,
                                                   base_shape,
                                                   place,
                                                   low=0,
                                                   high=word_dict_len - 1)
        ctx_p2 = fluid.create_random_int_lodtensor(recursive_seq_lens,
                                                   base_shape,
                                                   place,
                                                   low=0,
                                                   high=word_dict_len - 1)
        mark = fluid.create_random_int_lodtensor(recursive_seq_lens,
                                                 base_shape,
                                                 place,
                                                 low=0,
                                                 high=mark_dict_len - 1)
321 322 323 324 325 326 327 328 329 330 331 332

        # Construct feed as a dictionary of {feed_target_name: feed_target_data}
        # and results will contain a list of data corresponding to fetch_targets.
        assert feed_target_names[0] == 'word_data'
        assert feed_target_names[1] == 'verb_data'
        assert feed_target_names[2] == 'ctx_n2_data'
        assert feed_target_names[3] == 'ctx_n1_data'
        assert feed_target_names[4] == 'ctx_0_data'
        assert feed_target_names[5] == 'ctx_p1_data'
        assert feed_target_names[6] == 'ctx_p2_data'
        assert feed_target_names[7] == 'mark_data'

J
jshower 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345
        results = exe.run(inference_program,
                          feed={
                              feed_target_names[0]: word,
                              feed_target_names[1]: pred,
                              feed_target_names[2]: ctx_n2,
                              feed_target_names[3]: ctx_n1,
                              feed_target_names[4]: ctx_0,
                              feed_target_names[5]: ctx_p1,
                              feed_target_names[6]: ctx_p2,
                              feed_target_names[7]: mark
                          },
                          fetch_list=fetch_targets,
                          return_numpy=False)
346
        print(results[0].recursive_sequence_lengths())
347
        np_data = np.array(results[0])
348
        print("Inference Shape: ", np_data.shape)
349 350


武毅 已提交
351
def main(use_cuda, is_local=True):
352 353 354
    if use_cuda and not fluid.core.is_compiled_with_cuda():
        return

355
    temp_dir = tempfile.TemporaryDirectory()
356
    # Directory for saving the trained model
357 358
    save_dirname = os.path.join(temp_dir.name,
                                "label_semantic_roles.inference.model")
359

武毅 已提交
360
    train(use_cuda, save_dirname, is_local)
361 362
    infer(use_cuda, save_dirname)

363 364
    temp_dir.cleanup()

365 366

class TestLabelSemanticRoles(unittest.TestCase):
367

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    def test_cuda(self):
        with self.scope_prog_guard():
            main(use_cuda=True)

    def test_cpu(self):
        with self.scope_prog_guard():
            main(use_cuda=False)

    @contextlib.contextmanager
    def scope_prog_guard(self):
        prog = fluid.Program()
        startup_prog = fluid.Program()
        scope = fluid.core.Scope()
        with fluid.scope_guard(scope):
            with fluid.program_guard(prog, startup_prog):
                yield


Q
Qiao Longfei 已提交
386
if __name__ == '__main__':
387
    unittest.main()