token_classification.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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.token_classification 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.bert)
  25. @MODELS.register_module(Tasks.part_of_speech, module_name=Models.bert)
  26. @MODELS.register_module(Tasks.word_segmentation, module_name=Models.bert)
  27. class BertForTokenClassification(ModelForTokenClassification):
  28. r"""Bert Model with a token classification head on top (a linear layer on top of
  29. the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks, word-segmentation.
  30. This model inherits from :class:`TokenClassificationModel`. Check the superclass documentation for the generic
  31. methods the library implements for all its model (such as downloading or saving, resizing the input embeddings,
  32. pruning heads etc.)
  33. This model is also a PyTorch `torch.nn.Module <https://pytorch.org/docs/stable/nn.html#torch.nn.Module>`__
  34. subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to
  35. general usage and behavior.
  36. """
  37. base_model_type = 'bert'