macbert训练过程记录
来源:互联网
时间:2026-07-10 07:21:35
先把demo直接放到服务器上,一个scp命令搞定:
scp -r wangfei@10.30.71.37:/Users/wangfei/Documents/Codex/2026-06-26/jin-t/outputs/macbert_intent_demo /home/ubuntu/zlink/wangf36_temp
文件目录结构就是常规的工程项目,该有的模块文件都在里面,不多说。
接下来切换一下conda环境,别搞混了:
conda activate llama_factory
然后装一下依赖,跑起来之前先把这个搞定:
pip install -r requirements.txt
模型下载,两种方式可选
基座模型怎么弄?有两种方式供选择:
- 提前下载好放服务器上,训练时直接指定本地路径
- 从Hugging Face直接拉取
推荐的做法是提前下载本地,省得网络出幺蛾子。如果服务器上已经有本地MacBERT模型,比如放在:
/home/ubuntu/zlink/base_model/chinese-macbert-base
那就直接这样训练:
python train_intent_macbert.py --model_name /home/ubuntu/zlink/base_model/chinese-macbert-base --data_dir . --output_dir ./macbert-intent-model --epochs 5 --batch_size 16 --learning_rate 2e-5
如果服务器能顺畅访问Hugging Face,也可以直接指定模型名称:
python train_intent_macbert.py --model_name hfl/chinese-macbert-base --data_dir . --output_dir ./macbert-intent-model --epochs 5 --batch_size 16 --learning_rate 2e-5
我采用的是先下载到本地再上传到服务器的方式,稳当。
训练报错,直击问题
跑起来之后,控制台里直接报了个PermissionError,错误信息指向/tmp/ssl_keys.log。怎么回事呢?
(llama_factory) ubuntu@VM-12-16-ubuntu:~/zlink/wangf36_temp/macbert_intent_demo$ python train_intent_macbert.py --model_name /home/ubuntu/zlink/base_model/chinese-macbert-base --data_dir . --output_dir ./macbert-intent-model --epochs 5 --batch_size 16 --learning_rate 2e-5
Traceback (most recent call last):
File "/home/ubuntu/zlink/wangf36_temp/macbert_intent_demo/train_intent_macbert.py", line 6, in
from datasets import load_dataset
...
PermissionError: [Errno 13] Permission denied: '/tmp/ssl_keys.log'
其实是因为SSLKEYLOGFILE这个环境变量作祟,导致Python在创建SSL上下文时试图往/tmp/ssl_keys.log写文件,但权限不够。
解决办法,直接取消
解决方法很简单,把这个环境变量unset掉就行:
(llama_factory) ubuntu@VM-12-16-ubuntu:~/zlink/wangf36_temp/macbert_intent_demo$ unset SSLKEYLOGFILE
python train_intent_macbert.py --model_name /home/ubuntu/zlink/base_model/chinese-macbert-base --data_dir . --output_dir ./macbert-intent-model --epochs 5 --batch_size 16 --learning_rate 2e-5
Generating train split: 120 examples [00:00, 76445.39 examples/s]
Generating validation split: 30 examples [00:00, 41432.04 examples/s]
Generating test split: 18 examples [00:00, 31868.92 examples/s]
Map: 100%|...| 120/120 [00:00<00:00, 16187.97 examples/s]
Map: 100%|...| 30/30 [00:00<00:00, 10606.85 examples/s]
Map: 100%|...| 18/18 [00:00<00:00, 7173.84 examples/s]
Some weights of BertForSequenceClassification were not initialized from the model checkpoint at /home/ubuntu/zlink/base_model/chinese-macbert-base and are newly initialized: ['classifier.bias', 'classifier.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
/home/ubuntu/zlink/wangf36_temp/macbert_intent_demo/train_intent_macbert.py:121: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.
trainer = Trainer(
0%| 0/10 [00:00, ?it/s]
...
{'eval_loss': 1.3524516820907593, 'eval_accuracy': 0.6333333333333333, 'eval_macro_f1': 0.5854730781201369, 'eval_weighted_f1': 0.5854730781201369, 'eval_runtime': 0.0431, 'eval_samples_per_second': 695.542, 'eval_steps_per_second': 23.185, 'epoch': 5.0}
{'train_runtime': 7.9708, 'train_samples_per_second': 75.275, 'train_steps_per_second': 1.255, 'train_loss': 1.5742902755737305, 'epoch': 5.0}
100%|...| 10/10 [00:07<00:00, 1.25it/s]
...
Test metrics: {'eval_loss': 1.4313329458236694, 'eval_accuracy': 0.5555555555555556, 'eval_macro_f1': 0.4659090909090909, 'eval_weighted_f1': 0.46590909090909094, 'eval_runtime': 0.043, 'eval_samples_per_second': 418.208, 'eval_steps_per_second': 23.234, 'epoch': 5.0}
Sa ved model to macbert-intent-model/best
训练成功!
测试一下,结果也不错
训练完成后,随手跑个预测看看效果:
(llama_factory) ubuntu@VM-12-16-ubuntu:~/zlink/wangf36_temp/macbert_intent_demo$ python predict_intent.py --model_dir ./macbert-intent-model/best --text "明天上海会下雨吗"
{"text": "明天上海会下雨吗","intent": "unknown","raw_intent": "query_weather","confidence": 0.4739,"threshold": 0.6,"scores": {"query_weather": 0.4739,"book_ticket": 0.1637,"open_app": 0.087,"search_knowledge": 0.0848,"chat": 0.1355,"unknown": 0.0551}}
这里置信度0.4739,没有达到设置的0.6阈值,所以最终意图被判定为unknown。不过这个问题不大,主要是训练数据太少导致的——实际生产环境里,数据量充足的情况下,这个模型的表现会好很多。换句话说,数据量才是决定模型性能的硬道理,样本多了,置信度自然会上去。