bert pytorch源码_BERT Pytorch版本 源码解析(一)

本文介绍了BERT在PyTorch中的实现,包括安装、BertPreTrainedModel类的初始化和from_pretrained函数,用于加载预训练模型。详细解析了模型加载过程,包括对模型名称或本地路径的处理,以及如何从URL下载预训练模型。最后,简述了BertModel的初始化和前向传播过程。
摘要由CSDN通过智能技术生成

BERT Pytorch版本 源码解析(一)

一、BERT安装方式

pip install pytorch-pretrained-bert

二、BertPreTrainModel:

一个用于获取预训练好权重的抽象类,一个用于下载和载入预训练模型的简单接口

1、初始化函数(def __init__(self, config, *inputs, **kwargs)):

def __init__(self, config, *inputs, **kwargs):

super(BertPreTrainedModel, self).__init__()

if not isinstance(config, BertConfig):

raise ValueError(

"Parameter config in `{}(config)` should be an instance of class `BertConfig`. "

"To create a model from a Google pretrained model use "

"`model = {}.from_pretrained(PRETRAINED_MODEL_NAME)`".format(

self.__class__.__name__, self.__class__.__name__

))

self.config = config

初始化函数主要是用于传入BertConfig的一个对象,这样可以获得Bert模型所需的模型参数,例如hidden_size等

2、最重要的from_pretrained函数: def from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs)

pretrained_model_name_or_path: either:

- a str with the name of a pre-trained model to load selected in the list of:

. `bert-base-uncased`

. `bert-large-uncased`

. `bert-base-cased`

. `bert-large-cased`

. `bert-base-multilingual-uncased`

. `bert-base-multilingual-cased`

. `bert-base-chinese`

- a path or url to a pretrained model archive containing:

. `bert_config.json` a configuration file for the model

. `pytorch_model.bin` a PyTorch dump of a BertForPreTraining instance

- a path or url to a pretrained model archive containing:

. `bert_config.json` a configuration file for the model

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值