920232796 / bert_seq2seq

pytorch实现 Bert 做seq2seq任务,使用unilm方案,现在也可以做自动摘要,文本分类,情感分析,NER,词性标注等任务,支持t5模型,支持GPT2进行文章续写。
Apache License 2.0
1.28k stars 208 forks source link

关于输入mask的问题 #13

Open xiaomi1213 opened 3 years ago

xiaomi1213 commented 3 years ago

原本的bert的输入是需要75%的mask的,但是我看您的输入好像没有这一部分,请问是不是因为attenion mask存在的缘故??

920232796 commented 3 years ago

你说的是训练bert的时候把? 我这个是使用预训练模型, 已经给你预训练好了 是去使用自己的数据去训练。

xiaomi1213 commented 3 years ago

那自己训练的时候不需要对输入进行mask么?

920232796 commented 3 years ago

需要对输入进行pad,然后计算loss的时候需要对某些输出进行mask。

xiaomi1213 commented 3 years ago

还有就是关于attention_mask的问题:按照您的代码,生成的attention_mask是 1, 1, 1, 1, 0, 0, 0 1, 1, 1, 1, 0, 0, 0 1, 1, 1, 1, 0, 0, 0 1, 1, 1, 1, 0, 0, 0 1, 1, 1, 1, 1, 0, 0 1, 1, 1, 1, 1, 1, 0 但是bert模型代码的self-attention模块的attention_mask是 attention_scores = attention_scores + attention_mask 这样在做seq2seq的时候不是encoder部分加一了么,而且decoder部分因为mask都是0,所以没有起作用的说,如果是乘的话,那您的mask才会起作用。 按照UniLM的原论文构造的矩阵应该如下: 0, 0, 0, 0, -inf, -inf, -inf 0, 0, 0, 0, -inf, -inf, -inf 0, 0, 0, 0, -inf, -inf, -inf 0, 0, 0, 0, -inf, -inf, -inf 0, 0, 0, 0, 0, -inf, -inf 0, 0, 0, 0, 0, 0, -inf 不知道是不是我理解有误,麻烦您解答下?

920232796 commented 3 years ago

extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0

920232796 commented 3 years ago

在你发的那个加操作之前 会有这个。

920232796 commented 3 years ago

然后输入的时候确实也mask了,刚才我说错了,你去看下unilm论文,或者看下我博客上unilm的文章,有具体原理。

xiaomi1213 commented 3 years ago

bert的官方代码的extended_attention_mask: torch.Tensor = self.get_extended_attention_mask(attention_mask, input_shape, device) 也是跟您的extended_attention_mask = (1.0 - extended_attention_mask) -10000.0这段代码的作用一致么,所以我如果attention_mask已经构造出了[0,-inf]这类mask,那extended_attention_mask = (1.0 - extended_attention_mask) -10000.0这一步就不用加了吧?

920232796 commented 3 years ago

对的对的。

xiaomi1213 commented 3 years ago

看了您博客还有苏神的《从语言模型到Seq2Seq:Transformer如戏,全靠Mask》,我的理解是UniLM用attention_mask代替了bert和XLNet之类模型的输入的mask,然后输入全部用交叉熵做损失函数?

xiaomi1213 commented 3 years ago

看了微软UniLM的源码,在s2s-ft/utils.py/Seq2seqDatasetForBert类里,好像有对输出做随机的mask

920232796 commented 3 years ago

看了您博客还有苏神的《从语言模型到Seq2Seq:Transformer如戏,全靠Mask》,我的理解是UniLM用attention_mask代替了bert和XLNet之类模型的输入的mask,然后输入全部用交叉熵做损失函数?

嗯嗯

920232796 commented 3 years ago

看了微软UniLM的源码,在s2s-ft/utils.py/Seq2seqDatasetForBert类里,好像有对输出做随机的mask

mask的目的是什么呢?

xiaomi1213 commented 3 years ago

假设输入是: input_tensor = torch.tensor([[101,8,3,4,102,7,3,3,102,0,0,0],[101,2,4,4,102,7,5,102,0,0,0,0],[101,2,3,102,7,7,3,1,102,0,0,0]]) token_type_id = torch.tensor([[0,0,0,0,0,1,1,1,1,0,0,0],[0,0,0,0,0,1,1,1,0,0,0,0],[0,0,0,0,1,1,1,1,1,0,0,0]]) 按照您的代码,最后得到的extended_attention_mask[0]是,取-10000为-5 [[[-0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -0., -0., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -0., -0., -0., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -0., -0., -0., -0., -5., -5., -5.], [-0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5., -5.], [-0., -0., -0., -0., -0., -5., -5., -5., -5., -5., -5., -5.]]] 请问左下角的三行的零,在这里是属于输入的pad,不是应该mask为-5么?

920232796 commented 3 years ago

没必要呀。因为pad在embeding层的时候也会被处理掉,这里就没必要了。

920232796 commented 3 years ago

你如果太严格 那这里代码就不好写了

920232796 commented 3 years ago

pad处理是属于bert模型内部的问题 这个mask是额外求的。

xiaomi1213 commented 3 years ago

请问yayun_list是加密了么,都是字符的?

920232796 commented 3 years ago

没加密,就是这样的。