Exemplo n.º 1
0
    def __init__(
        self,
        task: str = None,
        load_checkpoint: str = None,
        label_map: Dict = None,
        num_classes: int = 2,
        **kwargs,
    ):
        super(ErnieV2, self).__init__()
        if label_map:
            self.label_map = label_map
            self.num_classes = len(label_map)
        else:
            self.num_classes = num_classes

        if task == 'sequence_classification':
            task = 'seq-cls'
            logger.warning(
                "current task name 'sequence_classification' was renamed to 'seq-cls', "
                "'sequence_classification' has been deprecated and will be removed in the future.",
            )
        if task == 'seq-cls':
            self.model = ErnieForSequenceClassification.from_pretrained(
                pretrained_model_name_or_path='ernie-2.0-en',
                num_classes=self.num_classes,
                **kwargs)
            self.criterion = paddle.nn.loss.CrossEntropyLoss()
            self.metric = paddle.metric.Accuracy()
        elif task == 'token-cls':
            self.model = ErnieForTokenClassification.from_pretrained(
                pretrained_model_name_or_path='ernie-2.0-en',
                num_classes=self.num_classes,
                **kwargs)
            self.criterion = paddle.nn.loss.CrossEntropyLoss()
            self.metric = ChunkEvaluator(label_list=[
                self.label_map[i] for i in sorted(self.label_map.keys())
            ])
        elif task == 'text-matching':
            self.model = ErnieModel.from_pretrained(
                pretrained_model_name_or_path='ernie-2.0-en', **kwargs)
            self.dropout = paddle.nn.Dropout(0.1)
            self.classifier = paddle.nn.Linear(
                self.model.config['hidden_size'] * 3, 2)
            self.criterion = paddle.nn.loss.CrossEntropyLoss()
            self.metric = paddle.metric.Accuracy()
        elif task is None:
            self.model = ErnieModel.from_pretrained(
                pretrained_model_name_or_path='ernie-2.0-en', **kwargs)
        else:
            raise RuntimeError(
                "Unknown task {}, task should be one in {}".format(
                    task, self._tasks_supported))

        self.task = task

        if load_checkpoint is not None and os.path.isfile(load_checkpoint):
            state_dict = paddle.load(load_checkpoint)
            self.set_state_dict(state_dict)
            logger.info('Loaded parameters from %s' %
                        os.path.abspath(load_checkpoint))
Exemplo n.º 2
0
    def __init__(self, config: CNNConfig, name_scope=None, dtype="float32"):
        super().__init__(name_scope, dtype)
        self.pretrained_model = ErnieModel.from_pretrained(
            config.pretrained_model)
        self.encoder = CNNEncoder(emb_dim=768,
                                  num_filter=config.num_filter,
                                  ngram_filter_sizes=config.filter_sizes,
                                  conv_layer_activation=get_activation(
                                      config.activation))
        self.dropout = nn.Dropout(p=config.dropout)

        self.classifier = nn.Linear(in_features=self.encoder.get_output_dim(),
                                    out_features=config.num_labels)
Exemplo n.º 3
0
    def __init__(
        self,
        task=None,
        load_checkpoint=None,
        label_map=None,
        num_classes=2,
        **kwargs,
    ):
        super(ErnieTiny, self).__init__()
        if label_map:
            self.num_classes = len(label_map)
        else:
            self.num_classes = num_classes

        if task == 'sequence_classification':
            task = 'seq-cls'
            logger.warning(
                "current task name 'sequence_classification' was renamed to 'seq-cls', "
                "'sequence_classification' has been deprecated and will be removed the future.",
            )
        if task == 'seq-cls':
            self.model = ErnieForSequenceClassification.from_pretrained(
                pretrained_model_name_or_path='ernie-tiny',
                num_classes=self.num_classes,
                **kwargs)
            self.criterion = paddle.nn.loss.CrossEntropyLoss()
            self.metric = paddle.metric.Accuracy()
        elif task == 'token-cls':
            self.model = ErnieForTokenClassification.from_pretrained(
                pretrained_model_name_or_path='ernie-tiny',
                num_classes=self.num_classes,
                **kwargs)
            self.criterion = paddle.nn.loss.CrossEntropyLoss()
            self.metric = paddle.metric.Accuracy()
        elif task is None:
            self.model = ErnieModel.from_pretrained(
                pretrained_model_name_or_path='ernie-tiny', **kwargs)
        else:
            raise RuntimeError(
                "Unknown task {}, task should be one in {}".format(
                    task, self._tasks_supported))

        self.task = task
        self.label_map = label_map

        if load_checkpoint is not None and os.path.isfile(load_checkpoint):
            state_dict = paddle.load(load_checkpoint)
            self.set_state_dict(state_dict)
            logger.info('Loaded parameters from %s' %
                        os.path.abspath(load_checkpoint))
Exemplo n.º 4
0
    def __init__(self, config: RNNConfig, name_scope=None, dtype="float32"):
        super().__init__(name_scope, dtype)
        self.pretrained_model = ErnieModel.from_pretrained(
            config.pretrained_model)
        self.encoder = RNNEncoder(
            input_size=768,
            hidden_size=config.hidden_size,
            num_layers=config.num_layers,
            direction='bidirect' if config.bidirection else 'forward',
            dropout=config.dropout,
            pooling_type=config.pooling_type)
        self.dropout = nn.Dropout(p=config.dropout)

        self.classifier = nn.Linear(in_features=self.encoder.get_output_dim(),
                                    out_features=config.num_labels)
        self.config = config