Pytorch 도구 체인이 성숙하면 이제 이와 같은 저장소를 보관해야합니다. 이 툴킷의 모든 부분에 대해보다 개발 된 옵션을 찾을 수 있습니다.
행복한 발전!
이 저장소를 무시하고 계속 개발하고 싶다면 저에게 연락하십시오. "petrochukm [at] gmail.com"에서 저에게 연락 할 수 있습니다.

Pytorch-NLP 또는 torchnlp 는 Pytorch NLP를위한 기본 유틸리티 라이브러리입니다. torchnlp Pytorch를 확장하여 기본 텍스트 데이터 처리 기능을 제공합니다.
Chloe Yeo의 로고, WellSaid Labs의 기업 후원
Python 3.6+ 및 Pytorch 1.0+가 있는지 확인하십시오. 그런 다음 PIP를 사용하여 pytorch-nlp 설치할 수 있습니다.
pip install pytorch - nlp또는 다음을 통해 최신 코드를 설치하려면
pip install git + https : // github . com / PetrochukM / PyTorch - NLP . git Pytorch-NLP에 대한 전체 문서는 ReadThedocs 웹 사이트를 통해 제공됩니다.
NLP 데이터 파이프 라인 내에서 이러한 기본 단계를 구현하려고합니다.
예를 들어 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'}예를 들어 사용자 정의 데이터 세트를로드하십시오.
from pathlib import Path
from torchnlp . download import download_file_maybe_extract
directory_path = Path ( 'data/' )
train_file_path = Path ( 'trees/train.txt' )
download_file_maybe_extract (
url = 'http://nlp.stanford.edu/sentiment/trainDevTestTrees_PTB.zip' ,
directory = directory_path ,
check_files = [ train_file_path ])
open ( directory_path / train_file_path )우리가 당신을 위해 캐싱을 처리 할 것을 걱정하지 마십시오!
텍스트를 텐서로 토큰 화하고 인코딩하십시오.
예를 들어, WhitespaceEncoder 공백 문자가 발생할 때마다 텍스트를 토큰으로 나눕니다.
from torchnlp . encoders . text import WhitespaceEncoder
loaded_data = [ "now this ain't funny" , "so don't you dare laugh" ]
encoder = WhitespaceEncoder ( loaded_data )
encoded_data = [ encoder . encode ( example ) for example in loaded_data ]로드 및 인코딩 된 데이터를 손에 넣으면 데이터 세트를 배치하려고합니다.
import torch
from torchnlp . samplers import BucketBatchSampler
from torchnlp . utils import collate_tensors
from torchnlp . encoders . text import stack_and_pad_tensors
encoded_data = [ torch . randn ( 2 ), torch . randn ( 3 ), torch . randn ( 4 ), torch . randn ( 5 )]
train_sampler = torch . utils . data . sampler . SequentialSampler ( encoded_data )
train_batch_sampler = BucketBatchSampler (
train_sampler , batch_size = 2 , drop_last = False , sort_key = lambda i : encoded_data [ i ]. shape [ 0 ])
batches = [[ encoded_data [ i ] for i in batch ] for batch in train_batch_sampler ]
batches = [ collate_tensors ( batch , stack_tensors = stack_and_pad_tensors ) for batch in batches ] Pytorch-NLP는 다양한 길이의 순차적 입력을 지원하기 위해 Pytorch의 기존 torch.utils.data.sampler , torch.stack 및 default_collate 위에 구축됩니다!
배치를 손에 들고 Pytorch를 사용하여 그라디언트 하강을 사용하여 모델을 개발하고 훈련시킬 수 있습니다. 예를 들어, 스탠포드 자연 언어 추론 (SNLI) 코퍼스에 대한 교육을위한이 예제 코드를 확인하십시오.
Pytorch-NLP에는 몇 가지 NLP 중심 유틸리티 패키지가 있습니다. ?
이제 파이프 라인을 설정하면 일부 기능이 결정적으로 실행되도록 할 수 있습니다. fork_rng 로 무작위로 코드를 감싸면 다음과 같이 갈 수 있습니다.
import random
import numpy
import torch
from torchnlp . random import fork_rng
with fork_rng ( seed = 123 ): # Ensure determinism
print ( 'Random:' , random . randint ( 1 , 2 ** 31 ))
print ( 'Numpy:' , numpy . random . randint ( 1 , 2 ** 31 ))
print ( 'Torch:' , int ( torch . randint ( 1 , 2 ** 31 , ( 1 ,))))이것은 항상 인쇄됩니다 :
Random: 224899943
Numpy: 843828735
Torch: 843828736
이제 어휘를 계산 했으므로 미리 훈련 된 워드 벡터를 사용하여 다음과 같은 등장을 설정할 수 있습니다.
import torch
from torchnlp . encoders . text import WhitespaceEncoder
from torchnlp . word_to_vector import GloVe
encoder = WhitespaceEncoder ([ "now this ain't funny" , "so don't you dare laugh" ])
vocab_set = set ( encoder . vocab )
pretrained_embedding = GloVe ( name = '6B' , dim = 100 , is_include = lambda w : w in vocab_set )
embedding_weights = torch . Tensor ( encoder . vocab_size , pretrained_embedding . dim )
for i , token in enumerate ( encoder . vocab ):
embedding_weights [ i ] = pretrained_embedding [ token ] 예를 들어, 신경망 패키지에서 최첨단 LockedDropout 적용하십시오.
import torch
from torchnlp . nn import LockedDropout
input_ = torch . randn ( 6 , 3 , 10 )
dropout = LockedDropout ( 0.5 )
# Apply a LockedDropout to `input_`
dropout ( input_ ) # RETURNS: torch.FloatTensor (6x3x10)BLEU 점수와 같은 일반적인 NLP 메트릭을 계산합니다.
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 더 긴 예제를 보면 examples/ 에서 도움이 될 수 있습니다.
더 많은 도움이 필요하십니까? Gitter Chat을 통해 귀하의 질문에 답변하게되어 기쁩니다.
Pytorch에서 NLP 용 기본 툴킷이 부족하여 Pytorch-NLP를 출시했습니다. 우리는 다른 조직이 프로젝트의 혜택을받을 수 있기를 바랍니다. 우리는 지역 사회의 기여에 감사합니다.
개발 프로세스에 대해 알아 보려면 기고 가이드를 읽으십시오, 버그 수정 및 개선 사항을 제안하는 방법, Pytorch-NLP 변경 및 테스트 방법.
Torchtext 및 Pytorch-NLP는 아키텍처 및 기능 세트가 다릅니다. 그렇지 않으면 비슷합니다. Torchtext 및 Pytorch-NLP는 미리 훈련 된 워드 벡터, 데이터 세트, 반복자 및 텍스트 인코더를 제공합니다. Pytorch-NLP는 또한 신경망 모듈과 메트릭을 제공합니다. 아키텍처 관점에서, Torchtext는 외부 커플 링으로 방향을 지정하는 반면 Pytorch-NLP는 낮은 커플 링으로 방향을 지정합니다.
Allennlp는 연구 플랫폼으로 설계되었습니다. Pytorch-NLP는 경량 툴킷으로 설계되었습니다.
Pytorch-NLP가 학술 간행물에 유용한 경우 다음 Bibtex를 사용하여 인용하십시오.
@misc{pytorch-nlp,
author = {Petrochuk, Michael},
title = {PyTorch-NLP: Rapid Prototyping with PyTorch Natural Language Processing (NLP) Tools},
year = {2018},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {url{https://github.com/PetrochukM/PyTorch-NLP}},
}