Skip to content

Instantly share code, notes, and snippets.

@zackenton
Created November 27, 2017 23:06
Show Gist options
  • Save zackenton/989792b3becd258fc19a396f0fb28596 to your computer and use it in GitHub Desktop.
Save zackenton/989792b3becd258fc19a396f0fb28596 to your computer and use it in GitHub Desktop.
initilization minimal example, here for conv2d init
class Model(nn.Module):
'''
Some Model, in this case a convolution single layer
'''
def __init__(self, in_channels, out_channels, kernel_size):
super(Model, self).__init__()
self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size)
# Initialize weights
for m in self.modules():
# set only initialization on these types of layers
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
m.bias.data.zero_()
def forward(self, x):
x = self.conv2d(x)
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment