torch.nn.init常用函数总结

torch.nn.init中常用的函数:

  1. torch.nn.init.uniform_
  2. torch.nn.init.normal_
  3. torch.nn.init.constant_
  4. torch.nn.init.ones_
  5. torch.nn.init.zeros_
  6. torch.nn.init.eye_
  7. torch.nn.init.sparse_

torch.nn.init

torch.nn.init.uniform_

torch.nn.init.uniform_(tensor, a=0.0, b=1.0)

按均匀分布 U ( a , b ) U(a,b) U(a,b)tensor随机赋值。

import torch
import torch.nn as nn

class MyModel(nn.Module):
    def __init__(self, **kwargs):
        super(MyModel, self).__init__()

        self.linear = nn.Linear(in_features=3, out_features=1)
        # 此时输出为随机赋值的参数值
        print(self.linear.weight)
        print(self.linear.bias)
        print()
        # 使用nn.Parameter对参数赋值
        self.linear.weight = nn.Parameter(torch.ones(1, 3, dtype=torch.float32))
        self.linear.bias = nn.Parameter(torch.ones(1, dtype=torch.float32))
        print(self.linear.weight)
        print(self.linear.bias)
        print()
        # 使用nn.init.uniform_对参数赋值
        nn.init.uniform_(self.linear.weight)
        nn.init.uniform_(self.linear.bias)
        print(self.linear.weight)
        print(self.linear.bias)

    def forward(self, x):
        pass

n = MyModel()
print()
for name, param in n.named_parameters(): # 显示注册的参数
    print(name)
    print(param)

"""
Parameter containing:
tensor([[ 0.2034, -0.0841,  0.5076]], requires_grad=True)
Parameter containing:
tensor([-0.1976], requires_grad=True)

Parameter containing:
tensor([[1., 1., 1.]], requires_grad=True)
Parameter containing:
tensor([1.], requires_grad=True)

Parameter containing:
tensor([[0.3873, 0.0731, 0.8832]], requires_grad=True)
Parameter containing:
tensor([0.1925], requires_grad=True)

linear.weight
Parameter containing:
tensor([[0.3873, 0.0731, 0.8832]], requires_grad=True)
linear.bias
Parameter containing:
tensor([0.1925], requires_grad=True)
"""

查看源码可以发现这么一句:with torch.no_grad():,这样一来我们就不需要自己写这句话了。(下面的函数同)

torch.nn.init.normal_

torch.nn.init.normal_(tensor, mean=0.0, std=1.0)

按正态分布 N ( μ 或 m e a n , σ 或 s t d ) N(\mu或mean,\sigma或std) N(μmean,σstd)tensor随机赋值。

torch.nn.init.constant_

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

使用常数valtensor赋值。

import torch.nn as nn

class MyModel(nn.Module):
    def __init__(self, **kwargs):
        super(MyModel, self).__init__()

        self.linear = nn.Linear(in_features=3, out_features=1)
        # 使用nn.init.constant_对参数赋值
        nn.init.constant_(self.linear.weight, 0.2)
        nn.init.constant_(self.linear.bias, 0.2)
        print(self.linear.weight)
        print(self.linear.bias)

    def forward(self, x):
        pass

n = MyModel()
"""
Parameter containing:
tensor([[0.2000, 0.2000, 0.2000]], requires_grad=True)
Parameter containing:
tensor([0.2000], requires_grad=True)
"""

torch.nn.init.ones_

torch.nn.init.ones_(tensor)

使用常数1tensor赋值。

torch.nn.init.zeros_

torch.nn.init.zeros_(tensor)

使用常数0tensor赋值。

torch.nn.init.eye_

torch.nn.init.eye_(tensor)

使用常数1tensor的主对角线进行赋值,其余位置为常数0

torch.nn.init.sparse_

torch.nn.init.sparse_(tensor, sparsity, std=0.01)

生成稀疏矩阵。

sparsity :每列中需要被设置成零的元素比例。

std :用于生成的正态分布的标准差。

import torch
import torch.nn as nn

w = torch.empty(8, 5, dtype=torch.float32)
print(nn.init.sparse_(w, sparsity=0.5))
"""
tensor([[-0.0058, -0.0074, -0.0078, -0.0055,  0.0000],
        [ 0.0000, -0.0023,  0.0000,  0.0014,  0.0046],
        [ 0.0000,  0.0000,  0.0000,  0.0003, -0.0004],
        [-0.0029,  0.0000,  0.0000,  0.0000,  0.0000],
        [ 0.0000, -0.0127,  0.0000, -0.0078, -0.0174],
        [-0.0017,  0.0000,  0.0049,  0.0000,  0.0000],
        [ 0.0000,  0.0000, -0.0067,  0.0000,  0.0072],
        [ 0.0091,  0.0027, -0.0008,  0.0000,  0.0000]])
"""

你可能感兴趣的:(【Pytorch学习】,pytorch,深度学习,python)