bert介绍和使用

pretrain两个任务:

论文不使用传统的从左到右或从右到左的语言模型来预训练BERT。相反,使用两个新的无监督预测任务对BERT进行预训练。

1 预测词

 双向 采用MLM(mask language model)[只预测masked words而不是重建整个句子]

为了达到真正的bidirectional的LM的效果,作者创新性的提出了Masked LM,但是缺点是如果常常把一些词mask起来,未来的fine tuning过程中模型有可能没见过这些词。这个量积累下来还是很大的。因为作者在他的实现中随机选择了句子中15%的WordPiece tokens作为要mask的词。

为了解决这个问题,作者在做mask的时候,

80%的时间真的用[MASK]取代被选中的词。比如 my dog is hairy -> my dog is [MASK]

10%的时间用一个随机词取代它:my dog is hairy -> my dog is apple

10%的时间保持不变: my dog is hairy -> my dog is hairy

为什么要以一定的概率保持不变呢? 这是因为刚才说了,如果100%的时间都用[MASK]来取代被选中的词,那么在fine tuning的时候模型会有一些没见过的词。那么为啥要以一定的概率使用随机词呢?这是因为Transformer要保持对每个输入token分布式的表征,否则Transformer很可能会记住这个[MASK]就是"hairy"。至于使用随机词带来的负面影响,文章中说了,所有其他的token(即非"hairy"的token)共享15%*10% = 1.5%的概率,其影响是可以忽略不计的

2 预测下一句

与从左到右的语言模型预训练不同,MLM 目标允许表征融合左右两侧的语境,从而预训练一个深度双向 Transformer。除了遮蔽语言模型之外,本文作者还引入了一个“下一句预测”(next sentence prediction)任务,可以和MLM共同预训练文本对的表示。

3 其中transformer核心介绍

multi-head attention

Scaled Dot-Product Attention

Multi-Head Attention就是把Scaled Dot-Product Attention的过程做H次,然后把输出Z合起来。论文中,它的结构图如下:

我们还是以上面的形式来解释:

我们重复记性8次相似的操作,得到8个Zi矩阵

为了使得输出与输入结构对标 乘以一个线性W0 得到最终的Z。

https://mp.weixin.qq.com/s/7RgCIFxPGnREiBk8PcxOBg

4 代码

谷歌开源的bert代码以及提供的训练好的模型https://github.com/google-research/bert

对bert进行封装,提供一个输入给出对应的向量https://github.com/lbda1/bert-as-service

基于bert的实体识别https://github.com/lbda1/BERT-NER

4 bert词向量输出https://blog.csdn.net/luoyexuge/article/details/84939755

import  tensorflow as  tf
from bert import modeling
import os
import collections
import  six
from gevent import monkey
monkey.patch_all()
from flask import Flask, request
from gevent import pywsgi
import numpy  as np
import json
flags = tf.flags

FLAGS = flags.FLAGS

bert_path = 'bert_model/'
//bert模型

flags.DEFINE_string(
    "bert_config_file", os.path.join(bert_path, 'bert_config.json'),
    "The config json file corresponding to the pre-trained BERT model."
)

flags.DEFINE_string(
    "bert_vocab_file", os.path.join(bert_path, 'vocab.txt'),
    "The config vocab file"
)

flags.DEFINE_string(
    "init_checkpoint", os.path.join(bert_path, 'bert_model.ckpt'),
    "Initial checkpoint (usually from a pre-trained BERT model)."
)

app = Flask(__name__)

def convert_to_unicode(text):
  """Converts `text` to Unicode (if it's not already), assuming utf-8 input."""
  if six.PY3:
    if isinstance(text, str):
      return text
    elif isinstance(text, bytes):
      return text.decode("utf-8", "ignore")
    else:
      raise ValueError("Unsupported string type: %s" % (type(text)))
  elif six.PY2:
    if isinstance(text, str):
      return text.decode("utf-8", "ignore")
    elif isinstance(text, unicode):
      return text
    else:
      raise ValueError("Unsupported string type: %s" % (type(text)))
  else:
    raise ValueError("Not running on Python2 or Python 3?")


def load_vocab(vocab_file):
  vocab = collections.OrderedDict()
  vocab.setdefault("blank",2)
  index = 0
  with tf.gfile.GFile(vocab_file, "r") as reader:
    while True:
      token = convert_to_unicode(reader.readline())
      if not token:
        break
      token = token.strip()
      vocab[token] = index
      index += 1
  return vocab

