Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
Xipeng Qiu 47ddb24d1d | 6 years ago | |
---|---|---|
.github | 6 years ago | |
docs | 6 years ago | |
fastNLP | 6 years ago | |
reproduction | 6 years ago | |
test | 6 years ago | |
.travis.yml | 6 years ago | |
LICENSE | 6 years ago | |
README.md | 6 years ago | |
fastnlp-architecture.jpg | 6 years ago | |
requirements.txt | 6 years ago | |
setup.py | 6 years ago |
fastNLP is a modular Natural Language Processing system based on PyTorch, for fast development of NLP tools. It divides the NLP model based on deep learning into different modules. These modules fall into 4 categories: encoder, interaction, aggregation and decoder, while each category contains different implemented modules. Encoder modules encode the input into some abstract representation, interaction modules make the information in the representation interact with each other, aggregation modules aggregate and reduce information, and decoder modules decode the representation into the output. Most current NLP models could be built on these modules, which vastly simplifies the process of developing NLP models. The architecture of fastNLP is as the figure below:
A typical fastNLP routine is composed of four phases: loading dataset, pre-processing data, constructing model and training model.
from fastNLP.models.base_model import BaseModel
from fastNLP.modules import encoder
from fastNLP.modules import aggregation
from fastNLP.modules import decoder
from fastNLP.loader.dataset_loader import ClassDatasetLoader
from fastNLP.loader.preprocess import ClassPreprocess
from fastNLP.core.trainer import ClassificationTrainer
from fastNLP.core.inference import ClassificationInfer
class ClassificationModel(BaseModel):
"""
Simple text classification model based on CNN.
"""
def __init__(self, num_classes, vocab_size):
super(ClassificationModel, self).__init__()
self.emb = encoder.Embedding(nums=vocab_size, dims=300)
self.enc = encoder.Conv(
in_channels=300, out_channels=100, kernel_size=3)
self.agg = aggregation.MaxPool()
self.dec = decoder.MLP(100, num_classes=num_classes)
def forward(self, x):
x = self.emb(x) # [N,L] -> [N,L,C]
x = self.enc(x) # [N,L,C_in] -> [N,L,C_out]
x = self.agg(x) # [N,L,C] -> [N,C]
x = self.dec(x) # [N,C] -> [N, N_class]
return x
data_dir = 'data' # directory to save data and model
train_path = 'test/data_for_tests/text_classify.txt' # training set file
# load dataset
ds_loader = ClassDatasetLoader("train", train_path)
data = ds_loader.load()
# pre-process dataset
pre = ClassPreprocess(data_dir)
vocab_size, n_classes = pre.process(data, "data_train.pkl")
# construct model
model_args = {
'num_classes': n_classes,
'vocab_size': vocab_size
}
model = ClassificationModel(num_classes=n_classes, vocab_size=vocab_size)
# train model
train_args = {
"epochs": 20,
"batch_size": 50,
"pickle_path": data_dir,
"validate": False,
"save_best_dev": False,
"model_saved_path": None,
"use_cuda": True,
"learn_rate": 1e-3,
"momentum": 0.9}
trainer = ClassificationTrainer(train_args)
trainer.train(model)
# predict using model
seqs = [x[0] for x in data]
infer = ClassificationInfer(data_dir)
labels_pred = infer.predict(model, seqs)
If you just want to use fastNLP, use:
git clone https://github.com/fastnlp/fastNLP
cd fastNLP
Visit the [PyTorch official website] for installation instructions based on your system. In general, you could use:
# using conda
conda install pytorch torchvision -c pytorch
# or using pip
pip3 install torch torchvision
FastNLP
├── docs
│ └── quick_tutorial.md
├── fastNLP
│ ├── action
│ │ ├── action.py
│ │ ├── inference.py
│ │ ├── __init__.py
│ │ ├── metrics.py
│ │ ├── optimizer.py
│ │ ├── README.md
│ │ ├── tester.py
│ │ └── trainer.py
│ ├── fastnlp.py
│ ├── __init__.py
│ ├── loader
│ │ ├── base_loader.py
│ │ ├── config_loader.py
│ │ ├── dataset_loader.py
│ │ ├── embed_loader.py
│ │ ├── __init__.py
│ │ ├── model_loader.py
│ │ └── preprocess.py
│ ├── models
│ │ ├── base_model.py
│ │ ├── char_language_model.py
│ │ ├── cnn_text_classification.py
│ │ ├── __init__.py
│ │ └── sequence_modeling.py
│ ├── modules
│ │ ├── aggregation
│ │ │ ├── attention.py
│ │ │ ├── avg_pool.py
│ │ │ ├── __init__.py
│ │ │ ├── kmax_pool.py
│ │ │ ├── max_pool.py
│ │ │ └── self_attention.py
│ │ ├── decoder
│ │ │ ├── CRF.py
│ │ │ └── __init__.py
│ │ ├── encoder
│ │ │ ├── char_embedding.py
│ │ │ ├── conv_maxpool.py
│ │ │ ├── conv.py
│ │ │ ├── embedding.py
│ │ │ ├── __init__.py
│ │ │ ├── linear.py
│ │ │ ├── lstm.py
│ │ │ ├── masked_rnn.py
│ │ │ └── variational_rnn.py
│ │ ├── __init__.py
│ │ ├── interaction
│ │ │ └── __init__.py
│ │ ├── other_modules.py
│ │ └── utils.py
│ └── saver
│ ├── base_saver.py
│ ├── __init__.py
│ ├── logger.py
│ └── model_saver.py
├── LICENSE
├── README.md
├── reproduction
│ ├── Char-aware_NLM
│ │
│ ├── CNN-sentence_classification
│ │
│ ├── HAN-document_classification
│ │
│ └── LSTM+self_attention_sentiment_analysis
|
├── requirements.txt
├── setup.py
└── test
├── data_for_tests
│ ├── charlm.txt
│ ├── config
│ ├── cws_test
│ ├── cws_train
│ ├── people_infer.txt
│ └── people.txt
├── test_charlm.py
├── test_cws.py
├── test_fastNLP.py
├── test_loader.py
├── test_seq_labeling.py
├── test_tester.py
└── test_trainer.py
一款轻量级的自然语言处理(NLP)工具包,目标是减少用户项目中的工程型代码,例如数据处理循环、训练循环、多卡运行等
Python Jupyter Notebook Text CSV Markdown