欢迎关注公众号:DeepL Newer

MENU

BERT的PyTorch实现

July 24, 2020 • Read: 5546 • Deep Learning阅读设置

B站视频讲解

本文主要介绍一下如何使用 PyTorch 复现BERT。请先花上 10 分钟阅读我的这篇文章 BERT详解(附带ELMo、GPT介绍),再来看本文,方能达到醍醐灌顶,事半功倍的效果

准备数据集

这里我并没有用什么大型的数据集,而是手动输入了两个人的对话,主要是为了降低代码阅读难度,我希望读者能更关注模型实现的部分

'''
  code by Tae Hwan Jung(Jeff Jung) @graykode, modify by wmathor
  Reference : https://github.com/jadore801120/attention-is-all-you-need-pytorch
         https://github.com/JayParks/transformer, https://github.com/dhlee347/pytorchic-bert
'''
import re
import math
import torch
import numpy as np
from random import *
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as Data

text = (
    'Hello, how are you? I am Romeo.\n' # R
    'Hello, Romeo My name is Juliet. Nice to meet you.\n' # J
    'Nice meet you too. How are you today?\n' # R
    'Great. My baseball team won the competition.\n' # J
    'Oh Congratulations, Juliet\n' # R
    'Thank you Romeo\n' # J
    'Where are you going today?\n' # R
    'I am going shopping. What about you?\n' # J
    'I am going to visit my grandmother. she is not very well' # R
)
sentences = re.sub("[.,!?\\-]", '', text.lower()).split('\n') # filter '.', ',', '?', '!'
word_list = list(set(" ".join(sentences).split())) # ['hello', 'how', 'are', 'you',...]
word2idx = {'[PAD]' : 0, '[CLS]' : 1, '[SEP]' : 2, '[MASK]' : 3}
for i, w in enumerate(word_list):
    word2idx[w] = i + 4
idx2word = {i: w for i, w in enumerate(word2idx)}
vocab_size = len(word2idx)

token_list = list()
for sentence in sentences:
    arr = [word2idx[s] for s in sentence.split()]
    token_list.append(arr)

最终token_list是个二维的list,里面每一行代表一句话

print(token_list)
'''
[[12, 7, 22, 5, 39, 21, 15],
 [12, 15, 13, 35, 10, 27, 34, 14, 19, 5],
 [34, 19, 5, 17, 7, 22, 5, 8],
 [33, 13, 37, 32, 28, 11, 16],
 [30, 23, 27],
 [6, 5, 15],
 [36, 22, 5, 31, 8],
 [39, 21, 31, 18, 9, 20, 5],
 [39, 21, 31, 14, 29, 13, 4, 25, 10, 26, 38, 24]]
'''

模型参数

# BERT Parameters
maxlen = 30
batch_size = 6
max_pred = 5 # max tokens of prediction
n_layers = 6
n_heads = 12
d_model = 768
d_ff = 768*4 # 4*d_model, FeedForward dimension
d_k = d_v = 64  # dimension of K(=Q), V
n_segments = 2
  • maxlen表示同一个batch中的所有句子都由30个token组成,不够的补PAD(这里我实现的方式比较粗暴,直接固定所有batch中的所有句子都为30)
  • max_pred表示最多需要预测多少个单词,即BERT中的完形填空任务
  • n_layers表示Encoder Layer的数量
  • d_model表示Token Embeddings、Segment Embeddings、Position Embeddings的维度
  • d_ff表示Encoder Layer中全连接层的维度
  • n_segments表示Decoder input由几句话组成

数据预处理

数据预处理部分,我们需要根据概率随机make或者替换(以下统称mask)一句话中15%的token,还需要拼接任意两句话

