使用自己的数据再训练BERT

Though the pre-trained model is good enough, you may still want to tune the pre-trained model offered by Google on your own domain-specific corpus for several additional epochs. That is, give your Bert model a chance to be familiar with your jargons. Then we can expect better performance in the end.
转自英文原文:https://github.com/pren1/A_Pipeline_Of_Pretraining_Bert_On_Google_TPU
准备一个 .txt文件。
这个文件就是你要训练BERT的自己的数据。训练的目的其实就是让BERT能对你数据中的那些黑话(专有名词等)有所熟悉。

做接下来的前提是,已经有了google cloud账户,并如何使用其shell和storage。
激活shell(打开google cloud的shell),执行命令:
ctpu up --name=yourname-tpu --tpu-size=v3-8 --preemptible
(其中的yourname是自己任意定义的名字)加上后面的–preemptible 是2.4刀/小时,不加是8刀/小时,区别是google是否可以随时终止你的运行。

接下来的提示中输入:y
询问设置ssh密码的时候,输入:y , 并设置自己的密码

ctpu status 可以查看状态

接着获得bert的源码:
git clone https://github.com/google-research/bert.git
cd bert

然后执行:
python create_pretraining_data.py
–input_file=gs://sample_bucket_test/sample_text.txt
–output_file=*gs://sample_bucket_test/tmp/tf_examples.tfrecord *
–vocab_file=gs://sample_bucket_test/multi_cased_L-12_H-768_A-12/vocab.txt
–do_lower_case=True
–max_seq_length=128
–max_predictions_per_seq=20
–masked_lm_prob=0.15
–random_seed=12345
–dupe_factor=5
上面的是创建预训练数据,最后,就是训练自己的model了(Now, it’s time to train the model! Run the following code. Notice that the tpu_name is set to the name you gave to the TPU previously):
执行:
python run_pretraining.py
–input_file=gs://sample_bucket_test/tmp/tf_examples.tfrecord
–output_dir=gs://sample_bucket_test/tmp/pretraining_output
–do_train=True
–do_eval=True
–bert_config_file=gs://sample_bucket_test/multi_cased_L-12_H-768_A-12/bert_config.json
–init_checkpoint=gs://sample_bucket_test/multi_cased_L-12_H-768_A-12/bert_model.ckpt
–train_batch_size=32
–max_seq_length=128
–max_predictions_per_seq=20
–num_train_steps=20
–num_warmup_steps=10
–learning_rate=2e-5
–use_tpu=True
–tpu_name=yourname-tpu
上面的斜体替换成自己storage下的目录,跟作者一样,同样建议将vocab_file以及bert_config_file改为官方本身就有的路径,这样比自己传省时间空间,比如:gs://cloud-tpu-checkpoints/bert/uncased_L-12_H-768_A-12/vocab.txt (是否用uncase或者 multi_case就根据自己的需求来决定了)

训练BERT模型使用自己的数据集,你可以按照以下步骤进行操作: 1. 准备一个包含你要训练数据的文本文件。这个文件应该包含你的领域特定的术语和黑话等内容,以便让BERT模型熟悉这些内容\[1\]。 2. 使用transformers库中的BertForPreTraining API来构建模型。首先,你需要创建一个BertConfig对象,其中vocab_size参数应该设置为你的词汇表大小加1。然后,使用BertForPreTraining类和配置对象来实例化模型\[2\]。 3. 进行训练。你可以使用一个循环来迭代训练数据集。在每个epoch中,遍历数据加载器中的数据,并将数据传递给模型。确保将输入数据转移到适当的设备上,并提供正确的标签。然后,通过调用模型的forward方法来获取模型的输出。计算损失并执行反向传播,最后更新模型的参数\[2\]。 4. 在训练之前,你需要确保你已经拥有Google Cloud账户,并且知道如何使用其shell和storage。激活Google Cloud的shell,并执行命令"ctpu up --name=yourname-tpu --tpu-size=v3-8 --preemptible"来启动TPU实例。其中,yourname是你自己定义的名称。添加"--preemptible"参数可以选择使用更便宜的预留实例,但可能会被Google随时终止\[3\]。 通过按照以上步骤,你可以使用自己的数据集训练BERT模型。这样,模型将能够更好地理解你的领域特定内容,并提供更好的性能。 #### 引用[.reference_title] - *1* *3* [使用自己的数据训练BERT](https://blog.csdn.net/bayou3/article/details/99655171)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v12^insert_chatgpt"}} ] [.reference_item] - *2* [【记录】使用transformers从头开始训练bert](https://blog.csdn.net/Finks_Chen/article/details/119334214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v12^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值