BERT模型

BERT是一种基于transformer的深度双向编码器,由Google AI于2018年提出,用于语言理解的预训练模型。与传统模型不同,BERT在每一层中同时基于左右上下文进行预测,仅需添加一层即可应用于多种NLP任务,并在多个任务上取得卓越成果。
部署运行你感兴趣的模型镜像

BERT是基于Vaswani et al(2017)的论文"Attention is all you need"中提出的transformer模型构建的多层双向transformoer encoder。就是说BERT只是一个NLP方向的编码器。他能对单独句子进行表征,也可以对问答等进行表征。具体可以看论文
论文题目:BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
论文发表时间:2018年10月11日
机构:Google AI
在这里插入图片描述
文献解决的问题:提出一个语言表征的模型,叫BERT(Bidirectional Encoder Representations from Transformers)
特点:与传统的语言表征模型不同,BERT使用的是深度双向表征,即在每一层中同时基于左边和右边的context来做预测。
优势:预训练的BERT模型只需要在上面增加额外的一层,就能广泛地于多种NLP任务中进行fine-tune.
结果:在11项NLP任务上都取得了state of art的成绩。

BERT是个开源的项目,能在github上下载源代码。值得一提到是,由于预训练消耗大量时间和资源,google还很贴心的给出了预训练的权重(模型),让我们进行微调即可。比较可惜的是中文预训练模型就一个。中文预训练模型如下:
模型解压,模型文件包括:
bert_config.json
bert_model.ckpt.data-00000-of-00001
bert_model.ckpt.index
bert_model.ckpt.meta
vocab.txt(除了中文字符,还有很多看不懂的特殊符号等)
1、制作特定格式data

class InputExample(object):
  """A single training/test example for simple sequence classification."""
 
  def __init__(self, guid, text_a, text_b=None, label=None):
    """Constructs a InputExample.
    Args:
      guid: Unique id for the example.
      text_a: string. The untokenized text of the first sequence. For single
        sequence tasks, only this sequence must be specified.
      text_b: (Optional) string. The untokenized text of the second sequence.
        Only must be specified for sequence pair tasks.
      label: (Optional) string. The label of the example. This should be
        specified for train and dev examples, but not for test examples.
    """
    self.guid = guid
    self.text_a = text_a
    self.text_b = text_b
    self.label = label

可以发现它要求的输入分别是guid, text_a, text_b, label,其中text_b和label为可选参数。我们要做的是单个句子的分类任务,那么就不需要输入text_b。对了,我们是中文的模型,只能输入512个字符(具有长度限制)。
2、重载DataProcessor类

class MyProcessor(DataProcessor):
  """Processor for my task-news classification """
  def __init__(self):
    self.labels = ['AD', 'CTRL']
 
  def get_train_examples(self, data_dir):
    return self._create_examples(
      self._read_tsv(os.path.join(data_dir, 'traintext.csv')), 'train')
 
  def get_dev_examples(self, data_dir):
    return self._create_examples(
      self._read_tsv(os.path.join(data_dir, 'vaildtext.csv')), 'val')
 
  def get_test_examples(self, data_dir):
    return self._create_examples(
      self._read_tsv(os.path.join(data_dir, 'testtext.csv')), 'test')
 
  def get_labels(self):
    return self.labels
 
  def _create_examples(self, lines, set_type):
    """create examples for the training and val sets"""
    examples = []
    for (i, line) in enumerate(lines):
      guid = '%s-%s' %(set_type, i)
      #print("line[0]:",line[0])
      #print("line[1]:",line[1])
      text_a = tokenization.convert_to_unicode(line[1])
      label = tokenization.convert_to_unicode(line[0])
      examples.append(InputExample(guid=guid, text_a=text_a, label=label))
    return examples

建立好了需要在main函数中的processors中增加自己的模型

  processors = {
      "cola": ColaProcessor,
      "mnli": MnliProcessor,
      "mrpc": MrpcProcessor,
      "xnli": XnliProcessor,
	  "my": MyProcessor,
  }

