Skip to content

Instantly share code, notes, and snippets.

@sadimanna
Created November 1, 2023 10:14
Show Gist options
  • Save sadimanna/8cd4f3c93e84ea4215fceed2fa40e2a9 to your computer and use it in GitHub Desktop.
Save sadimanna/8cd4f3c93e84ea4215fceed2fa40e2a9 to your computer and use it in GitHub Desktop.
reg_loss = 0
for mod in self.model.modules():
if isinstance(mod, _BatchNorm):
if self.decay_bn:
for name, param in mod.named_parameters(recurse=False):
reg_loss = reg_loss + param.norm(2)
else:
for name, param in mod.named_parameters(recurse=False):
if not name.endswith("bias"):
reg_loss = reg_loss + param.norm(2)
else:
if self.decay_bias:
reg_loss = reg_loss + param.norm(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment