backup / modules /weights.py
MatchLab's picture
Upload folder using huggingface_hub
c94c8c9 verified
import torch.nn as nn
def _init_weights_bert(module, std=0.02):
"""
Huggingface transformer weight initialization,
most commonly for bert initialization
"""
if isinstance(module, nn.Linear):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
module.weight.data.normal_(mean=0.0, std=std)
if module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.Embedding):
module.weight.data.normal_(mean=0.0, std=std)
if module.padding_idx is not None:
module.weight.data[module.padding_idx].zero_()
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)