在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

電子發燒友App

硬聲App

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示
電子發燒友網>電子資料下載>電子資料>PyTorch教程16.4之自然語言推理和數據集

PyTorch教程16.4之自然語言推理和數據集

2023-06-05 | pdf | 0.13 MB | 次下載 | 免費

資料介紹

16.1 節中,我們討論了情感分析的問題。該任務旨在將單個文本序列分類為預定義的類別,例如一組情感極性。然而,當需要決定一個句子是否可以從另一個句子推斷出來,或者通過識別語義等同的句子來消除冗余時,知道如何對一個文本序列進行分類是不夠的。相反,我們需要能夠對成對的文本序列進行推理。

16.4.1。自然語言推理

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

  • 蘊涵:假設可以從前提中推導出來。

  • 矛盾:可以從前提推導出假設的否定。

  • 中性:所有其他情況。

自然語言推理也稱為識別文本蘊含任務。例如,下面的一對將被標記為 蘊涵,因為假設中的“示愛”可以從前提中的“互相擁抱”中推導出來。

前提:兩個女人互相擁抱。

假設:兩個女人正在秀恩愛。

下面是一個矛盾的例子,因為“running the coding example”表示“not sleeping”而不是“sleeping”。

前提:一個人正在運行來自 Dive into Deep Learning 的編碼示例。

假設:這個人正在睡覺。

第三個例子顯示了一種中立關系,因為“為我們表演”這一事實不能推斷出“著名”或“不著名”。

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

假設:音樂家很有名。

自然語言推理一直是理解自然語言的中心話題。它享有從信息檢索到開放域問答的廣泛應用。為了研究這個問題,我們將從調查一個流行的自然語言推理基準數據集開始。

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

斯坦福自然語言推理 (SNLI) 語料庫是超過 500000 個帶標簽的英語句子對的集合 Bowman等人,2015 年。我們將提取的 SNLI 數據集下載并存儲在路徑中../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。讀取數據集

原始 SNLI 數據集包含的信息比我們在實驗中真正需要的信息豐富得多。因此,我們定義了一個函數read_snli 來僅提取部分數據集,然后返回前提、假設及其標簽的列表。

#@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

現在讓我們打印前 3 對前提和假設,以及它們的標簽(“0”、“1”和“2”分別對應“蘊含”、“矛盾”和“中性”)。

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

訓練集約550000對,測試集約10000對。下圖表明“蘊含”、“矛盾”、“中性”這三個標簽在訓練集和測試集上都是均衡的。

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。定義用于加載數據集的類

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

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

評論

查看更多

下載排行

本周

  1. 1涂鴉各WiFi模塊原理圖加PCB封裝
  2. 11.75 MB   |  76次下載  |  1 積分
  3. 2錦銳科技CA51F2 SDK開發包
  4. 24.06 MB   |  29次下載  |  1 積分
  5. 3錦銳CA51F005 SDK開發包
  6. 19.47 MB   |  3次下載  |  1 積分
  7. 4蘋果iphone 11電路原理圖
  8. 4.98 MB   |  3次下載  |  2 積分
  9. 5基礎模擬電子電路
  10. 3.80 MB   |  3次下載  |  1 積分
  11. 6RA-Eco-RA6M4-100PIN-V1.0開發板資料
  12. 34.89 MB  |  1次下載  |  免費
  13. 7STM32F3系列、STM32F4系列、STM32L4系列和STM32L4+系列Cortex-M4編程手冊
  14. 3.32 MB   |  1次下載  |  免費
  15. 8聯想A820t手機維修圖紙包括主板原理圖 尾板原理圖 點位圖
  16. 0.62 MB   |  次下載  |  5 積分

本月

  1. 1AI智能眼鏡產業鏈分析
  2. 4.43 MB   |  383次下載  |  免費
  3. 2蘇泊爾電磁爐線路的電路原理圖資料合集
  4. 2.02 MB   |  296次下載  |  5 積分
  5. 3貼片三極管上的印字與真實名稱的對照表詳細說明
  6. 0.50 MB   |  94次下載  |  1 積分
  7. 4長虹液晶電視R-HS310B-5HF01的電源板電路原理圖
  8. 0.46 MB   |  91次下載  |  5 積分
  9. 5涂鴉各WiFi模塊原理圖加PCB封裝
  10. 11.75 MB   |  76次下載  |  1 積分
  11. 6錦銳科技CA51F2 SDK開發包
  12. 24.06 MB   |  29次下載  |  1 積分
  13. 7AO4803A雙P通道增強型場效應晶體管的數據手冊
  14. 0.11 MB   |  28次下載  |  2 積分
  15. 8長虹液晶彩電LS29機芯的技術資料說明
  16. 3.42 MB   |  16次下載  |  2 積分

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935127次下載  |  10 積分
  3. 2開源硬件-PMP21529.1-4 開關降壓/升壓雙向直流/直流轉換器 PCB layout 設計
  4. 1.48MB  |  420064次下載  |  10 積分
  5. 3Altium DXP2002下載入口
  6. 未知  |  233089次下載  |  10 積分
  7. 4電路仿真軟件multisim 10.0免費下載
  8. 340992  |  191388次下載  |  10 積分
  9. 5十天學會AVR單片機與C語言視頻教程 下載
  10. 158M  |  183342次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81588次下載  |  10 積分
  13. 7Keil工具MDK-Arm免費下載
  14. 0.02 MB  |  73815次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65988次下載  |  10 積分
主站蜘蛛池模板: 狠狠色噜噜狠狠狠狠97不卡 | 久久综合九色婷婷97 | 最新中文字幕在线资源 | 天天操狠狠 | 亚洲4区| 色麒麟影院 | 色偷偷97 | www.午夜色| 日韩高清性爽一级毛片免费 | 理论片国产| 优优国产在线视频 | 欧美亚洲综合图区在线 | 一级欧美在线的视频 | 在线免费看黄的网站 | 久久夜色精品国产尤物 | 中文字幕在线观看第一页 | 美女张开大腿让男人桶 | 国产婷婷综合在线精品尤物 | 毛片网站网址 | 49vv婷婷网| 欧美一区二区三区免费 | 欲色影视香色天天影视来 | 综合五月天婷婷丁香 | 猫色网站| 九九精品影院 | 性喷潮久久久久久久久 | 天天操天天摸天天干 | 欧美三级大片在线观看 | 亚洲精品一区二区中文 | 欧美整片第一页 | 婷婷综合 在线 | 特级毛片a级毛免费播放 | 日本69xxⅹxxxxxx19| 三级黄色在线观看 | 欧美资源在线观看 | 色老头性xxxx老头视频 | jlzzjlzz亚洲大全 | 午夜影院普通 | 五月天婷婷一区二区三区久久 | 狠狠狠操 | 高清欧美性xxxx成熟 |