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


Python BertModel.from_pretrained方法代码示例

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


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

示例1: __init__

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def __init__(self, trigger_size=None, entity_size=None, all_postags=None, postag_embedding_dim=50, argument_size=None, entity_embedding_dim=50, device=torch.device("cpu")):
        super().__init__()
        self.bert = BertModel.from_pretrained('bert-base-cased')
        self.entity_embed = MultiLabelEmbeddingLayer(num_embeddings=entity_size, embedding_dim=entity_embedding_dim, device=device)
        self.postag_embed = nn.Embedding(num_embeddings=all_postags, embedding_dim=postag_embedding_dim)
        self.rnn = nn.LSTM(bidirectional=True, num_layers=1, input_size=768 + entity_embedding_dim, hidden_size=768 // 2, batch_first=True)

        # hidden_size = 768 + entity_embedding_dim + postag_embedding_dim
        hidden_size = 768
        self.fc1 = nn.Sequential(
            # nn.Dropout(0.5),
            nn.Linear(hidden_size, hidden_size, bias=True),
            nn.ReLU(),
        )
        self.fc_trigger = nn.Sequential(
            nn.Linear(hidden_size, trigger_size),
        )
        self.fc_argument = nn.Sequential(
            nn.Linear(hidden_size * 2, argument_size),
        )
        self.device = device 
开发者ID:nlpcl-lab,项目名称:bert-event-extraction,代码行数:23,代码来源:model.py

示例2: eval_semantic_sim_score

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def eval_semantic_sim_score(instances: List[CFRInstance], bert_model_type="bert-base-uncased"):

    tokenizer = BertTokenizer.from_pretrained(bert_model_type)
    model = BertModel.from_pretrained(bert_model_type)
    model.eval()
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    model.to(device)

    drift_similarities = []

    for instance in instances:
        clean_original_story = _clean_text(instance.original_context + ' ' + instance.original_ending)
        predicted_ending = _clean_text(instance.cf_context + ' ' + instance.predicted_ending)

        original_story_emb = _bert_embed_sentence(clean_original_story, model, tokenizer)
        predicted_ending_emb = _bert_embed_sentence(predicted_ending, model, tokenizer)

        all_sims = []
        for gold_cf in instance.gold_cf_endings:
            clean_gold_cf = _clean_text(instance.cf_context + ' ' + gold_cf)
            gold_cf_emb = _bert_embed_sentence(clean_gold_cf, model, tokenizer)

            all_sims.append(drift_similarity(original_story_emb, predicted_ending_emb, gold_cf_emb))

        drift_similarities.append(np.max(all_sims))

    return {
        "drift_similarity": np.mean(drift_similarities),
        "drift_similarity_by_instance": [float(f) for f in  drift_similarities]
    } 
开发者ID:qkaren,项目名称:Counterfactual-StoryRW,代码行数:32,代码来源:evaluate.py

示例3: get_bert

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def get_bert(bert_model, bert_do_lower_case):
    # Avoid a hard dependency on BERT by only importing it if it's being used
    from pytorch_pretrained_bert import BertTokenizer, BertModel
    if bert_model.endswith('.tar.gz'):
        tokenizer = BertTokenizer.from_pretrained(bert_model.replace('.tar.gz', '-vocab.txt'), do_lower_case=bert_do_lower_case)
    else:
        tokenizer = BertTokenizer.from_pretrained(bert_model, do_lower_case=bert_do_lower_case)
    bert = BertModel.from_pretrained(bert_model)
    return tokenizer, bert

# %% 
开发者ID:nikitakit,项目名称:self-attentive-parser,代码行数:13,代码来源:parse_nk.py

示例4: __init__

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def __init__(self, top_rnns=False, vocab_size=None, device='cpu', finetuning=False):
        super().__init__()
        self.bert = BertModel.from_pretrained('bert-base-cased')

        self.top_rnns=top_rnns
        if top_rnns:
            self.rnn = nn.LSTM(bidirectional=True, num_layers=2, input_size=768, hidden_size=768//2, batch_first=True)
        self.fc = nn.Linear(768, vocab_size)

        self.device = device
        self.finetuning = finetuning 
开发者ID:woshiyyya,项目名称:DFGN-pytorch,代码行数:13,代码来源:model.py

示例5: __init__

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def __init__(self, bert_type_name='') -> None:
        super().__init__()
        self.bert_type_name = bert_type_name

        self.bert_tokenizer = BertTokenizer.from_pretrained(self.bert_type_name)

        self.bert_model: BertModel = BertModel.from_pretrained(self.bert_type_name)
        self.bert_model.eval() 
开发者ID:easonnie,项目名称:combine-FEVER-NSMN,代码行数:10,代码来源:bert_servant.py

示例6: build_model

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def build_model(self):
        """
        Construct the model.
        """
        num_classes = len(self.class_list)
        return BertWrapper(BertModel.from_pretrained(self.pretrained_path), num_classes) 
开发者ID:facebookresearch,项目名称:ParlAI,代码行数:8,代码来源:bert_classifier.py

示例7: load_bert

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def load_bert():
    # Load pre-trained model tokenizer (vocabulary)
    tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
    model = BertModel.from_pretrained('bert-base-cased')
    model.eval()
    model.to(device)
    return tokenizer, model 
开发者ID:allanj,项目名称:ner_with_dependency,代码行数:9,代码来源:prebert.py

示例8: build_model

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def build_model(self):
        """Construct the model."""
        num_classes = len(self.class_list)
        return BertWrapper(BertModel.from_pretrained(self.pretrained_path), num_classes) 
开发者ID:natashamjaques,项目名称:neural_chat,代码行数:6,代码来源:bert_classifier.py

示例9: __init__

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def __init__(self, bert_name_or_config, num_classes=6, hidden_size=768,
                 num_layers=3, dropout=0.1, dis_lambda=0.5, concat=False, anneal=False):
        super(DomainQA, self).__init__()
        if isinstance(bert_name_or_config, BertConfig):
            self.bert = BertModel(bert_name_or_config)
        else:
            self.bert = BertModel.from_pretrained("bert-base-uncased")

        self.config = self.bert.config

        self.qa_outputs = nn.Linear(hidden_size, 2)
        # init weight
        self.qa_outputs.weight.data.normal_(mean=0.0, std=0.02)
        self.qa_outputs.bias.data.zero_()
        if concat:
            input_size = 2 * hidden_size
        else:
            input_size = hidden_size
        self.discriminator = DomainDiscriminator(num_classes, input_size, hidden_size, num_layers, dropout)

        self.num_classes = num_classes
        self.dis_lambda = dis_lambda
        self.anneal = anneal
        self.concat = concat
        self.sep_id = 102

    # only for prediction 
开发者ID:seanie12,项目名称:mrqa,代码行数:29,代码来源:model.py

示例10: __init__

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def __init__(self, batch_size = 32):
        # Load pre-trained model tokenizer (vocabulary)
        self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
        self.max_seq_length = self.tokenizer.max_len
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.model = BertModel.from_pretrained('bert-base-uncased')
        self.batch_size = batch_size

    # def fit(self, X, y):
    #     # TODO: Find the right value for max sequence length
    #     return BertPretrainedEncoderImpl() 
开发者ID:IBM,项目名称:lale,代码行数:13,代码来源:bert_pretrained_encoder.py

示例11: __init__

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def __init__(self, temp_dir, load_pretrained_bert, bert_config):
        super(Bert, self).__init__()
        if(load_pretrained_bert):
            self.model = BertModel.from_pretrained('bert-base-uncased', cache_dir=temp_dir)
        else:
            self.model = BertModel(bert_config) 
开发者ID:nlpyang,项目名称:BertSum,代码行数:8,代码来源:model_builder.py

示例12: tokenizer

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def tokenizer(self):
    """lazy model loading"""
    with MODEL_DOWNLOAD_LOCK:
      # use lock to ensure model isn't downloaded by two processes at once
      if not getattr(self, "_tokenizer", None):
        self._tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
        self.pad_token = '[PAD]'
        self.bos_token = '[BOS]'
        self.eos_token = '[EOS]'
        self.unk_token = '[UNK]'
        self.pad_idx = self._tokenizer.vocab[self.pad_token]
        self.unk_idx = self._tokenizer.vocab[self.unk_token]

        # add EOS and BOS tokens to vocab by reusing unused slots
        self._tokenizer.basic_tokenizer.never_split += (self.eos_token, self.bos_token)
        vocab = self._tokenizer.vocab
        oldkey, newkey = '[unused1]', self.bos_token
        vocab = OrderedDict((newkey if k == oldkey else k, v) for k, v in vocab.items())
        oldkey, newkey = '[unused2]', self.eos_token
        vocab = OrderedDict((newkey if k == oldkey else k, v) for k, v in vocab.items())
        self._tokenizer.vocab = vocab
        self._tokenizer.wordpiece_tokenizer.vocab = vocab
        self.bos_idx = vocab[self.bos_token]
        self.eos_idx = vocab[self.eos_token]
        ids_to_tokens = OrderedDict(
            [(ids, tok) for tok, ids in vocab.items()])
        self._tokenizer.ids_to_tokens = ids_to_tokens
        self._tokenizer.wordpiece_tokenizer.ids_to_tokens = ids_to_tokens
    return self._tokenizer 
开发者ID:microsoft,项目名称:dstc8-meta-dialog,代码行数:31,代码来源:input_embedding.py

示例13: model

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def model(self):
    """lazy model loading"""
    with MODEL_DOWNLOAD_LOCK:
      # use lock to ensure model isn't downloaded by two processes at once
      if not getattr(self, "_model", None):
        self._model = BertModel.from_pretrained('bert-base-uncased')
        self._model.eval()
      assert self._model.config.hidden_size == self.embed_dim
    if cuda_utils.CUDA_ENABLED and self.use_cuda_if_available:
      self._model.cuda()
    return self._model 
开发者ID:microsoft,项目名称:dstc8-meta-dialog,代码行数:13,代码来源:input_embedding.py

示例14: __init__

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def __init__(self, device_number='cuda:2', use_cuda = True):
        
        self.device_number = device_number
        self.use_cuda = use_cuda
        
        self.tokenizer = BertTokenizer.from_pretrained('bert-large-uncased')
        
        self.model = BertModel.from_pretrained('bert-large-uncased')
        self.model.eval()
        
        if use_cuda:
            self.model.to(device_number) 
开发者ID:uhh-lt,项目名称:bert-sense,代码行数:14,代码来源:BERT_Model.py

示例15: make_bert_capsule_network

# 需要导入模块: from pytorch_pretrained_bert import BertModel [as 别名]
# 或者: from pytorch_pretrained_bert.BertModel import from_pretrained [as 别名]
def make_bert_capsule_network(config):
    base_path = os.path.join(config['base_path'])
    log_path = os.path.join(base_path, 'log/log.yml')
    log = yaml.safe_load(open(log_path))
    config = config['aspect_category_model'][config['aspect_category_model']['type']]
    bert = BertModel.from_pretrained('bert-base-uncased')
    model = BertCapsuleNetwork(
        bert=bert,
        bert_size=config['bert_size'],
        capsule_size=config['capsule_size'],
        dropout=config['dropout'],
        num_categories=log['num_categories']
    )
    model.load_sentiment(os.path.join(base_path, 'processed/sentiment_matrix.npy'))
    return model 
开发者ID:siat-nlp,项目名称:MAMS-for-ABSA,代码行数:17,代码来源:make_aspect_category_model.py


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