# sample IsNext and NotNext to be same in small batch size
def make_data():
    batch = []
    positive = negative = 0
    while positive != batch_size/2 or negative != batch_size/2:
        tokens_a_index, tokens_b_index = randrange(len(sentences)), randrange(len(sentences)) # sample random index in sentences
        tokens_a, tokens_b = token_list[tokens_a_index], token_list[tokens_b_index]
        input_ids = [word2idx['[CLS]']] + tokens_a + [word2idx['[SEP]']] + tokens_b + [word2idx['[SEP]']]
        segment_ids = [0] * (1 + len(tokens_a) + 1) + [1] * (len(tokens_b) + 1)

        # MASK LM
        n_pred =  min(max_pred, max(1, int(len(input_ids) * 0.15))) # 15 % of tokens in one sentence
        cand_maked_pos = [i for i, token in enumerate(input_ids)
                          if token != word2idx['[CLS]'] and token != word2idx['[SEP]']] # candidate masked position
        shuffle(cand_maked_pos)
        masked_tokens, masked_pos = [], []
        for pos in cand_maked_pos[:n_pred]:
            masked_pos.append(pos)
            masked_tokens.append(input_ids[pos])
            if random() < 0.8:  # 80%
                input_ids[pos] = word2idx['[MASK]'] # make mask
            elif random() > 0.9:  # 10%
                index = randint(0, vocab_size - 1) # random index in vocabulary
                while index < 4: # can't involve 'CLS', 'SEP', 'PAD'
                  index = randint(0, vocab_size - 1)
                input_ids[pos] = index # replace

        # Zero Paddings
        n_pad = maxlen - len(input_ids)
        input_ids.extend([0] * n_pad)
        segment_ids.extend([0] * n_pad)

        # Zero Padding (100% - 15%) tokens
        if max_pred > n_pred:
            n_pad = max_pred - n_pred
            masked_tokens.extend([0] * n_pad)
            masked_pos.extend([0] * n_pad)

        if tokens_a_index + 1 == tokens_b_index and positive < batch_size/2:
            batch.append([input_ids, segment_ids, masked_tokens, masked_pos, True]) # IsNext
            positive += 1
        elif tokens_a_index + 1 != tokens_b_index and negative < batch_size/2:
            batch.append([input_ids, segment_ids, masked_tokens, masked_pos, False]) # NotNext
            negative += 1
    return batch
# Proprecessing Finished

batch = make_data()
input_ids, segment_ids, masked_tokens, masked_pos, isNext = zip(*batch)
input_ids, segment_ids, masked_tokens, masked_pos, isNext = \
    torch.LongTensor(input_ids),  torch.LongTensor(segment_ids), torch.LongTensor(masked_tokens),\
    torch.LongTensor(masked_pos), torch.LongTensor(isNext)

class MyDataSet(Data.Dataset):
  def __init__(self, input_ids, segment_ids, masked_tokens, masked_pos, isNext):
    self.input_ids = input_ids
    self.segment_ids = segment_ids
    self.masked_tokens = masked_tokens
    self.masked_pos = masked_pos
    self.isNext = isNext
  
  def __len__(self):
    return len(self.input_ids)
  
  def __getitem__(self, idx):
    return self.input_ids[idx], self.segment_ids[idx], self.masked_tokens[idx], self.masked_pos[idx], self.isNext[idx]

loader = Data.DataLoader(MyDataSet(input_ids, segment_ids, masked_tokens, masked_pos, isNext), batch_size, True)

上述代码中,positive变量代表两句话是连续的个数,negative代表两句话不是连续的个数,我们需要做到在一个batch中,这两个样本的比例为1:1。随机选取的两句话是否连续,只要通过判断tokens_a_index + 1 == tokens_b_index即可

然后是随机mask一些token,n_pred变量代表的是即将mask的token数量,cand_maked_pos代表的是有哪些位置是候选的、可以mask的(因为像[SEP],[CLS]这些不能做mask,没有意义),最后shuffle()一下,然后根据random()的值选择是替换为[MASK]还是替换为其它的token

接下来会做两个Zero Padding,第一个是为了补齐句子的长度,使得一个batch中的句子都是相同长度。第二个是为了补齐mask的数量,因为不同句子长度,会导致不同数量的单词进行mask,我们需要保证同一个batch中,mask的数量(必须)是相同的,所以也需要在后面补一些没有意义的东西,比方说[0]

以上就是整个数据预处理的部分

模型构建

模型结构主要采用了Transformer的Encoder,所以这里我不再多赘述,可以直接看我的这篇文章Transformer的PyTorch实现,以及B站视频讲解

def get_attn_pad_mask(seq_q, seq_k):
    batch_size, seq_len = seq_q.size()
    # eq(zero) is PAD token
    pad_attn_mask = seq_q.data.eq(0).unsqueeze(1)  # [batch_size, 1, seq_len]
    return pad_attn_mask.expand(batch_size, seq_len, seq_len)  # [batch_size, seq_len, seq_len]

def gelu(x):
    """
      Implementation of the gelu activation function.
      For information: OpenAI GPT's gelu is slightly different (and gives slightly different results):
      0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3))))
      Also see https://arxiv.org/abs/1606.08415
    """
    return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))