di=load_vocab(vocab_file=FLAGS.bert_vocab_file)
init_checkpoint=FLAGS.init_checkpoint
use_tpu=False

sess=tf.Session()

bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file)

print(init_checkpoint)

is_training=False
use_one_hot_embeddings=False

def inputs(vectors,maxlen=10):
    length=len(vectors)
    if length>=maxlen:
        return  vectors[0:maxlen],[1]*maxlen,[0]*maxlen
    else:
        input=vectors+[0]*(maxlen-length)
        mask=[1]*length+[0]*(maxlen-length)
        segment=[0]*maxlen
        return input,mask,segment


input_ids_p=tf.placeholder(shape=[None,None],dtype=tf.int32,name="input_ids_p")
input_mask_p=tf.placeholder(shape=[None,None],dtype=tf.int32,name="input_mask_p")
segment_ids_p=tf.placeholder(shape=[None,None],dtype=tf.int32,name="segment_ids_p")

model = modeling.BertModel(
        config=bert_config,
        is_training=is_training,
        input_ids=input_ids_p,
        input_mask=input_mask_p,
        token_type_ids=segment_ids_p,
        use_one_hot_embeddings=use_one_hot_embeddings
    )

restore_saver = tf.train.Saver()
restore_saver.restore(sess, init_checkpoint)



@app.route('/bertvectors')
def response_request():
    text = request.args.get('text')

    vectors = [di.get("[CLS]")] + [di.get(i) if i in di else di.get("[UNK]") for i in list(text)] + [di.get("[SEP]")]

    input, mask, segment = inputs(vectors)

    input_ids = np.reshape(np.array(input), [1, -1])
    input_mask = np.reshape(np.array(mask), [1, -1])
    segment_ids = np.reshape(np.array(segment), [1, -1])

    embedding = tf.squeeze(model.get_sequence_output())

    ret=sess.run(embedding,feed_dict={"input_ids_p:0":input_ids,"input_mask_p:0":input_mask,"segment_ids_p:0":segment_ids})
    return  json.dumps(ret.tolist(), ensure_ascii=False)

if __name__ == "__main__":
    server = pywsgi.WSGIServer(('0.0.0.0', 19877), app)
    server.serve_forever()

使用方式http://localhost:19877/bertvectors?text="hello"

MG-BERT是一种基于BERT模型的新型预训练语言模型。它结合了多粒度信息和多任务学习,能够更好地处理中文语言的复杂性和多义性。下面就对MG-BERT进行详细的介绍。 一、MG-BERT的背景及意义 自从BERT模型问世以来,预训练模型在自然语言处理领域中得到了广泛应用。然而,BERT模型只能处理单一粒度的信息,而中文语言的复杂性和多义性使得单一粒度的信息处理难以满足需求。因此,研究者们提出了MG-BERT,通过结合多粒度信息和多任务学习,可以更好地处理中文语言的复杂性和多义性。 二、MG-BERT的设计 MG-BERT是基于BERT模型的改进版本,它主要包含以下三个方面的改进。 1.多粒度信息的处理 MG-BERT通过多粒度信息的处理,能够更好地抓住中文语言的复杂性和多义性。具体来讲,MG-BERT通过引入汉字级别的信息,可以更准确地处理中文语言的复杂性。同时,MG-BERT还引入了词级别和句子级别的信息,可以更好地处理中文语言的多义性。 2.多任务学习的应用 MG-BERT通过多任务学习的应用,可以更好地处理不同的自然语言处理任务。具体来讲,MG-BERT结合了语言模型任务、分类任务和序列标注任务,可以更好地处理自然语言处理任务。 3.微调方法的改进 MG-BERT通过改进微调方法,可以更好地适应各种自然语言处理任务。具体来讲,MG-BERT通过引入动态学习率调整和多层全连接网络来微调模型,可以更好地适应各种自然语言处理任务。 三、MG-BERT的实验结果 为了验证MG-BERT的有效性,研究者们在多个自然语言处理任务上进行了实验。实验结果表明,MG-BERT在多个任务上的表现都比BERT模型要好。具体来讲,MG-BERT在语言模型任务、分类任务和序列标注任务上的表现都比BERT模型要好。 四、结论 MG-BERT是一种基于BERT模型的新型预训练语言模型。它结合了多粒度信息和多任务学习,能够更好地处理中文语言的复杂性和多义性。实验结果表明,MG-BERT在多个自然语言处理任务上表现出了优异的性能,具有很大的应用潜力。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值