電子發(fā)燒友App

硬聲App

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

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

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>PyTorch教程16.4之自然語言推理和數(shù)據(jù)集

PyTorch教程16.4之自然語言推理和數(shù)據(jù)集

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

資料介紹

16.1 節(jié)中,我們討論了情感分析的問題。該任務(wù)旨在將單個(gè)文本序列分類為預(yù)定義的類別,例如一組情感極性。然而,當(dāng)需要決定一個(gè)句子是否可以從另一個(gè)句子推斷出來,或者通過識(shí)別語義等同的句子來消除冗余時(shí),知道如何對(duì)一個(gè)文本序列進(jìn)行分類是不夠的。相反,我們需要能夠?qū)Τ蓪?duì)的文本序列進(jìn)行推理。

16.4.1。自然語言推理

自然語言推理研究是否可以從前提中推斷出假設(shè),其中兩者都是文本序列。換句話說,自然語言推理決定了一對(duì)文本序列之間的邏輯關(guān)系。這種關(guān)系通常分為三種類型:

  • 蘊(yùn)涵:假設(shè)可以從前提中推導(dǎo)出來。

  • 矛盾:可以從前提推導(dǎo)出假設(shè)的否定。

  • 中性:所有其他情況。

自然語言推理也稱為識(shí)別文本蘊(yùn)含任務(wù)。例如,下面的一對(duì)將被標(biāo)記為 蘊(yùn)涵,因?yàn)榧僭O(shè)中的“示愛”可以從前提中的“互相擁抱”中推導(dǎo)出來。

前提:兩個(gè)女人互相擁抱。

假設(shè):兩個(gè)女人正在秀恩愛。

下面是一個(gè)矛盾的例子,因?yàn)椤皉unning the coding example”表示“not sleeping”而不是“sleeping”。

前提:一個(gè)人正在運(yùn)行來自 Dive into Deep Learning 的編碼示例。

假設(shè):這個(gè)人正在睡覺。

第三個(gè)例子顯示了一種中立關(guān)系,因?yàn)椤盀槲覀儽硌荨边@一事實(shí)不能推斷出“著名”或“不著名”。

前提:音樂家正在為我們表演。

假設(shè):音樂家很有名。

自然語言推理一直是理解自然語言的中心話題它享有從信息檢索到開放域問答的廣泛應(yīng)用。為了研究這個(gè)問題,我們將從調(diào)查一個(gè)流行的自然語言推理基準(zhǔn)數(shù)據(jù)集開始。

16.4.2。斯坦福自然語言推理 (SNLI) 數(shù)據(jù)集

斯坦福自然語言推理 (SNLI) 語料庫是超過 500000 個(gè)帶標(biāo)簽的英語句子對(duì)的集合 Bowman等人,2015 年。我們將提取的 SNLI 數(shù)據(jù)集下載并存儲(chǔ)在路徑中../data/snli_1.0

import os
import re
import torch
from torch import nn
from d2l import torch as d2l

#@save
d2l.DATA_HUB['SNLI'] = (
  'https://nlp.stanford.edu/projects/snli/snli_1.0.zip',
  '9fcde07509c7e87ec61c640c1b2753d9041758e4')

data_dir = d2l.download_extract('SNLI')
Downloading ../data/snli_1.0.zip from https://nlp.stanford.edu/projects/snli/snli_1.0.zip...
import os
import re
from mxnet import gluon, np, npx
from d2l import mxnet as d2l

npx.set_np()

#@save
d2l.DATA_HUB['SNLI'] = (
  'https://nlp.stanford.edu/projects/snli/snli_1.0.zip',
  '9fcde07509c7e87ec61c640c1b2753d9041758e4')

data_dir = d2l.download_extract('SNLI')

16.4.2.1。讀取數(shù)據(jù)集

原始 SNLI 數(shù)據(jù)集包含的信息比我們?cè)趯?shí)驗(yàn)中真正需要的信息豐富得多。因此,我們定義了一個(gè)函數(shù)read_snli 來僅提取部分?jǐn)?shù)據(jù)集,然后返回前提、假設(shè)及其標(biāo)簽的列表。

#@save
def read_snli(data_dir, is_train):
  """Read the SNLI dataset into premises, hypotheses, and labels."""
  def extract_text(s):
    # Remove information that will not be used by us
    s = re.sub('\\(', '', s)
    s = re.sub('\\)', '', s)
    # Substitute two or more consecutive whitespace with space
    s = re.sub('\\s{2,}', ' ', s)
    return s.strip()
  label_set = {'entailment': 0, 'contradiction': 1, 'neutral': 2}
  file_name = os.path.join(data_dir, 'snli_1.0_train.txt'
               if is_train else 'snli_1.0_test.txt')
  with open(file_name, 'r') as f:
    rows = [row.split('\t') for row in f.readlines()[1:]]
  premises = [extract_text(row[1]) for row in rows if row[0] in label_set]
  hypotheses = [extract_text(row[2]) for row in rows if row[0] in label_set]
  labels = [label_set[row[0]] for row in rows if row[0] in label_set]
  return premises, hypotheses, labels
#@save
def read_snli(data_dir, is_train):
  """Read the SNLI dataset into premises, hypotheses, and labels."""
  def extract_text(s):
    # Remove information that will not be used by us
    s = re.sub('\\(', '', s)
    s = re.sub('\\)', '', s)
    # Substitute two or more consecutive whitespace with space
    s = re.sub('\\s{2,}', ' ', s)
    return s.strip()
  label_set = {'entailment': 0, 'contradiction': 1, 'neutral': 2}
  file_name = os.path.join(data_dir, 'snli_1.0_train.txt'
               if is_train else 'snli_1.0_test.txt')
  with open(file_name, 'r') as f:
    rows = [row.split('\t') for row in f.readlines()[1:]]
  premises = [extract_text(row[1]) for row in rows if row[0] in label_set]
  hypotheses = [extract_text(row[2]) for row in rows if row[0] in label_set]
  labels = [label_set[row[0]] for row in rows if row[0] in label_set]
  return premises, hypotheses, labels

現(xiàn)在讓我們打印前 3 對(duì)前提和假設(shè),以及它們的標(biāo)簽(“0”、“1”和“2”分別對(duì)應(yīng)“蘊(yùn)含”、“矛盾”和“中性”)。

train_data = read_snli(data_dir, is_train=True)
for x0, x1, y in zip(train_data[0][:3], train_data[1][:3], train_data[2][:3]):
  print('premise:', x0)
  print('hypothesis:', x1)
  print('label:', y)
premise: A person on a horse jumps over a broken down airplane .
hypothesis: A person is training his horse for a competition .
label: 2
premise: A person on a horse jumps over a broken down airplane .
hypothesis: A person is at a diner , ordering an omelette .
label: 1
premise: A person on a horse jumps over a broken down airplane .
hypothesis: A person is outdoors , on a horse .
label: 0
train_data = read_snli(data_dir, is_train=True)
for x0, x1, y in zip(train_data[0][:3], train_data[1][:3], train_data[2][:3]):
  print('premise:', x0)
  print('hypothesis:', x1)
  print('label:', y)
premise: A person on a horse jumps over a broken down airplane .
hypothesis: A person is training his horse for a competition .
label: 2
premise: A person on a horse jumps over a broken down airplane .
hypothesis: A person is at a diner , ordering an omelette .
label: 1
premise: A person on a horse jumps over a broken down airplane .
hypothesis: A person is outdoors , on a horse .
label: 0

訓(xùn)練集約550000對(duì),測(cè)試集約10000對(duì)。下圖表明“蘊(yùn)含”、“矛盾”、“中性”這三個(gè)標(biāo)簽在訓(xùn)練集和測(cè)試集上都是均衡的。

test_data = read_snli(data_dir, is_train=False)
for data in [train_data, test_data]:
  print([[row for row in data[2]].count(i) for i in range(3)])
[183416, 183187, 182764]
[3368, 3237, 3219]
test_data = read_snli(data_dir, is_train=False)
for data in [train_data, test_data]:
  print([[row for row in data[2]].count(i) for i in range(3)])
[183416, 183187, 182764]
[3368, 3237, 3219]

16.4.2.2。定義用于加載數(shù)據(jù)集的類

下面我們繼承DatasetGluon中的類定義一個(gè)加載SNLI數(shù)據(jù)集的類。類構(gòu)造函數(shù)中的參數(shù)num_steps指定文本序列的長度,以便每個(gè)小批量序列具有相同的形狀。換句話說,num_steps較長序列中第一個(gè)之后的標(biāo)記被修剪,而特殊標(biāo)記“”將附加到較短的序列,直到它們的長度變?yōu)?/font>num_steps. 通過實(shí)現(xiàn)該__getitem__ 功能,我們可以任意訪問前提、假設(shè)和帶有索引的標(biāo)簽idx。

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

評(píng)論

查看更多

下載排行

本周

  1. 1無鉛焊接的可靠性
  2. 1.03 MB   |  5次下載  |  1 積分
  3. 2GBT1094.11-2022電力變壓器第11部分:干式變壓器
  4. 14.12 MB   |  3次下載  |  免費(fèi)
  5. 3PT500齒輪傳動(dòng)動(dòng)力學(xué)綜合測(cè)試實(shí)驗(yàn)臺(tái)
  6. 0.16 MB   |  3次下載  |  免費(fèi)
  7. 4爬電距離和電氣間隙計(jì)算
  8. 0.75 MB   |  2次下載  |  1 積分
  9. 5SX1308應(yīng)用電路圖與SX1308升壓電路圖
  10. 0.18 MB   |  1次下載  |  免費(fèi)
  11. 6ADC參數(shù)單位換算
  12. 761.94KB   |  1次下載  |  免費(fèi)
  13. 7串口工具UartAssist5.0.exe
  14. 0.60 MB   |  1次下載  |  免費(fèi)
  15. 8UCC38C42 25瓦自諧振復(fù)位正激變換器
  16. 320.6KB   |  1次下載  |  免費(fèi)

本月

  1. 1ACDC變換器的原理圖免費(fèi)下載
  2. 0.26 MB   |  65次下載  |  免費(fèi)
  3. 2無刷電機(jī)控制方案設(shè)計(jì)合作
  4. 1.05 MB   |  22次下載  |  免費(fèi)
  5. 3美的超薄電磁爐TM-S1-09B主板原理圖
  6. 0.08 MB   |  20次下載  |  免費(fèi)
  7. 4純電動(dòng)汽?的主要部件及?作原理
  8. 5.76 MB   |  11次下載  |  5 積分
  9. 5GP328和GP88S對(duì)講機(jī)的維修實(shí)列資料合集免費(fèi)下載
  10. 0.03 MB   |  10次下載  |  10 積分
  11. 6舒爾SLX4無線話筒接收機(jī)原理圖:二次變頻超外差部分
  12. 0.27 MB   |  8次下載  |  免費(fèi)
  13. 7IP5385_DEMO開發(fā)資料
  14. 1.96 MB   |  7次下載  |  2 積分
  15. 8i.MX Linux開發(fā)實(shí)戰(zhàn)指南—基于野火i.MX系列開發(fā)板
  16. 17.86 MB   |  6次下載  |  免費(fèi)

總榜

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