class Embedding(nn.Module):
    def __init__(self):
        super(Embedding, self).__init__()
        self.tok_embed = nn.Embedding(vocab_size, d_model)  # token embedding
        self.pos_embed = nn.Embedding(maxlen, d_model)  # position embedding
        self.seg_embed = nn.Embedding(n_segments, d_model)  # segment(token type) embedding
        self.norm = nn.LayerNorm(d_model)

    def forward(self, x, seg):
        seq_len = x.size(1)
        pos = torch.arange(seq_len, dtype=torch.long)
        pos = pos.unsqueeze(0).expand_as(x)  # [seq_len] -> [batch_size, seq_len]
        embedding = self.tok_embed(x) + self.pos_embed(pos) + self.seg_embed(seg)
        return self.norm(embedding)

class ScaledDotProductAttention(nn.Module):
    def __init__(self):
        super(ScaledDotProductAttention, self).__init__()

    def forward(self, Q, K, V, attn_mask):
        scores = torch.matmul(Q, K.transpose(-1, -2)) / np.sqrt(d_k) # scores : [batch_size, n_heads, seq_len, seq_len]
        scores.masked_fill_(attn_mask, -1e9) # Fills elements of self tensor with value where mask is one.
        attn = nn.Softmax(dim=-1)(scores)
        context = torch.matmul(attn, V)
        return context

class MultiHeadAttention(nn.Module):
    def __init__(self):
        super(MultiHeadAttention, self).__init__()
        self.W_Q = nn.Linear(d_model, d_k * n_heads)
        self.W_K = nn.Linear(d_model, d_k * n_heads)
        self.W_V = nn.Linear(d_model, d_v * n_heads)
    def forward(self, Q, K, V, attn_mask):
        # q: [batch_size, seq_len, d_model], k: [batch_size, seq_len, d_model], v: [batch_size, seq_len, d_model]
        residual, batch_size = Q, Q.size(0)
        # (B, S, D) -proj-> (B, S, D) -split-> (B, S, H, W) -trans-> (B, H, S, W)
        q_s = self.W_Q(Q).view(batch_size, -1, n_heads, d_k).transpose(1,2)  # q_s: [batch_size, n_heads, seq_len, d_k]
        k_s = self.W_K(K).view(batch_size, -1, n_heads, d_k).transpose(1,2)  # k_s: [batch_size, n_heads, seq_len, d_k]
        v_s = self.W_V(V).view(batch_size, -1, n_heads, d_v).transpose(1,2)  # v_s: [batch_size, n_heads, seq_len, d_v]

        attn_mask = attn_mask.unsqueeze(1).repeat(1, n_heads, 1, 1) # attn_mask : [batch_size, n_heads, seq_len, seq_len]

        # context: [batch_size, n_heads, seq_len, d_v], attn: [batch_size, n_heads, seq_len, seq_len]
        context = ScaledDotProductAttention()(q_s, k_s, v_s, attn_mask)
        context = context.transpose(1, 2).contiguous().view(batch_size, -1, n_heads * d_v) # context: [batch_size, seq_len, n_heads * d_v]
        output = nn.Linear(n_heads * d_v, d_model)(context)
        return nn.LayerNorm(d_model)(output + residual) # output: [batch_size, seq_len, d_model]

class PoswiseFeedForwardNet(nn.Module):
    def __init__(self):
        super(PoswiseFeedForwardNet, self).__init__()
        self.fc1 = nn.Linear(d_model, d_ff)
        self.fc2 = nn.Linear(d_ff, d_model)

    def forward(self, x):
        # (batch_size, seq_len, d_model) -> (batch_size, seq_len, d_ff) -> (batch_size, seq_len, d_model)
        return self.fc2(gelu(self.fc1(x)))

class EncoderLayer(nn.Module):
    def __init__(self):
        super(EncoderLayer, self).__init__()
        self.enc_self_attn = MultiHeadAttention()
        self.pos_ffn = PoswiseFeedForwardNet()

    def forward(self, enc_inputs, enc_self_attn_mask):
        enc_outputs = self.enc_self_attn(enc_inputs, enc_inputs, enc_inputs, enc_self_attn_mask) # enc_inputs to same Q,K,V
        enc_outputs = self.pos_ffn(enc_outputs) # enc_outputs: [batch_size, seq_len, d_model]
        return enc_outputs

