pytorch默认初始化_Pytorch参数初始化--默认与自定义

1. Pytorch module默认初始化

Pytorch 的Module都只带默认的初始化方法,而且初始化会调用此函数,因此我们定义好后,不用手动初始化。

Conv

class _ConvNd(Module):

__constants__ = ['stride', 'padding', 'dilation', 'groups', 'bias',

'padding_mode', 'output_padding', 'in_channels',

'out_channels', 'kernel_size']

def __init__(self, in_channels, out_channels, kernel_size, stride,

padding, dilation, transposed, output_padding,

groups, bias, padding_mode):

super(_ConvNd, self).__init__()

if in_channels % groups != 0:

raise ValueError('in_channels must be divisible by groups')

if out_channels % groups != 0:

raise ValueError('out_channels must be divisible by groups')

self.in_channels = in_channels

self.out_channels = out_channels

self.kernel_size = kernel_size

self.stride = stride

self.padding = padding

self.dilation = dilation

self.transposed = transposed

self.output_padding = output_padding

self.groups = groups

self.padding_mode = padding_mode

if transposed:

self.weight = Parameter(torch.Tensor(

in_channels, out_channels // groups, *kernel_size))

else:

self.weight = Parameter(torch.Tensor(

out_channels, in_channels // groups, *kernel_size))

if bias:

self.bias = Parameter(torch.Tensor(out_channels))

else:

self.register_parameter('bias', None)

self.reset_parameters()

def reset_parameters(self):

init.kaiming_uniform_(self.weight, a=math.sqrt(5))

if self.bias is not None:

fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)

bound = 1 / math.sqrt(fan_in)

init.uniform_(self.bias, -bound, bound)

如上图所示,在__init__中最后一行调用函数reset_parameters进行参数初始化,卷积函数都继承了_ConvNd,因此所有卷积module都自动初始化。

我的Pytorch版本是1.2,此版本的初始化函数还是用的何凯名大神的kaiming_uniform_,真的牛逼。

Linear

class Linear(Module):

r"""Applies a linear transformation to the incoming data: :math:`y = xA^T + b`

Args:

in_features: size of each input sample

out_features: size of each output sample

bias: If set to ``False``, the layer will not learn an additive bias.

Default: ``True``

Shape:

- Input: :math:`(N, *, H_{in})` where :math:`*` means any number of

additional dimensions and :math:`H_{in} = \text{in\_features}`

- Output: :math:`(N, *, H_{out})` where all but the last dimension

are the same shape as the input and :math:`H_{out} = \text{out\_features}`.

Attributes:

weight: the learnable weights of the module of shape

:math:`(\text{out\_features}, \text{in\_features})`. The values are

initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where

:math:`k = \frac{1}{\text{in\_features}}`

bias: the learnable bias of the module of shape :math:`(\text{out\_features})`.

If :attr:`bias` is ``True``, the values are initialized from

:math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where

:math:`k = \frac{1}{\text{in\_features}}`

Examples::

>>> m = nn.Linear(20, 30)

>>> input = torch.randn(128, 20)

>>> output = m(input)

>>> print(output.size())

torch.Size([128, 30])

"""

__constants__ = ['bias', 'in_features', 'out_features']

def __init__(self, in_features, out_features, bias=True):

super(Linear, self).__init__()

self.in_features = in_features

self.out_features = out_features

self.weight = Parameter(torch.Tensor(out_features, in_features))

if bias:

self.bias = Parameter(torch.Tensor(out_features))

else:

self.register_parameter('bias', None)

self.reset_parameters()

def reset_parameters(self):

init.kaiming_uniform_(self.weight, a=math.sqrt(5))

if self.bias is not None:

fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)

bound = 1 / math.sqrt(fan_in)

init.uniform_(self.bias, -bound, bound)

如上图所示,全连接的初始化方法和卷积也是一样的。

2. 自定义

pytorch中提供了多种初始化函数:

torch.nn.init.constant(tensor, val)

torch.nn.init.normal(tensor, mean=0, std=1)

torch.nn.init.xavier_uniform(tensor, gain=1)

kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')

可以使用apply()函数初始化,也可以自行对每一个参数初始化:

def weights_init(m):

classname=m.__class__.__name__

if classname.find('Conv') != -1:

xavier(m.weight.data)

xavier(m.bias.data)

net = Net()

net.apply(weights_init) #apply函数会递归地搜索网络内的所有module并把参数表示的函数应用到所有的module上。

def weights_init(m):

if isinstance(m, nn.Conv2d):

xavier(m.weight.data)

xavier(m.bias.data)

Reference

你可能感兴趣的:(pytorch默认初始化)