Example #1
0
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 test_model_from_pretrained(self):
     for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         model = DebertaModel.from_pretrained(model_name)
         self.assertIsNotNone(model)
Example #5
0
 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)