chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

電子發(fā)燒友App

硬聲App

掃碼添加小助手

加入工程師交流群

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>PyTorch教程15.9之預(yù)訓(xùn)練BERT的數(shù)據(jù)集

PyTorch教程15.9之預(yù)訓(xùn)練BERT的數(shù)據(jù)集

2023-06-05 | pdf | 0.14 MB | 次下載 | 免費(fèi)

資料介紹

為了預(yù)訓(xùn)練第 15.8 節(jié)中實(shí)現(xiàn)的 BERT 模型,我們需要以理想的格式生成數(shù)據(jù)集,以促進(jìn)兩項(xiàng)預(yù)訓(xùn)練任務(wù):掩碼語(yǔ)言建模和下一句預(yù)測(cè)。一方面,原始的 BERT 模型是在兩個(gè)巨大的語(yǔ)料庫(kù) BookCorpus 和英文維基百科(參見(jiàn)第15.8.5 節(jié))的串聯(lián)上進(jìn)行預(yù)訓(xùn)練的,這使得本書(shū)的大多數(shù)讀者難以運(yùn)行。另一方面,現(xiàn)成的預(yù)訓(xùn)練 BERT 模型可能不適合醫(yī)學(xué)等特定領(lǐng)域的應(yīng)用。因此,在自定義數(shù)據(jù)集上預(yù)訓(xùn)練 BERT 變得越來(lái)越流行。為了便于演示 BERT 預(yù)訓(xùn)練,我們使用較小的語(yǔ)料庫(kù) WikiText-2 ( Merity et al. , 2016 )。

15.3節(jié)用于預(yù)訓(xùn)練word2vec的PTB數(shù)據(jù)集相比,WikiText-2(i)保留了原有的標(biāo)點(diǎn)符號(hào),適合下一句預(yù)測(cè);(ii) 保留原始案例和編號(hào);(iii) 大兩倍以上。

import os
import random
import torch
from d2l import torch as d2l
import os
import random
from mxnet import gluon, np, npx
from d2l import mxnet as d2l

npx.set_np()

在 WikiText-2 數(shù)據(jù)集中,每一行代表一個(gè)段落,其中在任何標(biāo)點(diǎn)符號(hào)及其前面的標(biāo)記之間插入空格。保留至少兩句話的段落。為了簡(jiǎn)單起見(jiàn),為了拆分句子,我們只使用句點(diǎn)作為分隔符。我們將在本節(jié)末尾的練習(xí)中討論更復(fù)雜的句子拆分技術(shù)。

#@save
d2l.DATA_HUB['wikitext-2'] = (
  'https://s3.amazonaws.com/research.metamind.io/wikitext/'
  'wikitext-2-v1.zip', '3c914d17d80b1459be871a5039ac23e752a53cbe')

#@save
def _read_wiki(data_dir):
  file_name = os.path.join(data_dir, 'wiki.train.tokens')
  with open(file_name, 'r') as f:
    lines = f.readlines()
  # Uppercase letters are converted to lowercase ones
  paragraphs = [line.strip().lower().split(' . ')
         for line in lines if len(line.split(' . ')) >= 2]
  random.shuffle(paragraphs)
  return paragraphs
#@save
d2l.DATA_HUB['wikitext-2'] = (
  'https://s3.amazonaws.com/research.metamind.io/wikitext/'
  'wikitext-2-v1.zip', '3c914d17d80b1459be871a5039ac23e752a53cbe')

#@save
def _read_wiki(data_dir):
  file_name = os.path.join(data_dir, 'wiki.train.tokens')
  with open(file_name, 'r') as f:
    lines = f.readlines()
  # Uppercase letters are converted to lowercase ones
  paragraphs = [line.strip().lower().split(' . ')
         for line in lines if len(line.split(' . ')) >= 2]
  random.shuffle(paragraphs)
  return paragraphs

15.9.1。為預(yù)訓(xùn)練任務(wù)定義輔助函數(shù)

