GLUE 评测与 BERT

简述

  • BERT, Bidirectional Encoder Representations from Transformers.
  • pre-train: 在超大语料上做无监督学习,可以得到 token 的通用表达.
  • fine-tune: 在预训练模型最后加一层 task-specific 的 layer, 然后 fine-tune 所有的 parameter, 即可得到令人满意的结果.

一. GLUE 基准测评

GLUE, General Language Understanding Evaluation. 发音为 [ɡluː],类似于英文单词 “glue”(胶水) 的发音.
见参考[1] [2].
它是流行的 NLP 任务测评基准. 有以下几种主要的评估任务:

1.1 单句分类任务

CoLA

The Corpus of Linguistic Acceptability. 判断一个单词序列是否为一个合乎语法的英语句子.

SST-2

Stanford Sentiment Treebank. 对影评之类的句子判断情感倾向, 有 positive / negative 两个类别.

1.2 语义一致判断

MRPC

The Microsoft Research Paraphrase Corpus. 判断句子 pair 是否语义一致.

QQP

Quora Question Pairs . 句子 pair 的二分类. 给出 Quora 问答网站上的两个提问, 判断它们语义是否一致.

1.3 推理任务 Inference

MNLI

Multi-Genre Natural Language Inference. 推理任务, 给定句子 pair, 问第二句与第一句是以下三类关系中的哪一种:

  • entailment. 可推论的蕴涵关系
  • contradiction, 矛盾的
  • neutral, 中立的
句子1:A man is playing a large drum.
句子2:The man is playing an instrument.
label: 可推论的

句子1:A man is playing a large drum.
句子2:The man is dancing.
label: 矛盾的

QNLI

Question Natural Language Inference. 句子pair的二分类. The positive examples are (question, sentence) pairs which do contain the correct answer, and the negative examples are (question, sentence) from the same paragraph which do not contain the answer.

WNLI

阅读理解任务, 系统读取含有代词 (pronoun) 的句子, 然后从选项中选出所指代的具体对象.

二. BERT 模型

在这里插入图片描述

多个transformer的encoder堆叠而成, 没有decoder.

token_emb

embedding_table = tf.get_variable(
     name=word_embedding_name,
     shape=[vocab_size, embedding_size],
     initializer=tf.truncated_normal_initializer(stddev=initializer_range))
# initializer_range = 0.02

position encoding

transformer中的 position encoding 采用的是 sin及cos函数. 而在bert中, 每个位置有自己的可学习参数, 参与训练.
bert.modeling.embedding_postprocessor() 中,

full_position_embeddings = tf.get_variable(
          name=position_embedding_name,
          shape=[max_position_embeddings, width],
          initializer=create_initializer(initializer_range))

pre-train task 与 loss

预训练包括以下两个任务.

masked LM task

输入时以 0.15 的概率对sequence中的token作[mask]替换, 然后预测这些 原本的 token.

# 与整个 vocab_emb_mat 相乘, 可以理解为 argmax 点乘
logits = tf.matmul(input_tensor, output_weights, transpose_b=True)
# 以下两行等价于 softmax_cross_entropy, 多分类, 见参考[1]
log_probs = tf.nn.log_softmax(logits, axis=-1)
per_example_loss = -tf.reduce_sum(log_probs * one_hot_labels, axis=[-1])

next sentence predict task

GLUE中很多任务涉及到语言推理, 即判断两个句子之间的关系, 所以预训练阶段也要有涉及句子关系的任务,原文说 pre-training towards this task is very beneficial to both QA and NLI.
bert使用[CLS]的hidden vector , 又给跟了一个FC,来做二分类.
dense(hidden_size, tanh), 再 dense(2, activation=None)
代码见下.

with tf.variable_scope("pooler"):
    # We "pool" the model by simply taking the hidden state corresponding
    # to the first token. We assume that this has been pre-trained
    first_token_tensor = tf.squeeze(self.sequence_output[:, 0:1, :], axis=1)
    pooled_output = tf.layers.dense(
        first_token_tensor,
        config.hidden_size, # 768
        activation=tf.tanh, # in [-1,1]
        kernel_initializer=create_initializer(config.initializer_range))

def get_next_sentence_output(bert_config, input_tensor, labels):
    """Get loss and log probs for the next sentence prediction."""

    # Simple binary classification. Note that 0 is "next sentence" and 1 is
    # "random sentence". This weight matrix is not used after pre-training.
    with tf.variable_scope("yichu_cls/seq_relationship"):
        output_weights = tf.get_variable(
            "output_weights",
            shape=[2, config.hidden_size],  # 变更 hidden_size 为 128
            initializer=modeling.create_initializer(bert_config.initializer_range))
        output_bias = tf.get_variable(
            "output_bias", shape=[2], initializer=tf.zeros_initializer())
        # input_tensor 即上文 pooled_output
        logits = tf.matmul(input_tensor, output_weights, transpose_b=True)
        logits = tf.nn.bias_add(logits, output_bias)
        log_probs = tf.nn.log_softmax(logits, axis=-1)
	    labels = tf.reshape(labels, [-1])
	    one_hot_labels = tf.one_hot(labels, depth=2, dtype=tf.float32)
	    per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
	    loss = tf.reduce_mean(per_example_loss)
	    return (loss, per_example_loss, log_probs)

