【NLP】序列到序列(seq2seq)建模工具fairseq使用详解

发布于:2024-12-18 ⋅ 阅读:(96) ⋅ 点赞:(0)

一、fairseq简介

fairseq 是 Facebook AI Research Sequence-to-Sequence Toolkit 的缩写,是一款开源的神经机器翻译框架。它基于PyTorch开发,用于 训练和评估各种序列到序列(seq2seq)模型 ,广泛应用于自然语言处理(NLP)任务,如机器翻译、文本生成、语音识别等。它支持多种模型架构,包括但不限于 Transformer、LSTM 和 Convolutional models,并且具有高效的多GPU训练和分布式训练功能。

二、安装方式

2.1 pip安装

pip install fairseq

2.2 源码安装

pip方式的安装可能对应需要最新版的同学会有弊端(原因在于pip库相对于 Github源码库会有版本更替的延迟)。

因此,也可以直接基于git源码安装,命令如下:

git clone https://github.com/pytorch/fairseq
cd fairseq
pip install --editable ./

注意:fairseq 需要匹配对应的 pytorch 及 cudatoolkit 版本!

三、fairseq命令工具

3.1 fairseq-preprocess

参考:https://fairseq.readthedocs.io/en/latest/command_line_tools.html#fairseq-preprocess

(一)预处理参数
在这里插入图片描述

3.2 fairseq-train

参考:https://fairseq.readthedocs.io/en/latest/command_line_tools.html#fairseq-train

(一) 数据集加载
在这里插入图片描述

(二) 模型选择

--arch, -a
# fairseq中集成了大量的模型类型,只需按照自己的任务需求选择即可,也可以自己进行训练。

(三) 模型优化

在这里插入图片描述

(四) 使用示例

CUDA_VISIBLE_DEVICES=0 fairseq-train data-bin/iwslt14.tokenized.de-en \
    --optimizer nag \ 
    --lr 0.25 \ 
    --clip-norm 0.1 \ 
    --dropout 0.2 \ 
    --max-tokens 4000 \
    --arch fconv_iwslt_de_en \ 
    --save-dir checkpoints/fconv

3.3 fairseq-generate

参考: https://fairseq.readthedocs.io/en/latest/command_line_tools.html#fairseq-generate

3.4 fairseq-interactivate

参考: https://fairseq.readthedocs.io/en/latest/command_line_tools.html#fairseq-interactive

3.5 fairseq-score

参考: https://fairseq.readthedocs.io/en/latest/command_line_tools.html#fairseq-score

3.6 fairseq-eval-lm

参考: https://fairseq.readthedocs.io/en/latest/command_line_tools.html#fairseq-eval-lm

4. 常见报错

报错1

  • 报错信息:
mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start)
ValueError: cannot mmap an empty file
Exception ignored in: <bound method MMapIndexedDataset.__del__ of <fairseq.data.indexed_dataset.MMapIndexedDataset object at 0x7fb0772f1a90>>
git clone https://github.com/NVIDIA/apex
cd apex
# if pip >= 23.1 
pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" ./
# otherwise
pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --global-option="--cpp_ext" --global-option="--cuda_ext" ./

apex的安装可参考:https://blog.csdn.net/u012856866/article/details/144526882

参考资料


网站公告

今日签到

点亮在社区的每一天
去签到