Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
fastNLP: A Modularized and Extensible NLP Framework. Currently still in incubation.
| Date | Stars |
|---|---|
| 2026-07-24 | 3145 |
| 2026-07-25 | 3145 |
| 2026-07-28 | 3145 |
| 2026-07-30 | 3145 |
| 2026-08-06 | 3145 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# fastNLP
[//]: # ([](https://travis-ci.org/fastnlp/fastNLP))
[//]: # ([](https://codecov.io/gh/fastnlp/fastNLP))
[//]: # ([](https://pypi.org/project/fastNLP))
[//]: # ()
[//]: # ([](http://fastnlp.readthedocs.io/?badge=latest))
fastNLP是一款轻量级的自然语言处理(NLP)工具包,目标是减少用户项目中的工程型代码,例如数据处理循环、训练循环、多卡运行等。
fastNLP具有如下的特性:
- 便捷。在数据处理中可以通过apply函数避免循环、使用多进程提速等;在训练循环阶段可以很方便定制操作。
- 高效。无需改动代码,实现fp16切换、多卡、ZeRO优化等。
- 兼容。fastNLP支持多种深度学习框架作为后端。
> :warning: **为了实现对不同深度学习架构的兼容,fastNLP 1.0.0之后的版本重新设计了架构,因此与过去的fastNLP版本不完全兼容,
> 基于更早的fastNLP代码需要做一定的调整**:
## fastNLP文档
[中文文档](http://www.fastnlp.top/docs/fastNLP/master/index.html)
## 安装指南
fastNLP可以通过以下的命令进行安装
```shell
pip install fastNLP>=1.0.0alpha
```
如果需要安装更早版本的fastNLP请指定版本号,例如
```shell
pip install fastNLP==0.7.1
```
另外,请根据使用的深度学习框架,安装相应的深度学习框架。
<details>
<summary>Pytorch</summary>
下面是使用pytorch来进行文本分类的例子。需要安装torch>=1.6.0。
```python
from fastNLP.io import ChnSentiCorpLoader
from functools import partial
from fastNLP import cache_results
from fastNLP.transformers.torch import BertTokenizer
# 使用cache_results装饰器装饰函数,将prepare_data的返回结果缓存到caches/cache.pkl,再次运行时,如果
# 该文件还存在,将自动读取缓存文件,而不再次运行预处理代码。
@cache_results('caches/cache.pkl')
def prepare_data():
# 会自动下载数据,并且可以通过文档看到返回的 dataset 应该是包含"raw_words"和"target"两个field的
data_bundle = ChnSentiCorpLoader().load()
# 使用tokenizer对数据进行tokenize
tokenizer = BertTokenizer.from_pretrained('hfl/chinese-bert-wwm')
tokenize = partial(tokenizer, max_length=256) # 限制数据的最大长度
data_bundle.apply_field_more(tokenize, field_name='raw_chars', num_proc=4) # 会新增"input_ids", "attention_mask"等field进入dataset中
data_bundle.apply_field(int, field_name='target', new_field_name='labels') # 将int函数应用到每个target上,并且放入新的labels field中
return data_bundle
data_bundle = prepare_data()
print(data_bundle.get_dataset('train')[:4])
# 初始化model, optimizer
from fastNLP.transformers.torch import BertForSequenceClassification
from torch import optim
model = BertForSequenceClassification.from_pretrained('hfl/chinese-bert-wwm')
optimizer = optim.AdamW(model.parameters(), lr=2e-5)
# 准备dataloader
from fastNLP import prepare_dataloader
dls = prepare_dataloader(data_bundle, batch_size=32)
# 准备训练
from fastNLP import Trainer, Accuracy, LoadBestModelCallback, TorchWarmupCallback, Event
callbacks = [
TorchWarmupCallback(warmup=0.1, schedule='linear'), # 训练过程中调整学习率。
LoadBestModelCallback() # 将在训练结束之后,加载性能最优的model
]
# 在训练特定时机加入一些操作, 不同时机能够获取到的参数不一样,可以通过Trainer.on函数的文档查看每个时机的参数
@Trainer.on(Event.on_before_backward())
def print_loss(trainer, outputs):
if trainer.global_forward_batches % 10 == 0: # 每10个batch打印一次loss。
print(outputs.loss.item())
trainer = Trainer(model=model, train_dataloader=dls['train'], optimizers=optimizer,
device=0, evaluate_dataloaders=dls['dev'], metrics={'acc': Accuracy()},
callbacks=callbacks, monitor='acc#acc',n_epochs=5,
# Accuracy的update()函数需要pred,target两个参数,它们实际对应的就是以下的field。
evaluate_input_mapping={'labels': 'target'}, # 在评测时,将dataloader中会输入到模型的labels重新命名为target
evaluate_output_mapping={'logits': 'pred'} # 在评测时,将model输出中的logits重新命名为pred
)
trainer.run()
# 在测试集合上进行评测
from fastNLP import Evaluator
evaluator = Evaluator(model=model, dataloaders=dls['test'], metrics={'acc': Accuracy()},
# Accuracy的update()函数需要pred,target两个参数,它们实际对应的就是以下的field。
output_mapping={'logits': 'pred'},
input_mapping={'labels': 'target'})
evaluator.run()
```
更多内容可以参考如下的链接
##Excerpt of 9,882 characters
Read on GitHubWould you bet a product on this? Bounded 0–100 and slow moving.
matched fp:d59d17dc76458de3, topic:natural-language-processing, topic:text-classification, readme:tokenizer
matched fp:d59d17dc76458de3, topic:deep-learning