class BERT(nn.Module):
    def __init__(self):
        super(BERT, self).__init__()
        self.embedding = Embedding()
        self.layers = nn.ModuleList([EncoderLayer() for _ in range(n_layers)])
        self.fc = nn.Sequential(
            nn.Linear(d_model, d_model),
            nn.Dropout(0.5),
            nn.Tanh(),
        )
        self.classifier = nn.Linear(d_model, 2)
        self.linear = nn.Linear(d_model, d_model)
        self.activ2 = gelu
        # fc2 is shared with embedding layer
        embed_weight = self.embedding.tok_embed.weight
        self.fc2 = nn.Linear(d_model, vocab_size, bias=False)
        self.fc2.weight = embed_weight

    def forward(self, input_ids, segment_ids, masked_pos):
        output = self.embedding(input_ids, segment_ids) # [bach_size, seq_len, d_model]
        enc_self_attn_mask = get_attn_pad_mask(input_ids, input_ids) # [batch_size, maxlen, maxlen]
        for layer in self.layers:
            # output: [batch_size, max_len, d_model]
            output = layer(output, enc_self_attn_mask)
        # it will be decided by first token(CLS)
        h_pooled = self.fc(output[:, 0]) # [batch_size, d_model]
        logits_clsf = self.classifier(h_pooled) # [batch_size, 2] predict isNext

        masked_pos = masked_pos[:, :, None].expand(-1, -1, d_model) # [batch_size, max_pred, d_model]
        h_masked = torch.gather(output, 1, masked_pos) # masking position [batch_size, max_pred, d_model]
        h_masked = self.activ2(self.linear(h_masked)) # [batch_size, max_pred, d_model]
        logits_lm = self.fc2(h_masked) # [batch_size, max_pred, vocab_size]
        return logits_lm, logits_clsf
model = BERT()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adadelta(model.parameters(), lr=0.001)

这段代码中用到了一个激活函数gelu,这是BERT论文中提出来的,具体公式可以看这篇文章GELU激活函数

这段代码有一个特别不好理解的地方,就是到数第7行的代码,用到了torch.gather()函数,这里我稍微讲一下。这个函数实际上实现了以下的功能

out = torch.gather(input, dim, index)
# out[i][j][k] = input[index[i][j][k]][j][k] # dim=0
# out[i][j][k] = input[i][index[i][j][k]][k] # dim=1
# out[i][j][k] = input[i][j][index[i][j][k]] # dim=2

具体以一个例子来说就是,首先我生成index变量

index = torch.from_numpy(np.array([[1, 2, 0], [2, 0, 1]])).type(torch.LongTensor)
index = index[:, :, None].expand(-1, -1, 10)
print(index)
'''
tensor([[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

        [[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]])
'''

然后随机生成一个[2, 3, 10]维的tensor,可以理解为有2个batch,每个batch有3句话,每句话由10个词构成,只不过这里的词不是以正整数(索引)的形式出现,而是连续的数值

input = torch.rand(2, 3, 10)
print(input)
'''
tensor([[[0.7912, 0.7098, 0.7548, 0.8627, 0.1966, 0.6327, 0.6629, 0.8158,
          0.7094, 0.1476],
         [0.0774, 0.6794, 0.0030, 0.1855, 0.7391, 0.0641, 0.2950, 0.9734,
          0.7018, 0.3370],
         [0.2190, 0.3976, 0.0112, 0.5581, 0.1329, 0.2154, 0.6277, 0.0850,
          0.4446, 0.5158]],

        [[0.4145, 0.8486, 0.9515, 0.3826, 0.6641, 0.5192, 0.2311, 0.6960,
          0.4215, 0.5597],
         [0.0221, 0.5232, 0.3971, 0.8972, 0.2772, 0.5046, 0.1881, 0.9044,
          0.6925, 0.9837],
         [0.6797, 0.5538, 0.8139, 0.1199, 0.0095, 0.4940, 0.7814, 0.1484,
          0.0200, 0.7489]]])
'''

之后调用torch.gather(input, 1, index)函数

