
要初始化单个图层的权重,请使用torch.nn.init中的函数.例如:
conv1 = torch.nn.Conv2d(...)torch.nn.init.xavIEr_uniform(conv1.weight)
或者,您可以通过写入conv1.weight.data(这是一个torch.Tensor)来修改参数.例:
conv1.weight.data.fill_(0.01)
这同样适用于偏见:
conv1.bias.data.fill_(0.01)
nn.Sequential或custom nn.Module
将初始化函数传递给torch.nn.Module.apply.它将递归地初始化整个nn.Module中的权重.
apply(fn): ApplIEs
fnrecursively to every submodule (as returned by.children()) as well as self. Typical use includes initializing the parameters of a model (see also torch-nn-init).
例:
def init_weights(m): if type(m) == nn.linear: torch.nn.init.xavIEr_uniform(m.weight) m.bias.data.fill_(0.01)net = nn.Sequential(nn.linear(2,2),nn.linear(2,2))net.apply(init_weights)总结
以上是内存溢出为你收集整理的python – 如何在PyTorch中初始化权重?全部内容,希望文章能够帮你解决python – 如何在PyTorch中初始化权重?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)