前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Github 项目推荐 | PyTorch 文本工具库/数据集

Github 项目推荐 | PyTorch 文本工具库/数据集

作者头像
AI研习社
发布2018-07-26 14:34:57
8400
发布2018-07-26 14:34:57
举报
文章被收录于专栏:AI研习社AI研习社

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:

pip install pytorch-nlp

文档

完整文档地址如下:

https://pytorchnlp.readthedocs.io/

加载数据集

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

举例,加载 IMDB 数据集:

from torchnlp.datasets import imdb_dataset

# Load the imdb training dataset
train = imdb_dataset(train=True)
train[0]  # 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 在遇到空白字符时将文本分解为条目:

from torchnlp.text_encoders import WhitespaceEncoder

# 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

from torchnlp.word_to_vector import FastText

vectors = FastText()
# Load vectors for any word as a `torch.FloatTensor`
vectors['hello']  # RETURNS: [torch.FloatTensor of size 100]

计算度量

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

最后,计算通用指标,如 BLEU 分数。

from torchnlp.metrics import get_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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AI研习社 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装
  • 文档
  • 加载数据集
  • (http://pytorchnlp.readthedocs.io/en/latest/source/torchnlp.datasets.html)
  • 应用神经网络层:
  • Encode Text
  • 加载 Word Vectors
  • 加载 FastText
  • 计算度量
相关产品与服务
NLP 服务
NLP 服务(Natural Language Process,NLP)深度整合了腾讯内部的 NLP 技术,提供多项智能文本处理和文本生成能力,包括词法分析、相似词召回、词相似度、句子相似度、文本润色、句子纠错、文本补全、句子生成等。满足各行业的文本智能需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档