当前位置: 首页>>代码示例>>Python>>正文


Python BertForSequenceClassification.from_pretrained方法代码示例

本文整理汇总了Python中transformers.BertForSequenceClassification.from_pretrained方法的典型用法代码示例。如果您正苦于以下问题:Python BertForSequenceClassification.from_pretrained方法的具体用法?Python BertForSequenceClassification.from_pretrained怎么用?Python BertForSequenceClassification.from_pretrained使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在transformers.BertForSequenceClassification的用法示例。


在下文中一共展示了BertForSequenceClassification.from_pretrained方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: set_classifier

# 需要导入模块: from transformers import BertForSequenceClassification [as 别名]
# 或者: from transformers.BertForSequenceClassification import from_pretrained [as 别名]
def set_classifier(self, classifier_type=CLASSIFIER_TYPE_RNN, classifier=None):
        """ Set the classifier from prepackaged option or provide custom classifier

        :param classifier_type: One of ['BERT', 'RNN', 'BERT_RNN']
        :type: str
        :param classifier: Custom provided classifier
        :type: Any
        """
        if classifier_type == CLASSIFIER_TYPE_RNN or classifier_type == CLASSIFIER_TYPE_BERT_RNN:
            self.classifier = ClassifierModule(self.model_config, self.preprocessor.word_vocab)
        elif classifier_type == CLASSIFIER_TYPE_BERT:
            self.classifier = BertForSequenceClassification.from_pretrained(
                "bert-base-uncased",
                num_labels=self.model_config.num_labels,
                output_hidden_states=False,
                output_attentions=False,
            )
        else:
            self.classifier = classifier 
开发者ID:interpretml,项目名称:interpret-text,代码行数:21,代码来源:explainer.py

示例2: set_anti_classifier

# 需要导入模块: from transformers import BertForSequenceClassification [as 别名]
# 或者: from transformers.BertForSequenceClassification import from_pretrained [as 别名]
def set_anti_classifier(self, classifier_type=CLASSIFIER_TYPE_RNN, anti_classifier=None):
        """ Set anti classifier from prepackaged option or provide custom anti classifier

        :param classifier_type: One of ['BERT', 'RNN', 'BERT_RNN']
        :type str
        :param anti_classifier: Custom provided anti classifier
        :type Any
        """
        if classifier_type == CLASSIFIER_TYPE_RNN or classifier_type == CLASSIFIER_TYPE_BERT_RNN:
            self.anti_classifier = ClassifierModule(self.model_config, self.preprocessor.word_vocab)

        elif classifier_type == CLASSIFIER_TYPE_BERT:
            self.anti_classifier = BertForSequenceClassification.from_pretrained(
                "bert-base-uncased",
                num_labels=self.model_config.num_labels,
                output_hidden_states=False,
                output_attentions=False,
            )
        else:
            self.anti_classifier = anti_classifier 
开发者ID:interpretml,项目名称:interpret-text,代码行数:22,代码来源:explainer.py

示例3: set_generator_classifier

# 需要导入模块: from transformers import BertForSequenceClassification [as 别名]
# 或者: from transformers.BertForSequenceClassification import from_pretrained [as 别名]
def set_generator_classifier(self, classifier_type=CLASSIFIER_TYPE_RNN, generator_classifier=None):
        """ Set classifier for the Generator

        :param classifier_type: One of ['BERT', 'RNN', 'BERT_RNN']
        :type classifier_type: str
        :param generator_classifier: Custom provided classifier for generator
        :type generator_classifier: Any
        :return: Any
        """
        if classifier_type == CLASSIFIER_TYPE_RNN:
            self.generator_classifier = ClassifierModule(self.model_config, self.preprocessor.word_vocab)
        elif classifier_type == CLASSIFIER_TYPE_BERT or classifier_type == CLASSIFIER_TYPE_BERT_RNN:
            self.generator_classifier = BertForSequenceClassification.from_pretrained(
                "bert-base-uncased",
                num_labels=self.model_config.num_labels,
                output_hidden_states=True,
                output_attentions=True,
            )
        else:
            self.generator_classifier = generator_classifier 
开发者ID:interpretml,项目名称:interpret-text,代码行数:22,代码来源:explainer.py

示例4: __init__

# 需要导入模块: from transformers import BertForSequenceClassification [as 别名]
# 或者: from transformers.BertForSequenceClassification import from_pretrained [as 别名]
def __init__(self, cache_dir=DEFAULT_CACHE_DIR, verbose=False):
        from transformers import BertTokenizer, BertForSequenceClassification

        # download the model or load the model path
        path_emotion = download_model('bert.emotion', cache_dir,
                                       process_func=_unzip_process_func,
                                       verbose=verbose)
        path_emotion = os.path.join(path_emotion,'bert.emotion')
        path_reject = download_model('bert.noemotion', cache_dir,
                                       process_func=_unzip_process_func,
                                       verbose=verbose)
        path_reject = os.path.join(path_reject,'bert.noemotion')
        # load the models
        self.tokenizer_rejct = BertTokenizer.from_pretrained(path_reject)
        self.model_reject = BertForSequenceClassification.from_pretrained(path_reject)
        
        self.tokenizer = BertTokenizer.from_pretrained(path_emotion)
        self.model = BertForSequenceClassification.from_pretrained(path_emotion)
        
        # load the class names mapping
        self.catagories = {5: 'Foragt/Modvilje', 2: 'Forventning/Interrese',
                           0: 'Glæde/Sindsro', 3: 'Overasket/Målløs',
                           1: 'Tillid/Accept',
                           4: 'Vrede/Irritation', 6: 'Sorg/trist',
                           7: 'Frygt/Bekymret'} 
开发者ID:alexandrainst,项目名称:danlp,代码行数:27,代码来源:bert_models.py

示例5: __init__

# 需要导入模块: from transformers import BertForSequenceClassification [as 别名]
# 或者: from transformers.BertForSequenceClassification import from_pretrained [as 别名]
def __init__(self, pretrain_path, max_length, cat_entity_rep=False): 
        nn.Module.__init__(self)
        self.bert = BertModel.from_pretrained(pretrain_path)
        self.max_length = max_length
        self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
        self.cat_entity_rep = cat_entity_rep 
开发者ID:thunlp,项目名称:FewRel,代码行数:8,代码来源:sentence_encoder.py


注:本文中的transformers.BertForSequenceClassification.from_pretrained方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。