BERT 输出不确定

Kea*_*aik 6 nlp transformer-model deep-learning bert-language-model

BERT 输出不是确定性的。当我输入相同的输入时,我希望输出值是确定性的,但是我的 bert 模型中的值正在发生变化。听起来很尴尬,相同的值被返回两次,一次。也就是说,一旦出现另一个值,就会出现相同的值并重复。如何使输出具有确定性?让我展示我的代码片段。我使用的模型如下。

对于 BERT 实现,我使用了 Huggingface 实现的 BERT pytorch 实现。这是 pytorch 领域非常有名的模型 ri 实现。[链接] https://github.com/huggingface/pytorch-pretrained-BERT/

        tokenizer = BertTokenizer.from_pretrained(self.bert_type, do_lower_case=self.do_lower_case, cache_dir=self.bert_cache_path)
        pretrain_bert = BertModel.from_pretrained(self.bert_type, cache_dir=self.bert_cache_path)
        bert_config = pretrain_bert.config
Run Code Online (Sandbox Code Playgroud)

得到这样的输出

        all_encoder_layer, pooled_output = self.model_bert(all_input_ids, all_segment_ids, all_input_mask)

        # all_encoder_layer: BERT outputs from all layers.
        # pooled_output: output of [CLS] vec.

Run Code Online (Sandbox Code Playgroud)

pooled_output

tensor([[-3.3997e-01,  2.6870e-01, -2.8109e-01, -2.0018e-01, -8.6849e-02,

tensor([[ 7.4340e-02, -3.4894e-03, -4.9583e-03,  6.0806e-02,  8.5685e-02,

tensor([[-3.3997e-01,  2.6870e-01, -2.8109e-01, -2.0018e-01, -8.6849e-02,

tensor([[ 7.4340e-02, -3.4894e-03, -4.9583e-03,  6.0806e-02,  8.5685e-02,
Run Code Online (Sandbox Code Playgroud)

对于所有编码器层,情况是相同的, - 一次两次相同。

我从bert中提取词嵌入特征,情况是一样的。

wemb_n
tensor([[[ 0.1623,  0.4293,  0.1031,  ..., -0.0434, -0.5156, -1.0220],

tensor([[[ 0.0389,  0.5050,  0.1327,  ...,  0.3232,  0.2232, -0.5383],

tensor([[[ 0.1623,  0.4293,  0.1031,  ..., -0.0434, -0.5156, -1.0220],

tensor([[[ 0.0389,  0.5050,  0.1327,  ...,  0.3232,  0.2232, -0.5383],
Run Code Online (Sandbox Code Playgroud)

小智 7

请尝试设置种子。我遇到了同样的问题并设置了种子以确保我们每次都获得相同的值。可能的原因之一可能是 BERT 发生了辍学。

  • 我们知道推理过程中什么是随机的吗?在训练过程中,我相信大多数版本的 BERT 都使用 dropout,即使用随机化。但是,为了推断,我不确定在哪里使用随机数生成器。 (6认同)