资源简介
流行BERT模型的一个简单而完整的实现
代码片段和文件信息
# coding=utf-8
import torch
from torch.optim import Optimizer
class AdamWeightDecayOptimizer(Optimizer):
“““A basic Adam optimizer that includes “correct“ L2 weight decay.
https://github.com/google-research/bert/blob/master/optimization.py
https://raw.githubusercontent.com/pytorch/pytorch/v1.0.0/torch/optim/adam.py“““
def __init__(self params lr=1e-3 betas=(0.9 0.999) eps=1e-8
weight_decay=0 amsgrad=False):
if not 0.0 <= lr:
raise ValueError(“Invalid learning rate: {}“.format(lr))
if not 0.0 <= eps:
raise ValueError(“Invalid epsilon value: {}“.format(eps))
if not 0.0 <= betas[0] < 1.0:
raise ValueError(“Invalid beta parameter at index 0: {}“.format(betas[0]))
if not 0.0 <= betas[1] < 1.0:
raise ValueError(“Invalid beta parameter at index 1: {}“.format(betas[1]))
defaults = dict(lr=lr betas=betas eps=eps
weight_decay=weight_decay amsgrad=amsgrad)
super(AdamWeightDecayOptimizer self).__init__(params defaults)
def __setstate__(self state):
super(AdamWeightDecayOptimizer self).__setstate__(state)
for group in self.param_groups:
group.setdefault(‘amsgrad‘ False)
def step(self closure=None):
“““Performs a single optimization step.
Arguments:
closure (callable optional): A closure that reevaluates the model
and returns the loss.
“““
loss = None
if closure is not None:
loss = closure()
for group in self.param_groups:
for p in group[‘params‘]:
if p.grad is None:
continue
grad = p.grad.data
if grad.is_sparse:
raise RuntimeError(‘Adam does not support sparse gradients please consider SparseAdam instead‘)
amsgrad = group[‘amsgrad‘]
state = self.state[p]
# State initialization
if len(state) == 0:
state[‘step‘] = 0
# Exponential moving average of gradient values
state[‘exp_avg‘] = torch.zeros_like(p.data)
# Exponential moving average of squared gradient values
state[‘exp_avg_sq‘] = torch.zeros_like(p.data)
if amsgrad:
# Maintains max of all exp. moving avg. of sq. grad. values
state[‘max_exp_avg_sq‘] = torch.zeros_like(p.data)
exp_avg exp_avg_sq = state[‘exp_avg‘] state[‘exp_avg_sq‘]
if amsgrad:
max_exp_avg_sq = state[‘max_exp_avg_sq‘]
beta1 beta2 = group[‘betas‘]
state[‘step‘] += 1
# Decay the first and second moment running average coefficient
exp_avg.mul_(beta1).add_(1 - beta1 grad)
exp_avg_sq.mul_
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-03-15 10:01 BERT-master\
文件 66 2019-03-15 10:01 BERT-master\.gitattributes
文件 1316 2019-03-15 10:01 BERT-master\.gitignore
文件 1430 2019-03-15 10:01 BERT-master\README.md
文件 4134 2019-03-15 10:01 BERT-master\adam.py
文件 5522 2019-03-15 10:01 BERT-master\bert.py
文件 4687 2019-03-15 10:01 BERT-master\data.py
文件 7028 2019-03-15 10:01 BERT-master\example_use.py
文件 292 2019-03-15 10:01 BERT-master\example_use.sh
文件 9862 2019-03-15 10:01 BERT-master\google_bert.py
文件 2754 2019-03-15 10:01 BERT-master\preprocess.py
目录 0 2019-03-15 10:01 BERT-master\toy\
文件 323 2019-03-15 10:01 BERT-master\toy\gen.py
文件 193086 2019-03-15 10:01 BERT-master\toy\sample_from_zhwiki
文件 1883 2019-03-15 10:01 BERT-master\toy\train
文件 110 2019-03-15 10:01 BERT-master\toy\vocab
文件 6682 2019-03-15 10:01 BERT-master\train.py
文件 831 2019-03-15 10:01 BERT-master\train.sh
文件 10717 2019-03-15 10:01 BERT-master\transformer.py
文件 1047 2019-03-15 10:01 BERT-master\utils.py
相关资源
- Python-NLP之旅包含NLP文章代码集锦
- Python-python3实现互信息和左右熵的新词
- Python-全唐诗分析程序
- Python-pycorrector中文错别字纠正工具音
- Python-对四种句子文本相似度计算方法
- Python-爬取百度百科中文页面抽取三元
- Python-CCKS2017中文电子病例命名实体识
- Python-这是GoogleBERT模型的一个Pytorch重
- Python-深度学习与自然语言处理知识图
- Python-利用Python实现中文文本关键词抽
- Python-2019年百度的三元组抽取比赛一个
- Python-基于TensorFlow和BERT的管道式实体
评论
共有 条评论