下面,我們首先為兩個(gè) BERT 預(yù)訓(xùn)練任務(wù)實(shí)現(xiàn)輔助函數(shù):下一句預(yù)測(cè)和掩碼語(yǔ)言建模。這些輔助函數(shù)將在稍后將原始文本語(yǔ)料庫(kù)轉(zhuǎn)換為理想格式的數(shù)據(jù)集以預(yù)訓(xùn)練 BERT 時(shí)調(diào)用。

15.9.1.1。生成下一句預(yù)測(cè)任務(wù)

根據(jù)15.8.5.2 節(jié)的描述,該 _get_next_sentence函數(shù)為二元分類(lèi)任務(wù)生成一個(gè)訓(xùn)練樣例。

#@save
def _get_next_sentence(sentence, next_sentence, paragraphs):
  if random.random() < 0.5:
    is_next = True
  else:
    # `paragraphs` is a list of lists of lists
    next_sentence = random.choice(random.choice(paragraphs))
    is_next = False
  return sentence, next_sentence, is_next
#@save
def _get_next_sentence(sentence, next_sentence, paragraphs):
  if random.random() < 0.5:
    is_next = True
  else:
    # `paragraphs` is a list of lists of lists
    next_sentence = random.choice(random.choice(paragraphs))
    is_next = False
  return sentence, next_sentence, is_next

以下函數(shù)paragraph通過(guò)調(diào)用該 _get_next_sentence函數(shù)從輸入生成用于下一句預(yù)測(cè)的訓(xùn)練示例。paragraph是一個(gè)句子列表,其中每個(gè)句子都是一個(gè)標(biāo)記列表。該參數(shù) max_len指定預(yù)訓(xùn)練期間 BERT 輸入序列的最大長(zhǎng)度。

#@save
def _get_nsp_data_from_paragraph(paragraph, paragraphs, vocab, max_len):
  nsp_data_from_paragraph = []
  for i in range(len(paragraph) - 1):
    tokens_a, tokens_b, is_next = _get_next_sentence(
      paragraph[i], paragraph[i + 1], paragraphs)
    # Consider 1 '' token and 2 '' tokens
    if len(tokens_a) + len(tokens_b) + 3 > max_len:
      continue
    tokens, segments = d2l.get_tokens_and_segments(tokens_a, tokens_b)
    nsp_data_from_paragraph.append((tokens, segments, is_next))
  return nsp_data_from_paragraph
#@save
def _get_nsp_data_from_paragraph(paragraph, paragraphs, vocab, max_len):
  nsp_data_from_paragraph = []
  for i in range(len(paragraph) - 1):
    tokens_a, tokens_b, is_next = _get_next_sentence(
      paragraph[i], paragraph[i + 1], paragraphs)
    # Consider 1 '' token and 2 '' tokens
    if len(tokens_a) + len(tokens_b) + 3 > max_len:
      continue
    tokens, segments = d2l.get_tokens_and_segments(tokens_a, tokens_b)
    nsp_data_from_paragraph.append((tokens, segments, is_next))
  return nsp_data_from_paragraph

15.9.1.2。生成掩碼語(yǔ)言建模任務(wù)

為了從 BERT 輸入序列為掩碼語(yǔ)言建模任務(wù)生成訓(xùn)練示例,我們定義了以下 _replace_mlm_tokens函數(shù)。在它的輸入中,tokens是代表BERT輸入序列的token列表,candidate_pred_positions 是BERT輸入序列的token索引列表,不包括特殊token(masked語(yǔ)言建模任務(wù)中不預(yù)測(cè)特殊token),num_mlm_preds表示預(yù)測(cè)(召回 15% 的隨機(jī)標(biāo)記來(lái)預(yù)測(cè))。遵循第 15.8.5.1 節(jié)中屏蔽語(yǔ)言建模任務(wù)的定義 ,在每個(gè)預(yù)測(cè)位置,輸入可能被特殊的“”標(biāo)記或隨機(jī)標(biāo)記替換,或者保持不變。最后,該函數(shù)返回可能替換后的輸入標(biāo)記、發(fā)生預(yù)測(cè)的標(biāo)記索引以及這些預(yù)測(cè)的標(biāo)簽。