print(torch.gather(input, 1, index))
'''
tensor([[[0.0774, 0.6794, 0.0030, 0.1855, 0.7391, 0.0641, 0.2950, 0.9734,
          0.7018, 0.3370],
         [0.2190, 0.3976, 0.0112, 0.5581, 0.1329, 0.2154, 0.6277, 0.0850,
          0.4446, 0.5158],
         [0.7912, 0.7098, 0.7548, 0.8627, 0.1966, 0.6327, 0.6629, 0.8158,
          0.7094, 0.1476]],

        [[0.6797, 0.5538, 0.8139, 0.1199, 0.0095, 0.4940, 0.7814, 0.1484,
          0.0200, 0.7489],
         [0.4145, 0.8486, 0.9515, 0.3826, 0.6641, 0.5192, 0.2311, 0.6960,
          0.4215, 0.5597],
         [0.0221, 0.5232, 0.3971, 0.8972, 0.2772, 0.5046, 0.1881, 0.9044,
          0.6925, 0.9837]]])
'''

index中第一行的tensor会作用于input的第一个batch,具体来说,原本三句话的顺序是[0, 1, 2],现在会根据[1, 2, 0]调换顺序。index中第2行的tensor会作用于input的第二个batch,具体来说,原本三句话的顺序是[0, 1, 2],现在会根据[2, 0, 1]调换顺序

训练&测试

以下是训练代码

for epoch in range(180):
    for input_ids, segment_ids, masked_tokens, masked_pos, isNext in loader:
      logits_lm, logits_clsf = model(input_ids, segment_ids, masked_pos)
      loss_lm = criterion(logits_lm.view(-1, vocab_size), masked_tokens.view(-1)) # for masked LM
      loss_lm = (loss_lm.float()).mean()
      loss_clsf = criterion(logits_clsf, isNext) # for sentence classification
      loss = loss_lm + loss_clsf
      if (epoch + 1) % 10 == 0:
          print('Epoch:', '%04d' % (epoch + 1), 'loss =', '{:.6f}'.format(loss))
      optimizer.zero_grad()
      loss.backward()
      optimizer.step()

以下是测试代码

# Predict mask tokens ans isNext
input_ids, segment_ids, masked_tokens, masked_pos, isNext = batch[0]
print(text)
print([idx2word[w] for w in input_ids if idx2word[w] != '[PAD]'])

logits_lm, logits_clsf = model(torch.LongTensor([input_ids]), \
                 torch.LongTensor([segment_ids]), torch.LongTensor([masked_pos]))
logits_lm = logits_lm.data.max(2)[1][0].data.numpy()
print('masked tokens list : ',[pos for pos in masked_tokens if pos != 0])
print('predict masked tokens list : ',[pos for pos in logits_lm if pos != 0])

logits_clsf = logits_clsf.data.max(1)[1].data.numpy()[0]
print('isNext : ', True if isNext else False)
print('predict isNext : ',True if logits_clsf else False)

最后给出完整代码链接(需要科学的力量)
Github 项目地址:nlp-tutorial

Last Modified: April 24, 2021
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment

