Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/test_conv_substring_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import torch.nn as nn
from utils import apply_weight_norm, init_weights


def test_init_weights_and_weight_norm_apply_only_to_conv_modules():
conv = nn.Conv1d(1, 1, 3)
linear = nn.Linear(3, 3)

init_weights(conv, std=0.5)
init_weights(linear, std=0.5)

# Conv weights are re-initialized; linear weights stay at zero.
assert conv.weight.std().item() > 0
assert linear.weight.std().item() == 0

apply_weight_norm(conv)
apply_weight_norm(linear)

# Weight normalization is applied only to Conv modules.
assert hasattr(conv, "weight_g")
4 changes: 2 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ def plot_spectrogram_clipped(spectrogram, clip_max=2.0):

def init_weights(m, mean=0.0, std=0.01):
classname = m.__class__.__name__
if classname.find("Conv") != -1:
if "Conv" in classname:
m.weight.data.normal_(mean, std)


def apply_weight_norm(m):
classname = m.__class__.__name__
if classname.find("Conv") != -1:
if "Conv" in classname:
weight_norm(m)


Expand Down