🚩🚩🚩Hugging Face 實戰系列 總目錄
有任何問題歡迎在下面留言
本篇文章的代碼運行界面均在PyCharm中進行
本篇文章配套的代碼資源已經上傳
從零構建屬于自己的GPT系列1:數據預處理
從零構建屬于自己的GPT系列2:模型訓練1
從零構建屬于自己的GPT系列3:模型訓練2
從零構建屬于自己的GPT系列4:模型訓練3
6 序列填充函數
def collate_fn(batch):input_ids = rnn_utils.pad_sequence(batch, batch_first=True, padding_value=5)labels = rnn_utils.pad_sequence(batch, batch_first=True, padding_value=-100)return input_ids, labels
7 損失計算函數
def caculate_loss(logit, target, pad_idx, smoothing=True):if smoothing:logit = logit[..., :-1, :].contiguous().view(-1, logit.size(2))target = target[..., 1:].contiguous().view(-1)eps = 0.1n_class = logit.size(-1)one_hot = torch.zeros_like(logit).scatter(1, target.view(-1, 1), 1)one_hot = one_hot * (1 - eps) + (1 - one_hot) * eps / (n_class - 1)log_prb = F.log_softmax(logit, dim=1)non_pad_mask = target.ne(pad_idx)loss = -(one_hot * log_prb).sum(dim=1)loss = loss.masked_select(non_pad_mask).mean() # average laterelse:# loss = F.cross_entropy(predict_logit, target, ignore_index=pad_idx)logit = logit[..., :-1, :].contiguous().view(-1, logit.size(-1))labels = target[..., 1:].contiguous().view(-1)loss = F.cross_entropy(logit, labels, ignore_index=pad_idx)return loss
8 評價函數
def calculate_acc(logit, labels, ignore_index=-100):logit = logit[..., :-1, :].contiguous().view(-1, logit.size(-1))labels = labels[..., 1:].contiguous().view(-1)_, logit = logit.max(dim=-1) # 對于每條數據,返回最大的index# 進行非運算,返回一個tensor,若labels的第i個位置為pad_id,則置為0,否則為1non_pad_mask = labels.ne(ignore_index)n_correct = logit.eq(labels).masked_select(non_pad_mask).sum().item()n_word = non_pad_mask.sum().item()return n_correct, n_word
9 訓練過程解讀
從零構建屬于自己的GPT系列1:數據預處理
從零構建屬于自己的GPT系列2:模型訓練1
從零構建屬于自己的GPT系列3:模型訓練2
從零構建屬于自己的GPT系列4:模型訓練3