def __init__(self, config): from model_utils import ContextPooler super().__init__(config) self.deberta = DebertaModel(config) self.pooler = ContextPooler(config) self.init_weights()
def get_transformers_model( settings: Dict[str, Any], model_name: str, pretrained: bool = True, ckptdir: Optional[Path] = None, ) -> PreTrainedModel: model_path = model_name if pretrained else str(ckptdir) config = AutoConfig.from_pretrained(model_path) config.attention_probs_dropout_prob = settings.get( 'encoder_attn_dropout_rate', 0.1) config.hidden_dropout_prob = settings.get('encoder_ffn_dropout_rate', 0.1) config.layer_norm_eps = settings.get('layer_norm_eps', 1e-5) if pretrained: model = AutoModel.from_pretrained(model_name, config=config) return model # if you want not parameters but only model structure, each model class is needed. if 'xlm' in model_name: model = XLMModel(config=config) elif 'albert' in model_name: model = AlbertModel(config=config) elif 'roberta' in model_name: model = RobertaModel(config=config) elif 'deberta-v2' in model_name: model = DebertaV2Model(config=config) elif 'deberta' in model_name: model = DebertaModel(config=config) elif 'bert' in model_name: model = BertModel(config=config) elif 'electra' in model_name: model = ElectraModel(config=config) else: model = BertModel(config=config) return model
def base_deberta(): from transformers import DebertaTokenizer, DebertaModel tokenizer = DebertaTokenizer.from_pretrained('microsoft/deberta-base') model = DebertaModel.from_pretrained('microsoft/deberta-base') inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") outputs = model(**inputs) last_hidden_states = outputs.last_hidden_state #输出最后一层的隐藏层状态 print(last_hidden_states)
def test_inference_no_head(self): model = DebertaModel.from_pretrained("microsoft/deberta-base") input_ids = torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]) attention_mask = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]) output = model(input_ids, attention_mask=attention_mask)[0] # compare the actual values for a slice. expected_slice = torch.tensor( [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]] ) self.assertTrue(torch.allclose(output[:, 1:4, 1:4], expected_slice, atol=1e-4), f"{output[:, 1:4, 1:4]}")
def test_inference_no_head(self): random.seed(0) np.random.seed(0) torch.manual_seed(0) torch.cuda.manual_seed_all(0) model = DebertaModel.from_pretrained("microsoft/deberta-base") input_ids = torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]) output = model(input_ids)[0] # compare the actual values for a slice. expected_slice = torch.tensor( [[[-0.0218, -0.6641, -0.3665], [-0.3907, -0.4716, -0.6640], [0.7461, 1.2570, -0.9063]]] ) self.assertTrue(torch.allclose(output[:, :3, :3], expected_slice, atol=1e-4), f"{output[:, :3, :3]}")
def __init__(self, config): super().__init__(config) self.num_labels = config.num_labels self.deberta = DebertaModel(config) self.dropout = nn.Dropout(config.hidden_dropout_prob) self.classifier = nn.Linear(config.hidden_size, config.num_labels) self.loss_fct = nn.CrossEntropyLoss() self.use_crf = config.use_crf if self.use_crf: self.crf_layer = Transformer_CRF( num_labels=config.num_labels, start_label_id=config.label2idx['CLS']) else: self.crf_layer = None self.init_weights()
def create_and_check_deberta_model( self, config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels ): model = DebertaModel(config=config) model.to(torch_device) model.eval() sequence_output = model(input_ids, attention_mask=input_mask, token_type_ids=token_type_ids)[0] sequence_output = model(input_ids, token_type_ids=token_type_ids)[0] sequence_output = model(input_ids)[0] self.parent.assertListEqual(list(sequence_output.size()), [self.batch_size, self.seq_length, self.hidden_size])
def test_model_from_pretrained(self): for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]: model = DebertaModel.from_pretrained(model_name) self.assertIsNotNone(model)
def __init__(self, config: DebertaConfig, model_path: str): super(DeBertForOppo, self).__init__(config) self.bert = DebertaModel.from_pretrained(model_path, config=config) self.classifier = nn.Linear(2 * config.hidden_size, config.num_labels) self.dropout = nn.Dropout(config.hidden_dropout_prob)