28 Comments
  1. ChanggengWei ChanggengWei

    感谢up主的无私分享,我是从你的seq2seq,transformer一路看到bert的,非常感谢你的详细分析,另外我写一下我对文中几处的理解。
    在BERT的forward中
    masked_pos = masked_pos[:, :, None].expand(-1, -1, d_model)
    h_masked = torch.gather(output, 1, masked_pos)
    两行代码的注释中,个人觉得masked_pos和h_masked的维度应该为 [batch_size, max_pred, d_model],后面的logits_lm维度应该为[batch_size, max_pred, d_model]

    另外说说我对torch.gather(input, 1, index)的理解,个人觉得,这个函数的作用是按index抽取input里的元素,比如index为[4,,6,10]的话,返回的是input中索引为4,6,10的元素。基于我个人的理解,BERT的encoder最后抽取的是所有词的信息,维度为[batch_size, maxlen, d_mdel],其中有max_pred个词被mask,然后拿gather函数抽取被mask的预测值[batch, max_pred, d_model]来做损失计算。
    以上纯属个人理解,不吝赐教!

    1. lxt lxt

      @ChanggengWei我觉得你说的对

    2. mathor mathor

      @ChanggengWei感谢您的指正,我思考了下您说的是对的

      我已修改文章和代码,再次感谢

      ps:最近一段时间太多人给我提各种各样的问题,没第一时间回复您,不好意思

  2. 鸭鸭 鸭鸭

    博主,文章里好像漏了制作DataLoader的部分?

    1. mathor mathor

      @鸭鸭感谢提醒,已经补上了

      ps:建议您下次评论的时候填一下邮箱,这样可以及时收到我给您的回复

    2. 鸭鸭 鸭鸭

      @mathor好的,谢谢博主

  3. Kevin Kevin

    feedforward 这部分 是bert源代码就没有 Add & Norm 这部分吗 因为我看transformer encoder里好像就有

    1. mathor mathor

      @Kevin你想加的话可以加,这些都是小细节,没有那么重要啦

  4. waiting涙 waiting涙

    请问大佬模型代码里 self.fc2.weight = embed_weight 的意义是什么呢,如果不进行这个embedding的参数共享后果会如何?

    1. mathor mathor

      @waiting涙就是单纯字面意义,不共享参数也是可以的,随意

    2. waiting涙 waiting涙

      @mathor感谢博主解答@(哈哈)

  5. 尘世猫 尘世猫

    请问bert中,最后这段代码什么意思呢
    embed_weight = self.embedding.tok_embed.weight
    self.fc2 = nn.Linear(d_model, vocab_size, bias=False)
    self.fc2.weight = embed_weight

    1. mathor mathor

      @尘世猫权重共享罢了,不这么写也可以

  6. TZZHH TZZHH

    您好,想请问一下运行训练部分的时候会报错如下:
    ----> 8 logits_lm, logits_clsf = model(input_ids, segment_ids, masked_pos)
    E:ANACONDAenvspytorchlibsite-packagestorchnnmodulesmodule.py in _call_impl(self, input, *kwargs)
    --> 889 result = self.forward(input, *kwargs)
    <ipython-input-23-055b6dc2b658> in forward(self, input_ids, segment_ids, masked_pos)
    --> 106 enc_self_attn_mask = get_attn_pad_mask(input_ids, input_ids)
    <ipython-input-23-055b6dc2b658> in get_attn_pad_mask(seq_q, seq_k)
    ----> 5 pad_attn_mask = seq_len.data.eq(0).unsqueeze(1)
    AttributeError: 'int' object has no attribute 'data'
    但是在transformer部分相同的get_attn_pad_mask却没有错,请问是为什么呀,看了好久也没有解决

    1. wesley wesley

      @TZZHH换成 pad_attn_mask = seq_q.data.eq(0).unsqueeze(1)

      seq_q !!!

    2. mathor mathor

      @wesley感谢提醒,我也没发现文章中写错了,已修改

    3. TZZHH TZZHH

      @wesley感谢感谢

  7. manba manba

    请问以下,在输入数据的时候一般不是第一个维度表示batch_size大小吗?博主这里是重新设置了第一个表示batch_size大小吗,还有一个问题,如果我用pytorch中的transformerEncoder和你这里实现的相比的话,我可不可以直接就用这个代替你写的transformerEncoder这一部分啊?

  8. 星隐 星隐

    谢谢博主的精彩解析! 我有一个小问题在于,损失函数criterion = nn.CrossEntropyLoss()中似乎对于MLM任务的损失没有添加ignore_index 参数, 导致填充词也参与了损失函数计算。而对于句子预测任务,又无法使用ignore_index = 1。是否应该采用两个损失函数进行计算呢?@(乖)

    1. mathor mathor

      @星隐你说的貌似有点道理

  9. 123 123

    请问经过预训练的BERT和没有经过预训练的最后效果差别大吗

    1. mathor mathor

      @123非常大,经过预训练的效果会非常好

  10. Ziyi Cui Ziyi Cui

    代码看起来极度舒适 感恩!@(爱心)

  11. Ziyi Cui Ziyi Cui

    有个疑问请教一下。我想通过bert学到embedding并用到下游任务,但是在bert的forward里 dim=1是max_pred而不是sentence的长度。那么这种情况,在预训练结束后,该如何转化成sentence的长度,并用于我的下游任务呢

    1. mathor mathor

      @Ziyi Cui那你可以在Model里定义一个inference方法(方法名无所谓,随便起),然后在训练结束后,也就是测试阶段,调用model.inference()去进行测试,而不是直接用model()去测试

  12. 椒麻鸡吧 椒麻鸡吧

    博主你好,我想请教一下,如果使用bert训练的encoder去做预测,是直接使用embedding去预测,还是将未来时刻全部mask再去encoder最后预测?

    1. mathor mathor

      @椒麻鸡吧你要预测什么呢?如果是简单的分类问题,应该是将BERT第12层的输出送入nn.Linear()做预测

    2. 椒麻鸡吧 椒麻鸡吧

      @mathor好的,谢谢回复。
      但是不是那样对整个序列的分类。
      我的预测问题是类似下棋这样的每个时间步上的特征是在集合里面选择的序列预测,也是分类问题