代码中采用了 log_softmax() 函数, 其实就是 softmax_crossentropy, 对比讨论见参考[7]

additional pre-train

官方在github发布了 BERT-Base, Chinese 预训练模型, Chinese Simplified and Traditional, 12-layer, 768-hidden, 12-heads, 110M parameters.
我们可以基于这个release的ckpt, 用自有的corpus, 做 pre-train 续跑.
If your task has a large domain-specific corpus available (e.g., “movie reviews” or “scientific papers”), it will likely be beneficial to run additional steps of pre-training on your corpus, starting from the BERT checkpoint.

改动模型

因为官方release的 BERT-Base, Chinese , 其 hidden size=768, 而我们若想要低维(如d=128)的hidden vector, 便可在网络最后加了一个 128d的dense layer.

第一次续跑

  • init_checkpoint
    此处为官方ckpt, 如 bert_base_dir/chinese_L-12_H-768_A-12/bert_model.ckpt .
    使用tf.train.init_from_checkpoint(init_checkpoint, assignment_map)函数恢复ckpt中的所有parameter.
  • output_dir
    该续跑模型的目录. 对应 tf.estimator.RunConfig(model_dir=FLAGS.output_dir,...)

续跑的续跑

此时不再需要 init_checkpoint 参数. 所有的参数(以及自有模型新加的参数)都已经在output_dir 中了.

fine-tune

略.
图: 不同下游任务上的相应 fine-tune 结构.
注意[CLS]在句子对及单句子任务中不同的用法.

feature extract

In certain cases, rather than fine-tuning the entire pre-trained model end-to-end, it can be beneficial to obtained pre-trained contextual embeddings, which are fixed contextual representations of each input token generated from the hidden layers of the pre-trained model.
对应extract_features.py脚本.

  • init_checkpoint
    如果自己续跑过, 就用自有的ckpt, 否则用官方release的.

三. RoBERTa 等后继者

RoBERTa, A Robustly Optimized BERT Pretraining Approach, 一个稳健的 BERT 方法(不喜欢’鲁棒’的译法).

  • 数据集. 增加 CC-NEWS, 原16G->160G.
  • 计算图/网络结构. 更长的序列, 移除NSP任务.
  • 训练过程. 加大 batch size, 修改 adam 的参数.

性能比较, 见下图. 比如在 MNLI 任务上的指标从 86->90 .
在这里插入图片描述

参考

  1. glue 网站
  2. glue论文
  3. RoBERTa: A Robustly Optimized BERT Pretraining Approach
### MLPerf 基准测试中关于 BERT 的性能评估实现细节 MLPerf 是一种用于衡量机器学习硬件、软件和服务性能的标准基准测试套件。它涵盖了训练和推理两个方面,并针对多种任务提供了标准化的评测方法,其中包括自然语言处理(NLP)。以下是有关 MLPerf 中 BERT 实现及其性能评估的关键点: #### 1. **BERT 在 MLPerf 中的角色** 在 MLPerf 推理基准测试中,BERT 被广泛应用于 NLP 领域的任务,特别是涉及文本分类、问答系统等场景。作为一项重要的模型,BERT 的性能被用来评价硬件平台以及优化框架的能力[^3]。 #### 2. **预训练微调过程的影响** 根据已知的研究成果,在实际应用中,BERT 的性能很大程度上依赖于其预训练的质量以及后续微调的效果。具体来说: - **预训练阶段**:此阶段利用大规模无标注语料库完成自监督学习目标,例如掩码语言建模 (Masked Language Modeling, MLM) 和下一句预测 (Next Sentence Prediction, NSP)。 - **微调阶段**:在此过程中,基于特定任务的数据集调整权重参数以适应具体的下游应用场景,比如 GLUE 数据集上的各项子任务[^2]。 这些步骤共同决定了最终部署环境中 BERT 表现如何。 #### 3. **性能指标考量因素** 当讨论到 MLPerf 对 BERT 性能测量时,以下几个维度通常会被考虑进去: - **吞吐量 Throughput**: 即单位时间内能够处理多少样本数量; - **延迟 Latency**: 处理单个请求所需时间长短; - **准确性 Accuracy**: 输出结果相对于真实标签的一致程度; 对于像中文 LLaMA 或 Alpaca 这样的大型语言模型(LLMs),虽然它们可能采用不同于传统 Transformer 架构的设计思路来提升效率及效果,但在某些情况下仍需参照类似的评估标准来进行对比分析[^1]。 #### 4. **技术优化手段** 为了提高 BERT 在 MLPerf 测试中的表现水平,研究者们尝试了各种各样的策略和技术改进措施,包括但不限于以下几种方式: - 使用混合精度计算减少内存占用并加速运算速度; - 应用知识蒸馏(Knowledge Distillation) 技术创建更轻量化版本的小型化模型; - 利用剪枝(Pruning) 方法去除冗余连接从而降低复杂度; 下面展示了一段 Python 示例代码片段演示如何设置 TensorFlow/Keras 下面进行混合精度训练: ```python import tensorflow as tf # 启用自动混合精度 policy = tf.keras.mixed_precision.Policy('mixed_float16') tf.keras.mixed_precision.set_global_policy(policy) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) ``` 上述代码展示了启用 TensorFlow 自动混合精度功能的过程,这有助于加快 GPU 上深度神经网络的收敛速率同时保持较高的数值稳定性。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值