哈哈哈哈哈操欧洲电影,久草网在线,亚洲久久熟女熟妇视频,麻豆精品色,久久福利在线视频,日韩中文字幕的,淫乱毛视频一区,亚洲成人一二三,中文人妻日韩精品电影

電子發(fā)燒友App

硬聲App

掃碼添加小助手

加入工程師交流群

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

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

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>PyTorch教程9.2之將原始文本轉(zhuǎn)換為序列數(shù)據(jù)

PyTorch教程9.2之將原始文本轉(zhuǎn)換為序列數(shù)據(jù)

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

資料介紹

在本書中,我們經(jīng)常會使用表示為單詞、字符或單詞序列的文本數(shù)據(jù)。首先,我們需要一些基本工具來將原始文本轉(zhuǎn)換為適當(dāng)形式的序列。典型的預(yù)處理流水線執(zhí)行以下步驟:

  1. 將文本作為字符串加載到內(nèi)存中。

  2. 將字符串拆分為標(biāo)記(例如,單詞或字符)。

  3. 構(gòu)建一個詞匯詞典,將每個詞匯元素與一個數(shù)字索引相關(guān)聯(lián)。

  4. 將文本轉(zhuǎn)換為數(shù)字索引序列。

import collections
import random
import re
import torch
from d2l import torch as d2l
import collections
import random
import re
from mxnet import np, npx
from d2l import mxnet as d2l

npx.set_np()
import collections
import random
import re
import jax
from jax import numpy as jnp
from d2l import jax as d2l
No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
import collections
import random
import re
import tensorflow as tf
from d2l import tensorflow as d2l

9.2.1. 讀取數(shù)據(jù)集

在這里,我們將使用 HG Wells 的The Time Machine,這是一本 30000 多字的書。雖然實際應(yīng)用程序通常會涉及大得多的數(shù)據(jù)集,但這足以演示預(yù)處理管道。以下_download方法將原始文本讀入字符串。

class TimeMachine(d2l.DataModule): #@save
  """The Time Machine dataset."""
  def _download(self):
    fname = d2l.download(d2l.DATA_URL + 'timemachine.txt', self.root,
               '090b5e7e70c295757f55df93cb0a180b9691891a')
    with open(fname) as f:
      return f.read()

data = TimeMachine()
raw_text = data._download()
raw_text[:60]
'時間機器,HG Wells [1898]nnnnnInnnThe Time Tra'
class TimeMachine(d2l.DataModule): #@save
  """The Time Machine dataset."""
  def _download(self):
    fname = d2l.download(d2l.DATA_URL + 'timemachine.txt', self.root,
               '090b5e7e70c295757f55df93cb0a180b9691891a')
    with open(fname) as f:
      return f.read()

data = TimeMachine()
raw_text = data._download()
raw_text[:60]
Downloading ../data/timemachine.txt from http://d2l-data.s3-accelerate.amazonaws.com/timemachine.txt...
'The Time Machine, by H. G. Wells [1898]nnnnnInnnThe Time Tra'
class TimeMachine(d2l.DataModule): #@save
  """The Time Machine dataset."""
  def _download(self):
    fname = d2l.download(d2l.DATA_URL + 'timemachine.txt', self.root,
               '090b5e7e70c295757f55df93cb0a180b9691891a')
    with open(fname) as f:
      return f.read()

data = TimeMachine()
raw_text = data._download()
raw_text[:60]
'The Time Machine, by H. G. Wells [1898]nnnnnInnnThe Time Tra'
class TimeMachine(d2l.DataModule): #@save
  """The Time Machine dataset."""
  def _download(self):
    fname = d2l.download(d2l.DATA_URL + 'timemachine.txt', self.root,
               '090b5e7e70c295757f55df93cb0a180b9691891a')
    with open(fname) as f:
      return f.read()

data = TimeMachine()
raw_text = data._download()
raw_text[:60]
'The Time Machine, by H. G. Wells [1898]nnnnnInnnThe Time Tra'

為簡單起見,我們在預(yù)處理原始文本時忽略標(biāo)點符號和大寫字母。

@d2l.add_to_class(TimeMachine) #@save
def _preprocess(self, text):
  return re.sub('[^A-Za-z]+', ' ', text).lower()

text = data._preprocess(raw_text)
text[:60]
'the time machine by h g wells i the time traveller for so it'
@d2l.add_to_class(TimeMachine) #@save
def _preprocess(self, text):
  return re.sub('[^A-Za-z]+', ' ', text).lower()

text = data._preprocess(raw_text)
text[:60]
'the time machine by h g wells i the time traveller for so it'
@d2l.add_to_class(TimeMachine) #@save
def _preprocess(self, text):
  return re.sub('[^A-Za-z]+', ' ', text).lower()

text = data._preprocess(raw_text)
text[:60]
'the time machine by h g wells i the time traveller for so it'
@d2l.add_to_class(TimeMachine) #@save
def _preprocess(self, text):
  return re.sub('[^A-Za-z]+', ' ', text).lower()

