import torch from torch import nn net=nn.Sequential(nn.Linear(4,8),nn.ReLU(),nn.Linear(8,1)) x=torch.rand(size=(2,4)) y=net(x) print(y) print(net[2].state_dict()) print(type(net[2].bias)) print(net[2].bias) print(net[2].bias.data) print(net[2].weight.grad==None) print(*[(name,param.shape)for name,param in net[0].named_parameters()]) print(*[(name,param.shape)for name,param in net.named_parameters()]) a=net.state_dict()['2.bias'].data print(a) # 有嵌套情况下 def block1(): return nn.Sequential(nn.Linear(4,8),nn.ReLU(),nn.Linear(8,4),nn.ReLU()) def block2(): net=nn.Sequential() for i in range(4): net.add_module(f'block{i}',block1()) return net rgnet=nn.Sequential(block2(),nn.Linear(4,1)) rgnet(x) print(rgnet) # 修改默认的初始参数 def init_normal(m):#m就是一个moudle if type(m)==nn.Linear: nn.init.normal_(m.weight,mean=0,std=0.01)#替换函数 nn.init.zeros_(m.bias) #所有函数都在init里面 net.apply(init_normal)#对于所有net的层,调用函数,遍历一遍6 c,d=net[0].weight.data[0],net[0].bias.data[0] print(c,d) def init_constant(m): if type(m)==nn.Linear: nn.init.constant_(m.weight,1) nn.init.zeros_(m.bias) net.apply(init_constant) e,f=net[0].weight.data[0],net[0].bias.data[0] print(e,f) #简单的方法 net[0].weight.data[:]+=1 net[0].weight.data[0,0]=42 g=net[0].weight.data[0] #层共享参数 shared=nn.Linear(8,8) net=nn.Sequential(nn.Linear(4,8),nn.ReLU(),shared,nn.ReLU(),shared,nn.ReLU(),nn.Linear(8,1)) print(net)
pytorch参数管理
最新推荐文章于 2024-05-17 10:41:35 发布