token_classification.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
  3. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  4. # All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from modelscope.metainfo import Heads, Models
  18. from modelscope.models.builder import MODELS
  19. from modelscope.models.nlp.task_models import (
  20. ModelForTokenClassification, ModelForTokenClassificationWithCRF)
  21. from modelscope.utils import logger as logging
  22. from modelscope.utils.constant import Tasks
  23. logger = logging.get_logger()
  24. @MODELS.register_module(Tasks.token_classification, module_name=Models.lcrf)
  25. @MODELS.register_module(
  26. Tasks.named_entity_recognition, module_name=Models.lcrf)
  27. @MODELS.register_module(Tasks.part_of_speech, module_name=Models.lcrf)
  28. @MODELS.register_module(Tasks.word_segmentation, module_name=Models.lcrf)
  29. @MODELS.register_module(Tasks.word_segmentation, module_name=Models.lcrf_wseg)
  30. class LSTMForTokenClassificationWithCRF(ModelForTokenClassificationWithCRF):
  31. r"""Model with a token classification head on top (a linear layer on top of
  32. the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks, word-segmentation.
  33. """
  34. override_base_model_type = True
  35. base_model_type = Models.lstm
  36. head_type = Heads.lstm_crf
  37. def parse_head_cfg(self):
  38. head_cfg = super(ModelForTokenClassification, self).parse_head_cfg()
  39. head_cfg['hidden_size'] = (
  40. head_cfg.hidden_size
  41. if hasattr(head_cfg, 'hidden_size') else head_cfg.lstm_hidden_size)
  42. head_cfg['num_labels'] = self.config.num_labels
  43. return head_cfg