#@save
def _replace_mlm_tokens(tokens, candidate_pred_positions, num_mlm_preds,
            vocab):
  # For the input of a masked language model, make a new copy of tokens and
  # replace some of them by '' or random tokens
  mlm_input_tokens = [token for token in tokens]
  pred_positions_and_labels = []
  # Shuffle for getting 15% random tokens for prediction in the masked
  # language modeling task
  random.shuffle(candidate_pred_positions)
  for mlm_pred_position in candidate_pred_positions:
    if len(pred_positions_and_labels) >= num_mlm_preds:
      break
    masked_token = None
    # 80% of the time: replace the word with the '' token
    if random.random() < 0.8:
      masked_token = ''
    else:
      # 10% of the time: keep the word unchanged
      if random.random() < 0.5:
        masked_token = tokens[mlm_pred_position]
      # 10% of the time: replace the word with a random word
      else:
        masked_token = random.choice(vocab.idx_to_token)
    mlm_input_tokens[mlm_pred_position] = masked_token
    pred_positions_and_labels.append(
      (mlm_pred_position, tokens[mlm_pred_position]))
  return mlm_input_tokens, pred_positions_and_labels
#@save
def _replace_mlm_tokens(tokens, candidate_pred_positions, num_mlm_preds,
            vocab):
  # For the input of a masked language model, make a new copy of tokens and
  # replace some of them by '' or random tokens
  mlm_input_tokens = [token for token in tokens]
  pred_positions_and_labels = []
  # Shuffle for getting 15% random tokens for prediction in the masked
  # language modeling task
  random.shuffle(candidate_pred_positions)
  for mlm_pred_position in candidate_pred_positions:
    if len(pred_positions_and_labels) >= num_mlm_preds:
      break
    masked_token = None
    # 80% of the time: replace the word with the '' token
    if random.random() < 0.8:
      masked_token = ''
    else:
      # 10% of the time: keep the word unchanged
      if random.random() < 0.5:
        masked_token = tokens[mlm_pred_position]
      # 10% of the time: replace the word with a random word
      else:
        masked_token = random.choice(vocab.idx_to_token)
    mlm_input_tokens[mlm_pred_position] = masked_token
    pred_positions_and_labels.append(
      (mlm_pred_position, tokens[mlm_pred_position]))
  return mlm_input_tokens, pred_positions_and_labels

通過(guò)調(diào)用上述_replace_mlm_tokens函數(shù),以下函數(shù)將 BERT 輸入序列 ( tokens) 作為輸入并返回輸入標(biāo)記的索引(在可能的標(biāo)記替換之后,如第15.8.5.1 節(jié)所述)、發(fā)生預(yù)測(cè)的標(biāo)記索引和標(biāo)簽這些預(yù)測(cè)的指標(biāo)。

#@save
def _get_mlm_data_from_tokens(tokens, vocab):
  candidate_pred_positions = []
  # `tokens` is a list of strings
  for i, token in enumerate(tokens):
    # Special tokens are not predicted in the masked language modeling
    # task
    if token in ['', '']:
      continue
    candidate_pred_positions.append(i)
  # 15% of random tokens are predicted in the masked language modeling task
  num_mlm_preds = max(1, round(len(tokens) * 0.15))
  mlm_input_tokens, pred_positions_and_labels = _replace_mlm_tokens(
    tokens, candidate_pred_positions, num_mlm_preds, vocab)
  pred_positions_and_labels = sorted(pred_positions_and_labels,
                    key=lambda x: x[0])
  pred_positions <

函數(shù) 數(shù)據(jù)集 pytorch
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評(píng)論

查看更多

下載排行

