creator.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding: UTF-8 -*-
  2. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. Define the function to create lexical analysis model and model's data reader
  17. """
  18. import sys
  19. import os
  20. import math
  21. import paddle
  22. import paddle.fluid as fluid
  23. from paddle.fluid.initializer import NormalInitializer
  24. import jieba.lac_small.nets as nets
  25. def create_model(vocab_size, num_labels, mode='train'):
  26. """create lac model"""
  27. # model's input data
  28. words = fluid.data(name='words', shape=[-1, 1], dtype='int64', lod_level=1)
  29. targets = fluid.data(
  30. name='targets', shape=[-1, 1], dtype='int64', lod_level=1)
  31. # for inference process
  32. if mode == 'infer':
  33. crf_decode = nets.lex_net(
  34. words, vocab_size, num_labels, for_infer=True, target=None)
  35. return {
  36. "feed_list": [words],
  37. "words": words,
  38. "crf_decode": crf_decode,
  39. }
  40. return ret