BERT输出不是确定性的。我希望当我输入相同的输入时,输出值是确定的,但我的bert模型的输出值正在变化。听起来很别扭,相同的值返回两次,一次。也就是说,一旦出现另一个值,同样的值也会出现并重复。如何使输出具有确定性?让我展示一下我的代码片段。我使用下面的模型。
对于BERT实现,我使用huggingface实现的BERT pytorch实现。这是pytorch领域中非常著名的模型ri实现。link
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像这样获得输出
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.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,对于所有编码层,情况是一样的,一次两次都是一样的。
我从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],发布于 2019-06-18 17:51:50
请试着播种。我面对着同样的问题,并设置了种子,以确保我们每次都获得相同的值。其中一个可能的原因可能是BERT发生了辍学。
https://stackoverflow.com/questions/56639938
复制相似问题