官方代码微调bert

train

export BERT_BASE_DIR=../GLUE/BERT_BASE_DIR/uncased_L-12_H-768_A-12
export GLUE_DIR=../GLUE/glue_data

python run_classifier.py \
  --task_name=MRPC \
  --do_train=true \
  --do_eval=true \
  --data_dir=$GLUE_DIR/MRPC \
  --vocab_file=$BERT_BASE_DIR/vocab.txt \
  --bert_config_file=$BERT_BASE_DIR/bert_config.json \
  --init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt \
  --max_seq_length=128 \
  --train_batch_size=32 \
  --learning_rate=2e-5 \
  --num_train_epochs=3.0 \
  --output_dir=../GLUE/output

debugger

export BERT_BASE_DIR=./GLUE/BERT_BASE_DIR/uncased_L-12_H-768_A-12
export GLUE_DIR=./GLUE/glue_data

python run_classifier.py \
  --task_name=MRPC \
  --do_train=true \
  --do_eval=true \
  --data_dir=$GLUE_DIR/MRPC \
  --vocab_file=$BERT_BASE_DIR/vocab.txt \
  --bert_config_file=$BERT_BASE_DIR/bert_config.json \
  --init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt \
  --max_seq_length=128 \
  --train_batch_size=32 \
  --learning_rate=2e-5 \
  --num_train_epochs=3.0 \
  --output_dir=./GLUE/output

AttributeError: module ‘tensorflow_core._api.v2.train‘ has no attribute

https://juejin.cn/s/attributeerror%20module%20’tensorflow._api.v2.train’%20has%20no%20attribute%20’sessionrunhook’
更改优化器的调用方式
https://blog.csdn.net/weixin_41845265/article/details/108572738

AttributeError: module ‘tensorflow’ has no attribute ‘flags’

https://blog.csdn.net/qq_53016081/article/details/119831833

加了keras =tf.keras.optimizers.Optimizer

vscode需要切换编译器python环境(这样就成功了)

debugger的终端也需要重新配置环境变量,其是一个临时的变量

为什么debugger的时候,每次都会

(myenv) ➜  bert  cd /Users/wangfeng/Downloads/bert ; /usr/bin/env /opt/miniconda3/envs/myenv/bin/python /Users/wangfeng/.vscode/extensions/ms-python.debugpy-2024.7.11371017/bun
dled/libs/debugpy/adapter/../../debugpy/launcher 62860 -- /Users/wangfeng/Downloads/bert/bert-master/run_classifier.py --task_name=MRPC \   --do_train=true \   --do_eval=true \
   --data_dir=$GLUE_DIR/MRPC \   --vocab_file=$BERT_BASE_DIR/vocab.txt \   --bert_config_file=$BERT_BASE_DIR/bert_config.json \   --init_checkpoint=$BERT_BASE_DIR/bert_model.ck
pt \   --max_seq_length=128 \   --train_batch_size=32 \   --learning_rate=2e-5 \   --num_train_epochs=3.0 \   --output_dir=../GLUE/output 
WARNING:tensorflow:From /Users/wangfeng/Downloads/bert/bert-master/optimization.py:87: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

launch文件

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        

        {
            "name": "Python Debugger: Current File with Arguments",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": "${command:pickArgs}"
        }
    ]
}

bert模型

bert_model.meta:

Meta信息:这是TensorFlow的元数据文件,包含了计算图(graph)的结构和节点的元信息。具体来说,它定义了模型的计算节点及其相互之间的连接关系,但不包括实际的权重值。这是为了在恢复模型时知道如何重建整个计算图。

bert_model.index:

Index信息:这是TensorFlow的索引文件,用于指示模型参数文件(即.ckpt文件)中每个变量所在的位置。它相当于一个目录,帮助模型在加载时能快速定位和读取参数值。

bert.config

{
  "attention_probs_dropout_prob": 0.1,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 768,
  "initializer_range": 0.02,
  "intermediate_size": 3072,
  "max_position_embeddings": 512,
  "num_attention_heads": 12,
  "num_hidden_layers": 12,
  "type_vocab_size": 2,
  "vocab_size": 30522
}

损失函数

https://blog.csdn.net/yhsunhfut/article/details/126730359

BERT(Bidirectional Encoder Representations from Transformers)是一种预训练语言模型,可以用于各种自然语言处理任务的微调。下面是BERT预训练和微调的简要介绍: BERT预训练: BERT的预训练是指在大规模文本上训练出一个通用的语言模型,然后将这个模型微调到各种具体的NLP任务上。预训练BERT包含两个阶段:Masked Language Model(MLM)和Next Sentence Prediction(NSP)。在MLM阶段,部分输入单词被随机替换成[Mask]标记,模型需要预测这些[Mask]标记对应的原始单词;在NSP阶段,模型需要判断两个句子是否是连续的。BERT的预训练代码可以使用谷歌官方实现的TensorFlow版本或者Hugging Face开发的PyTorch版本。 BERT微调BERT微调是指在任务特定的数据集上微调预训练好的BERT模型。微调步骤包括数据处理、模型配置、模型微调和模型评估。在数据处理阶段,需要将任务特定数据集转换成适合BERT输入的格式;在模型配置阶段,需要根据任务需求选择合适的模型结构、超参数和优化器;在模型微调阶段,需要用微调数据集对模型进行训练;在模型评估阶段,需要使用测试集对微调后的模型进行评估。BERT微调代码可以使用各种框架实现,如PyTorch、TensorFlow等。 如果您想学习更多关于BERT预训练和微调的内容,我建议您查看谷歌官方论文和Hugging Face官方文档。此外,您也可以在GitHub上找到各种不同的BERT实现和教程。如果您需要具体的代码示例,可以告诉我您所使用的框架和任务类型,我可以为您提供相关参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值