飞桨 PaddleNLP 中 IndoBERT Base(phase1-uncased)印尼语预训练模型的下载与使用指南
飞桨 PaddleNLP 中 IndoBERT Base(phase1-uncased)印尼语预训练模型的下载与使用指南
本篇指南围绕飞桨模型库中 indobenchmark/indobert-base-p1 模型的下载与接入展开,完整说明该印尼语预训练模型的背景、权重文件构成、paddlenlp CLI 一键下载命令,以及基于 AutoModel.from_pretrained 的代码级加载与推理方式。读完本文,你可以直接在本仓库环境下获取并运行该模型,并了解其同系列模型的选型参考。
模型背景:面向印尼语的 BERT 预训练模型
indobenchmark/indobert-base-p1(IndoBERT Base Model,phase1 - uncased)是由 indobenchmark 团队发布的印尼语(Indonesian)预训练语言模型。从模型介绍(见 introduction_cn.ipynb)可知:
- 模型架构基于 BERT,属于 Base 规模的 Transformer 双向编码器;
- 预训练目标同时采用掩码语言建模(Masked Language Modeling,MLM)与下一句预测(Next Sentence Prediction,NSP)两类自监督任务;
- 训练语料为 Indo4B 语料(约 23.43 GB 的印尼语文本),模型参数量为 124.5M;
- 该模型的学术出处为论文《IndoNLU: Benchmark and Resources for Evaluating Indonesian Natural Language Understanding》(arXiv:2009.05387),该论文同时构建了印尼语自然语言理解基准数据集 IndoNLU。
在 info.yaml 中,模型元信息被整理为:
| 字段 | 取值 |
|---|---|
| 模型名称 | indobenchmark/indobert-base-p1 |
| 模型描述 | IndoBERT Base Model(phase1 - uncased) |
| 语言 | Indonesian(印尼语) |
| 许可协议 | MIT |
| 下游数据集 | indonlu |
| 任务类型 | 自然语言处理 / 特征抽取(Feature Extraction) |
权重文件清单与直接下载
在仓库的 download_cn.md 中,该模型以表格形式给出了完整的文件清单。模型整体大小为 474.73MB,由以下 4 个文件组成:
| 文件 | 作用 |
|---|---|
model_config.json |
模型结构配置文件(层数、隐藏维度、注意力头数等超参数) |
model_state.pdparams |
飞桨格式的模型权重参数文件,即实际可加载的预训练权重 |
tokenizer_config.json |
分词器配置,记录分词器的类型与相关参数 |
vocab.txt |
词表文件,用于将印尼语文本切分为 token 并映射为 id |
这四个文件是 PaddleNLP 加载模型的最小完整集合:配置文件决定网络结构,.pdparams 提供权重,分词器配置与词表负责文本侧的处理。下载方式有两种——一是从上述清单中逐个获取各文件,二是使用下文介绍的 paddlenlp CLI 工具一次性拉取全部文件。
使用 paddlenlp CLI 一键下载全部权重
paddlenlp 内置了 download 子命令,可按照模型名(indobenchmark/indobert-base-p1)自动定位并下载上述全部文件,无需手动拼接文件路径。
第一步:安装 paddlenlp
pip install --upgrade paddlenlp
使用 --upgrade 可确保安装的是包含最新下载逻辑与模型索引的版本。
第二步:执行下载命令
paddlenlp download --cache-dir ./pretrained_models indobenchmark/indobert-base-p1
参数说明:
--cache-dir ./pretrained_models:指定权重保存目录,命令会在当前路径下创建pretrained_models目录(如目录已存在则直接使用),下载的model_config.json、model_state.pdparams、tokenizer_config.json、vocab.txt都会落入该目录;indobenchmark/indobert-base-p1:模型名,必须与仓库中的模型标识完全一致,大小写敏感。
下载完成后,即可在本地目录中看到完整的模型文件,供后续加载使用。若下载过程中遇到任何问题,可以在 PaddleNLP 项目中提交 Issue 反馈。
在 PaddleNLP 中加载与使用模型
结合 introduction_cn.ipynb 中的示例,通过 AutoModel 即可一键加载该预训练模型:
import paddle
from paddlenlp.transformers import AutoModel
model = AutoModel.from_pretrained("indobenchmark/indobert-base-p1")
# 构造一个随机的 token id 输入并前向计算
input_ids = paddle.randint(100, 200, shape=[1, 20])
print(model(input_ids))
运行逻辑说明:
AutoModel.from_pretrained("indobenchmark/indobert-base-p1")会按模型名查找对应的配置与权重;若本地--cache-dir中已有缓存则直接读取,否则自动下载;paddle.randint(100, 200, shape=[1, 20])生成一个形状为[1, 20]的随机 id 序列,用于模拟一条长度为 20 的 token 序列输入;- 前向输出即该序列经过 12 层 BERT 编码后的隐藏表示(
[1, 20, hidden_size]),可以直接作为下游任务的语义特征。
在实际场景中,需要先将真实文本通过分词器转换为 input_ids 和 token_type_ids,再送入模型。分词器同样可以通过 PaddleNLP 的 AutoTokenizer 接口按模型名加载:
from paddlenlp.transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("indobenchmark/indobert-base-p1")
encoded = tokenizer("Selamat pagi, apa kabar?") # 印尼语示例:"早上好,你好吗?"
由于该模型为 uncased 版本,分词器在预处理时会统一将文本转为小写,适用于对大小写不敏感的下游任务。
同系列模型一览与选型参考
introduction_cn.ipynb 同时列出了 indobenchmark 发布的同系列预训练模型,均基于 Indo4B(23.43 GB 文本)训练,可作为不同算力与精度需求下的选型参考:
| 模型 | 参数量 | 架构 | 训练数据 |
|---|---|---|---|
indobenchmark/indobert-base-p1 |
124.5M | Base | Indo4B(23.43 GB) |
indobenchmark/indobert-base-p2 |
124.5M | Base | Indo4B(23.43 GB) |
indobenchmark/indobert-large-p1 |
335.2M | Large | Indo4B(23.43 GB) |
indobenchmark/indobert-large-p2 |
335.2M | Large | Indo4B(23.43 GB) |
indobenchmark/indobert-lite-base-p1 |
11.7M | Base | Indo4B(23.43 GB) |
indobenchmark/indobert-lite-base-p2 |
11.7M | Base | Indo4B(23.43 GB) |
indobenchmark/indobert-lite-large-p1 |
17.7M | Large | Indo4B(23.43 GB) |
indobenchmark/indobert-lite-large-p2 |
17.7M | Large | Indo4B(23.43 GB) |
其中 p1 与 p2 代表不同的预训练阶段检查点;Large 版本容量更大、语义建模能力更强,lite 版本则显著压缩参数量,适合资源受限的移动端或低延迟场景。开发者可按任务复杂度与硬件条件选择对应模型名替换 from_pretrained 的参数即可复用上述加载流程。
引用方式
若在论文或技术报告中使用该模型,可引用其原始论文:
@inproceedings{wilie2020indonlu,
title={IndoNLU: Benchmark and Resources for Evaluating Indonesian Natural Language Understanding},
author={Bryan Wilie and Karissa Vincentio and Genta Indra Winata and Samuel Cahyawijaya and X. Li and Zhi Yuan Lim and S. Soleman and R. Mahendra and Pascale Fung and Syafri Bahar and A. Purwarianti},
booktitle={Proceedings of the 1st Conference of the Asia-Pacific Chapter of the Association for Computational Linguistics and the 10th International Joint Conference on Natural Language Processing},
year={2020}
}
更多仓库资源
- download_cn.md:本文对应的中文下载文档(文件清单与 CLI 命令);
- download_en.md:英文版下载文档;
- info.yaml:模型卡片元信息(语言、许可、任务类型、论文等);
- introduction_cn.ipynb:模型介绍、加载示例与同系列模型对照表(中文版);
- introduction_en.ipynb:模型介绍与加载示例(英文版)。
至此,你已经掌握了 indobenchmark/indobert-base-p1 从下载、安装到代码加载的完整链路:先通过 pip install --upgrade paddlenlp 安装依赖,再用 paddlenlp download --cache-dir ./pretrained_models indobenchmark/indobert-base-p1 拉取 474.73MB 的四件套权重文件,最后以 AutoModel.from_pretrained 或 AutoTokenizer.from_pretrained 直接接入下游印尼语 NLP 任务。