Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update checks for bias in initialization #4574

Merged
merged 3 commits into from
Aug 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions espnet2/torch_utils/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"""Initialize modules for espnet2 neural networks."""

import logging
import math

import torch
Expand All @@ -24,11 +25,17 @@ def initialize(model: torch.nn.Module, init: str):

if init == "chainer":
# 1. lecun_normal_init_parameters
for p in model.parameters():
for name, p in model.named_parameters():
data = p.data
if data.dim() == 1:
if ".bias" in name and data.dim() == 1:
# bias
data.zero_()
logging.info(f"Initialize {name} to zeros")
elif data.dim() == 1:
# linear weight
n = data.size(0)
stdv = 1.0 / math.sqrt(n)
data.normal_(0, stdv)
elif data.dim() == 2:
# linear weight
n = data.size(1)
Expand Down Expand Up @@ -75,9 +82,10 @@ def initialize(model: torch.nn.Module, init: str):
else:
raise ValueError("Unknown initialization: " + init)
# bias init
for p in model.parameters():
if p.dim() == 1:
for name, p in model.named_parameters():
if ".bias" in name and p.dim() == 1:
p.data.zero_()
logging.info(f"Initialize {name} to zeros")

# reset some modules with default init
for m in model.modules():
Expand Down