Bert (Bi-directional Encoder Representations from Transformers) Pytorch 源码解读(一)
前言 Bert (Bi-directional Encoder Representations from Transfromers) 预训练语言模型可谓是2018年 NLP 领域最耀眼的模型,看过很多对 Bert 论文和原理解读的文章,但是对 Bert 源码进行解读的文章较少, 这篇博客 有一份 TensorFlow 版本的 Bert 源码解读,这里来对 Pytorch 版本的 Bert 源码记录一份 “详细” 注释。 这份基于 Pytorch 的 Bert 源码由 Espresso大神提供,地址在这 https://github.com/aespresso/a_journey_into_math_of_ml ,大家也可以在 Espresso大神 的 B站 观看他的视频,讲得非常不错。 今天记录的这一部分是 bert_model.py 文件,主要实现了 bert 的预训练模型搭建部分。 开始 1. 定义激活函数 def gelu(x): """Implementation of the gelu activation function. For information: OpenAI GPT's gelu is slightly different (and gives slightly different results): 0.5 * x * (1 + torch.tanh