本周

  1. 1矽力杰 Silergy SY7215A 同步升壓調(diào)節(jié)器 規(guī)格書(shū) Datasheet 佰祥電子
  2. 1.12 MB  |  5次下載  |  免費(fèi)
  3. 2HT81696H 內(nèi)置升壓的30W立體聲D類(lèi)音頻功放數(shù)據(jù)手冊(cè)
  4. 1.21 MB   |  1次下載  |  免費(fèi)
  5. 3HTA6863 3W超低噪聲超低功耗單聲道D類(lèi)音頻功率放大器數(shù)據(jù)手冊(cè)
  6. 0.87 MB   |  次下載  |  免費(fèi)
  7. 4南芯 Southchip SC8802C 充電控制器 規(guī)格書(shū) Datasheet 佰祥電子
  8. 88.16 KB  |  次下載  |  免費(fèi)
  9. 5矽力杰 Silergy SY7065 同步升壓轉(zhuǎn)換器 規(guī)格書(shū) Datasheet 佰祥電子
  10. 910.67 KB  |  次下載  |  免費(fèi)
  11. 6矽力杰 Silergy SY7066 同步升壓轉(zhuǎn)換器 規(guī)格書(shū) Datasheet 佰祥電子
  12. 989.14 KB  |  次下載  |  免費(fèi)
  13. 7WD6208A產(chǎn)品規(guī)格書(shū)
  14. 631.24 KB  |  次下載  |  免費(fèi)
  15. 8NB685 26 V,12 A,低靜態(tài)電流,大電流 同步降壓變換器數(shù)據(jù)手冊(cè)
  16. 1.64 MB   |  次下載  |  2 積分

本月

  1. 1EMC PCB設(shè)計(jì)總結(jié)
  2. 0.33 MB   |  12次下載  |  免費(fèi)
  3. 2PD取電芯片 ECP5702規(guī)格書(shū)
  4. 0.88 MB   |  5次下載  |  免費(fèi)
  5. 3矽力杰 Silergy SY7215A 同步升壓調(diào)節(jié)器 規(guī)格書(shū) Datasheet 佰祥電子
  6. 1.12 MB  |  5次下載  |  免費(fèi)
  7. 4氮化鎵GaN FET/GaN HEMT 功率驅(qū)動(dòng)電路選型表
  8. 0.10 MB   |  3次下載  |  免費(fèi)
  9. 5PD取電芯片,可取5/9/12/15/20V電壓ECP5702數(shù)據(jù)手冊(cè)
  10. 0.88 MB   |  3次下載  |  免費(fèi)
  11. 6SY50655 用于高輸入電壓應(yīng)用的偽固定頻率SSR反激式穩(wěn)壓器英文資料
  12. 1.01 MB   |  3次下載  |  免費(fèi)
  13. 7怎么為半導(dǎo)體測(cè)試儀選擇精密放大器
  14. 0.65 MB   |  2次下載  |  免費(fèi)
  15. 8SY52341 次級(jí)側(cè)同步整流英文手冊(cè)
  16. 0.94 MB   |  2次下載  |  免費(fèi)

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935137次下載  |  10 積分
  3. 2開(kāi)源硬件-PMP21529.1-4 開(kāi)關(guān)降壓/升壓雙向直流/直流轉(zhuǎn)換器 PCB layout 設(shè)計(jì)
  4. 1.48MB  |  420064次下載  |  10 積分
  5. 3Altium DXP2002下載入口
  6. 未知  |  233095次下載  |  10 積分
  7. 4電路仿真軟件multisim 10.0免費(fèi)下載
  8. 340992  |  191469次下載  |  10 積分
  9. 5十天學(xué)會(huì)AVR單片機(jī)與C語(yǔ)言視頻教程 下載
  10. 158M  |  183360次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81606次下載  |  10 積分
  13. 7Keil工具M(jìn)DK-Arm免費(fèi)下載
  14. 0.02 MB  |  73832次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65991次下載  |  10 積分