text = data._preprocess(raw_text)
text[:60]
'the time machine by h g wells i the time traveller for so it'

9.2.2. 代幣化

標(biāo)記是文本的原子(不可分割)單元。每個時間步對應(yīng) 1 個 token,但究竟什么是 token 是一種設(shè)計選擇。例如,我們可以將句子“Baby needs a new pair of shoes”表示為一個包含 7 個單詞的序列,其中所有單詞的集合包含一個很大的詞匯表(通常是數(shù)萬或數(shù)十萬個單詞)。或者我們將同一個句子表示為更長的 30 個字符序列,使用更小的詞匯表(只有 256 個不同的 ASCII 字符)。下面,我們將預(yù)處理后的文本標(biāo)記為一系列字符。

@d2l.add_to_class(TimeMachine) #@save
def _tokenize(self, text):
  return list(text)

tokens = data._tokenize(text)
','.join(tokens[:30])
't,h,e, ,t,i,m,e, ,m,a,c,h,i,n,e, ,b,y, ,h, ,g, ,w,e,l,l,s, '
@d2l.add_to_class(TimeMachine) #@save
def _tokenize(self, text):
  return list(text)

tokens = data._tokenize(text)
','.join(tokens[:30])
't,h,e, ,t,i,m,e, ,m,a,c,h,i,n,e, ,b,y, ,h, ,g, ,w,e,l,l,s, '
@d2l.add_to_class(TimeMachine) #@save
def _tokenize(self, text):
  return list(text)

tokens = data._tokenize(text)
','.join(tokens[:30])
't,h,e, ,t,i,m,e, ,m,a,c,h,i,n,e, ,b,y, ,h, ,g, ,w,e,l,l,s, '
@d2l.add_to_class(TimeMachine) #@save
def _tokenize(self, text):
  return list(text)

tokens = data._tokenize(text)
','.join(tokens[:30])
't,h,e, ,t,i,m,e, ,m,a,c,h,i,n,e, ,b,y, ,h, ,g, ,w,e,l,l,s, '

9.2.3. 詞匯

這些標(biāo)記仍然是字符串。然而,我們模型的輸入最終必須由數(shù)值輸入組成。接下來,我們介紹一個用于構(gòu)建詞匯表的類,即,將每個不同的標(biāo)記值與唯一索引相關(guān)聯(lián)的對象。首先,我們確定訓(xùn)練語料庫中的唯一標(biāo)記集然后我們?yōu)槊總€唯一標(biāo)記分配一個數(shù)字索引。為方便起見,通常會刪除不常用的詞匯元素。Whenever we encounter a token at training or test time that had not been previously seen or was dropped from the vocabulary, we represent it by a special “” token, signifying that this is an unknown value.

class Vocab: #@save
  """Vocabulary for text."""
  def __init__(self, tokens=[], min_freq=0, reserved_tokens=[]):
    # Flatten a 2D list if needed
    if tokens and isinstance(tokens[0], list):
      tokens = [token for line in tokens for token in line]
    # Count token frequencies
    counter = collections.Counter(tokens)
    self.token_freqs = sorted(counter.items(), key=lambda x: x[1],
                 reverse=True)
    # The list of unique tokens
    self.idx_to_token = list(sorted(set([''] + reserved_tokens + [
      token for token, freq in self.token_freqs if freq >= min_freq])))
    self.token_to_idx = {token: idx
               for idx, token in enumerate(self.idx_to_token)}

  def __len__(self):
    return len(self.idx_to_token)

  def __getitem__(self, tokens):
    if not isinstance(tokens, (list, tuple)):
      return self.token_to_idx.get(tokens,
字符串 序列 文本 pytorch
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

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

評論

查看更多

下載排行

本周

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

本月

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

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935137次下載  |  10 積分
  3. 2開源硬件-PMP21529.1-4 開關(guān)降壓/升壓雙向直流/直流轉(zhuǎn)換器 PCB layout 設(shè)計
  4. 1.48MB  |  420064次下載  |  10 積分
  5. 3Altium DXP2002下載入口
  6. 未知  |  233095次下載  |  10 積分
  7. 4電路仿真軟件multisim 10.0免費下載
  8. 340992  |  191469次下載  |  10 積分
  9. 5十天學(xué)會AVR單片機與C語言視頻教程 下載
  10. 158M  |  183360次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81606次下載  |  10 積分
  13. 7Keil工具MDK-Arm免費下載
  14. 0.02 MB  |  73832次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65991次下載  |  10 積分
政和县| 米易县| 海晏县| 岗巴县| 手机| 乌兰县| 谢通门县| 宣化县| 昌都县| 孟州市| 文安县| 岑巩县| 获嘉县| 海伦市| 甘孜| 若尔盖县| 济宁市| 鞍山市| 宕昌县| 札达县| 武宁县| 镇康县| 五常市| 县级市| 逊克县| 惠州市| 泽州县| 高州市| 新疆| 西峡县| 黔西| 常德市| 定西市| 太湖县| 邵阳县| 平凉市| 徐汇区| 衡山县| 简阳市| 昌江| 孙吴县|