PyTorch学习:参数初始化

Sequential 模型的参数初始化


import numpy as np
import torch
from torch import nn

# 定义一个 Sequential 模型
net1 = nn.Sequential(
                nn.Linear(2, 4), #nn.Linear(2, 4) shape为(4,2)
                nn.ReLU(),
                nn.Linear(4, 5),
                nn.ReLU(),
                nn.Linear(5, 2)
               )

# 访问第一层的参数
w1 = net1[0].weight
b1 = net1[0].bias
print(w1)

# 定义一个 Tensor 直接对其进行替换
net1[0].weight.data = torch.from_numpy(np.random.uniform(3, 5, size=(4, 2)))
print(net1[0].weight)
for layer in net1:
    if isinstance(layer, nn.Linear): # 判断是否是线性层
    param_shape = layer.weight.shape
    layer.weight.data = torch.from_numpy(np.random.normal(0, 0.5, size=param_shape))

# 定义为均值为 0,方差为 0.5 的正态分布
  1.  

 Module 模型的参数初始化

 
  1. class sim_net(nn.Module):
        def __init__(self):
    		super(sim_net, self).__init__()
    		self.l1 = nn.Sequential(nn.Linear(2, 4),nn.ReLU())
    	    self.l1[0].weight.data = torch.randn(4, 2) # 直接对某一层初始化
    	    self.l2 = nn.Sequential(nn.Linear(4, 5),nn.ReLU())
    	    self.l3 = nn.Sequential(nn.Linear(5, 2),nn.ReLU())
    	def forward(self, x):
    		x = self.l1(x)
    		x = self.l2(x)
    		x = self.l3(x)
    		return x
    net2 = sim_net()
    a=0
    
    # 访问 children
    for i in net2.children():
    	print(a)
    	a+=1
    	print(i)

     

  2.  

0
Sequential(
  (0): Linear(in_features=2, out_features=4, bias=True)
  (1): ReLU()
)
1
Sequential(
  (0): Linear(in_features=4, out_features=5, bias=True)
  (1): ReLU()
)
2
Sequential(
  (0): Linear(in_features=5, out_features=2, bias=True)
  (1): ReLU()
)

 
  1. # # 访问 modules

  2. a=0

  3. for i in net2.modules():

  4. print(a)

  5. a+=1

  6. print(i)

  7. #children 只会访问到模型定义中的第一层,因为上面的模型中定义了三个 Sequential,

  8. #所以只会访问到三个 Sequential,而 modules 会访问到最后的结构.

  9. #比如上面的例子,modules 不仅访问到了 Sequential,也访问到了 Sequential 里面,这就对我们做初始化非常方便.

0
sim_net(
  (l1): Sequential(
    (0): Linear(in_features=2, out_features=4, bias=True)
    (1): ReLU()
  )
  (l2): Sequential(
    (0): Linear(in_features=4, out_features=5, bias=True)
    (1): ReLU()
  )
  (l3): Sequential(
    (0): Linear(in_features=5, out_features=2, bias=True)
    (1): ReLU()
  )
)
1
Sequential(
  (0): Linear(in_features=2, out_features=4, bias=True)
  (1): ReLU()
)
2
Linear(in_features=2, out_features=4, bias=True)
3
ReLU()
4
Sequential(
  (0): Linear(in_features=4, out_features=5, bias=True)
  (1): ReLU()
)
5
Linear(in_features=4, out_features=5, bias=True)
6
ReLU()
7
Sequential(
  (0): Linear(in_features=5, out_features=2, bias=True)
  (1): ReLU()
)
8
Linear(in_features=5, out_features=2, bias=True)
9
ReLU()

 
  1. for layer in net2.modules():

  2. if isinstance(layer, nn.Linear):

  3. print(layer)

  4. param_shape = layer.weight.shape

  5. layer.weight.data = torch.from_numpy(np.random.normal(0, 0.5, size=param_shape))

Linear(in_features=2, out_features=4, bias=True)
Linear(in_features=4, out_features=5, bias=True)
Linear(in_features=5, out_features=2, bias=True)

torch.nn.init

因为 PyTorch 灵活的特性,我们可以直接对 Tensor 进行操作从而初始化,PyTorch 也提供了初始化的函数帮助我们快速初始化,就是 torch.nn.init,其操作层面仍然在 Tensor 上.

 
  1. from torch.nn import init

  2. w = torch.Tensor(3, 5)

  3. init.xavier_uniform_(w, gain=1)

  4. print("w:",w)

init.xavier_uniform_(layer.weight, gain=1)  #tensor – n维的torch.Tensor  gain - 可选的缩放因子

转自:https://blog.csdn.net/qq_35447659/article/details/84030286

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值