當前位置:
首頁 > 知識 > PyTorch 文本工具庫/數據集

PyTorch 文本工具庫/數據集

PyTorch-NLP,簡稱 torchnlp,是一個神經網路層、文本處理模塊和數據集庫,旨在加速自然語言處理的研究。

有興趣加入該社區的開發者可以在 Gitter(https://gitter.im/PyTorch-NLP/Lobby) 和 Google Group(https://groups.google.com/forum/#!forum/pytorch-nlp) 上跟作者交流。

Github 地址:

https://github.com/PetrochukM/PyTorch-NLP

安裝

先確保你已經安裝好了 Python 3.5+ 、PyTorch 0.2.0 或者更新的版本,你可以通過 pip 來安裝pytorch-nlp:

pipinstallpytorch-nlp


文檔

完整文檔地址如下:

https://pytorchnlp.readthedocs.io/


載入數據集

(http://pytorchnlp.readthedocs.io/en/latest/source/torchnlp.datasets.html)

舉例,載入 IMDB 數據集:

fromtorchnlp.datasetsimportimdb_dataset

# Load the imdb training dataset

train = imdb_dataset(train=True)

train[]# RETURNS: {"text": "For a movie that gets..", "sentiment": "pos"}


應用神經網路層:

(http://pytorchnlp.readthedocs.io/en/latest/source/torchnlp.nn.html)

例如,從神經網路包中,應用一個簡單循環單元(SRU):

from torchnlp.nn import SRU

import torch

input_ = torch.autograd.Variable(torch.randn(6, 3, 10))

sru = SRU(10, 20)

#Apply a Simple Recurrent Unit to `input_`

sru(input_)

#RETURNS: (

#output [torch.FloatTensor (6x3x20)],

#hidden_state [torch.FloatTensor (2x3x20)]

#)


Encode Text

(http://pytorchnlp.readthedocs.io/en/latest/source/torchnlp.text_encoders.html)

WhitespaceEncoder在遇到空白字元時將文本分解為條目:

fromtorchnlp.text_encodersimportWhitespaceEncoder

# Create a `WhitespaceEncoder` with a corpus of text

encoder = WhitespaceEncoder(["now this ain"t funny","so don"t you dare laugh"])

# Encode and decode phrases

encoder.encode("this ain"t funny.")# RETURNS: torch.LongTensor([6, 7, 1])

encoder.decode(encoder.encode("This ain"t funny."))# RETURNS: "this ain"t funny."


載入 Word Vectors

(http://pytorchnlp.readthedocs.io/en/latest/source/torchnlp.word_to_vector.html)

載入 FastText

fromtorchnlp.word_to_vectorimportFastText

vectors = FastText()

# Load vectorsforanywordasa`torch.FloatTensor`

vectors["hello"] # RETURNS: [torch.FloatTensor of size100]


計算度量

http://pytorchnlp.readthedocs.io/en/latest/source/torchnlp.metrics.html

最後,計算通用指標,如 BLEU 分數。

fromtorchnlp.metricsimportget_moses_multi_bleu

hypotheses = ["The brown fox jumps over the dog 笑"]

references = ["The quick brown fox jumps over the lazy dog 笑"]

# Compute BLEU score with the official BLEU perl script

get_moses_multi_bleu(hypotheses, references, lowercase=True)# RETURNS: 47.9

4 月 AI 求職季

8 大明星企業

10 場分享盛宴

20 小時獨門秘籍

4.10-4.19,我們準時相約!

新人福利

關注 AI 研習社(okweiwu),回復1領取

【超過 1000G 神經網路 / AI / 大數據資料】

新加坡國立大學霍華德:NLP 都有哪些有意思的事兒?


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 AI研習社 的精彩文章:

加速 AI 2.0,ARC 推理挑戰賽等你來戰!
TensorFlow 開發者峰會:推出 TensorFlow.js,支持 Swift,TF 將更易於使用

TAG:AI研習社 |