3、修改loss输出

  if FLAGS.do_train:
    train_file = os.path.join(FLAGS.output_dir, "train.tf_record")
    file_based_convert_examples_to_features(
        train_examples, label_list, FLAGS.max_seq_length, tokenizer, train_file)
    tf.logging.info("***** Running training *****")
    tf.logging.info("  Num examples = %d", len(train_examples))
    tf.logging.info("  Batch size = %d", FLAGS.train_batch_size)
    tf.logging.info("  Num steps = %d", num_train_steps)
    train_input_fn = file_based_input_fn_builder(
        input_file=train_file,
        seq_length=FLAGS.max_seq_length,
        is_training=True,
        drop_remainder=True)
    tensors_to_log={'train loss':'loss/Mean:0'}  #修改
    logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log,every_n_iter=20)#修改
    estimator.train(input_fn=train_input_fn, hooks=[logging_hook],max_steps=num_train_steps) #修改

训练

export BERT_BASE_DIR=./chinese_L-12_H-768_A-12#这里是存放中文模型的路径
export DATA_DIR=.  #这里是存放数据的路径
 
python3 run_classifier.py \
--task_name=my \     #这里是processor的名字
--do_train=true \    #是否训练
--do_eval=true  \    #是否验证
--do_predict=false \  #是否预测(对应test)
--data_dir=$DATA_DIR \ 
--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=512 \#最大文本程度,最大512
--train_batch_size=4 \
--learning_rate=2e-5 \
--num_train_epochs=15 \
--output_dir=./mymodel #输出目录

测试

export BERT_BASE_DIR=./chinese_L-12_H-768_A-12
export DATA_DIR=./mymodel
# TRAINED_CLASSIFIER为刚刚训练的输出目录,无需在进一步指定模型模型名称,否则分类结果会不对
export ./mymodel
 
python3 run_classifier.py \
  --task_name=chi \
  --do_predict=true \
  --data_dir=$DATA_DIR \
  --vocab_file=$BERT_BASE_DIR/vocab.txt \
  --bert_config_file=$BERT_BASE_DIR/bert_config.json \
  --init_checkpoint=$TRAINED_CLASSIFIER \
  --max_seq_length=512 \
  --output_dir=./mymodel

https://www.cnblogs.com/rucwxb/p/10277217.html
https://blog.youkuaiyun.com/Kaiyuan_sjtu/article/details/88709580

您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

### BERT模型概述 BERT(Bidirectional Encoder Representations from Transformers)是一种基于Transformer架构的语言表示模型,旨在解决自然语言处理中的多种任务。该模型通过引入双向编码器来捕捉输入序列的上下文信息[^1]。 #### 模型结构详解 BERT的核心在于其独特的网络架构设计: - **Embedding 层**:负责将输入文本转换成向量形式。这一步骤不仅考虑词本身的含义,还结合位置信息以及句子间的关系。 - **Encoder 层**:由多层自注意力机制构成,每层都包含了前馈神经网络组件。此部分实现了对整个句子甚至文档级别的语义建模,从而赋予了模型强大的泛化能力。 - **Pooler 层**:用于特定下游任务时提取固定长度特征向量,比如分类问题中获取整句表征[^2]。 这种深度双向架构允许同一个预训练好的BERT模型可以被广泛应用于不同的NLP场景下,如情感分析、问答系统构建等[^3]。 ### 实现简单BERT分类模型案例 下面展示如何利用Python编程环境配合TensorFlow框架快速搭建并测试一个基础版的BERT二元分类器实例: ```python import tensorflow as tf from transformers import BertTokenizer, TFBertForSequenceClassification # 加载预训练权重与分词工具 tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') model = TFBertForSequenceClassification.from_pretrained('bert-base-uncased') def predict(texts): inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="tf") outputs = model(**inputs) predictions = tf.nn.softmax(outputs.logits, axis=-1).numpy() return predictions.argmax(axis=1) sample_texts = ["I love programming.", "This movie is terrible."] print(predict(sample_texts)) ``` 上述代码片段展示了怎样加载预先训练完成的基础版本BERT模型,并定义了一个预测函数`predict()`来进行简单的文本分类操作。这里选取了一些样例数据作为输入进行了演示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值