mindformers.trainer.token_classification.token_classification 源代码

# Copyright 2023 Huawei Technologies Co., Ltd
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
# ============================================================================
"""Token Classification Trainer."""
from typing import Optional, List, Union
from mindspore.train import Callback
from mindspore.dataset import GeneratorDataset
from mindspore.nn import TrainOneStepCell, Optimizer, Cell

from mindformers.dataset import BaseDataset
from mindformers.models import BaseModel, BaseTokenizer
from mindformers.tools.logger import logger
from mindformers.tools.register import MindFormerRegister, \
    MindFormerModuleType, MindFormerConfig
from ..base_trainer import BaseTrainer
from ..config_args import ConfigArguments
from ..training_args import TrainingArguments
from ...dataset.labels import cluener_labels


[文档]@MindFormerRegister.register(MindFormerModuleType.TRAINER) class TokenClassificationTrainer(BaseTrainer): r"""TokenClassification Task For Trainer. Args: model_name (str): The model name of Task-Trainer. Default: None Examples: >>> import numpy as np >>> from mindspore.dataset import GeneratorDataset >>> from mindspore.nn import AdamWeightDecay, TrainOneStepCell >>> from mindformers.core.lr import build_lr >>> from mindformers.trainer import TokenClassificationTrainer >>> from mindformers.tools.register import MindFormerConfig >>> from mindformers.models import BertForTokenClassification, BertConfig >>> config = MindFormerConfig("configs/tokcls/run_tokcls_bert_base_chinese.yaml") >>> #1) use config to train or evaluate or predict >>> tokcls_task = TokenClassificationTrainer(model_name='tokcls_bert_base_chinese') >>> tokcls_task.train(config=config) >>> tokcls_task.evaluate(config=config) >>> input_data = ["表身刻有代表日内瓦钟表匠freresoltramare的“fo”字样。", "的时间会去玩玩星际2。"] >>> res = tokcls_task.predict(input_data=input_data) >>> print(res) [[{'entity_group': 'address', 'word': '日内瓦', 'start': 6, 'end': 9}], [{'entity_group': 'game', 'word': '星际2', 'start': 7, 'end': 10}]] >>> #2) use instance function to train or evaluate or predict Raises: NotImplementedError: If train method or evaluate method or predict method not implemented. """ def __init__(self, model_name: str = None): super(TokenClassificationTrainer, self).__init__("token_classification", model_name)
[文档] def train(self, config: Optional[Union[dict, MindFormerConfig, ConfigArguments, TrainingArguments]] = None, network: Optional[Union[Cell, BaseModel]] = None, dataset: Optional[Union[BaseDataset, GeneratorDataset]] = None, wrapper: Optional[TrainOneStepCell] = None, optimizer: Optional[Optimizer] = None, callbacks: Optional[Union[Callback, List[Callback]]] = None, **kwargs): r"""Train task for TokenClassification Trainer. This function is used to train or fine-tune the network. The trainer interface is used to quickly start training for general task. It also allows users to customize the network, optimizer, dataset, wrapper, callback. Args: config (Optional[Union[dict, MindFormerConfig, ConfigArguments, TrainingArguments]]): The task config which is used to configure the dataset, the hyper-parameter, optimizer, etc. It supports config dict or MindFormerConfig or TrainingArguments or ConfigArguments class. Default: None. network (Optional[Union[Cell, BaseModel]]): The network for trainer. It supports model name or BaseModel or MindSpore Cell class. Default: None. dataset (Optional[Union[BaseDataset, GeneratorDataset]]): The training dataset. It support real dataset path or BaseDateset class or MindSpore Dataset class. Default: None. optimizer (Optional[Optimizer]): The training network's optimizer. It support Optimizer class of MindSpore. Default: None. wrapper (Optional[TrainOneStepCell]): Wraps the `network` with the `optimizer`. It support TrainOneStepCell class of MindSpore. Default: None. callbacks (Optional[Union[Callback, List[Callback]]]): The training callback function. It support CallBack or CallBack List of MindSpore. Default: None. Raises: NotImplementedError: If wrapper not implemented. """ self.training_process( config=config, network=network, callbacks=callbacks, dataset=dataset, wrapper=wrapper, optimizer=optimizer, **kwargs)
[文档] def evaluate(self, config: Optional[Union[dict, MindFormerConfig, ConfigArguments, TrainingArguments]] = None, network: Optional[Union[Cell, BaseModel]] = None, dataset: Optional[Union[BaseDataset, GeneratorDataset]] = None, callbacks: Optional[Union[Callback, List[Callback]]] = None, compute_metrics: Optional[Union[dict, set]] = None, **kwargs): r"""Evaluate task for TokenClassification Trainer. This function is used to evaluate the network. The trainer interface is used to quickly start training for general task. It also allows users to customize the network, dataset, callbacks, compute_metrics. Args: config (Optional[Union[dict, MindFormerConfig, ConfigArguments, TrainingArguments]]): The task config which is used to configure the dataset, the hyper-parameter, optimizer, etc. It supports config dict or MindFormerConfig or TrainingArguments or ConfigArguments class. Default: None. network (Optional[Union[Cell, BaseModel]]): The network for trainer. It supports model name or BaseModel or MindSpore Cell class. Default: None. dataset (Optional[Union[BaseDataset]]): The evaluate dataset. It support real dataset path or BaseDateset class or MindSpore Dataset class. Default: None. callbacks (Optional[Union[Callback, List[Callback]]]): The eval callback function. It support CallBack or CallBack List of MindSpore. Default: None. compute_metrics (Optional[Union[dict, set]]): The metric of evaluating. It support dict or set in MindSpore's Metric class. Default: None. """ metric_name = "Entity Metric" kwargs.setdefault("metric_name", metric_name) super().evaluate_process( config=config, network=network, dataset=dataset, compute_metrics=compute_metrics, callbacks=callbacks, **kwargs )
[文档] def predict(self, config: Optional[Union[dict, MindFormerConfig, ConfigArguments, TrainingArguments]] = None, input_data: Optional[Union[str, list]] = None, network: Optional[Union[Cell, BaseModel]] = None, tokenizer: Optional[BaseTokenizer] = None, **kwargs): """ Executes the predict of the trainer. Args: config (Optional[Union[dict, MindFormerConfig, ConfigArguments, TrainingArguments]]): The task config which is used to configure the dataset, the hyper-parameter, optimizer, etc. It supports config dict or MindFormerConfig or TrainingArguments or ConfigArguments class. Default: None. input_data (Optional[Union[Tensor, str, list]]): The predict data. Default: None. network (Optional[Union[Cell, BaseModel]]): The network for trainer. It supports model name or BaseModel or MindSpore Cell class. Default: None. tokenizer (Optional[BaseTokenizer]): The tokenizer for tokenizing the input text. Default: None. Returns: A list of prediction. """ config = self.set_config(config) logger.info(".........Build Input Data For Predict..........") if input_data is None: input_data = config.input_data if not isinstance(input_data, (str, list)): raise ValueError("Input data's type must be one of [str, list]") if isinstance(input_data, list): for item in input_data: if not isinstance(item, str): raise ValueError("The element of input data list must be str") # This is a known issue, you need to specify batch size equal to 1 when creating bert model. config.model.model_config.batch_size = 1 max_length = network.config.seq_length if network else config.model.model_config.seq_length id2label = {label_id: label for label_id, label in enumerate(cluener_labels)} return self.predict_process(config=config, input_data=input_data, task='token_classification', network=network, tokenizer=tokenizer, max_length=max_length, padding="max_length", id2label=id2label, **kwargs)