Python 如何在pytorch模型中初始化权重
我这里有一个相当直接的问题。 我刚刚用下面的代码中所示的upConv顺序容器替换nn.Upsample,完成了网络的重新配置。我已经通过运行summaryUNetPP,3,128,128验证了一切都已就绪,运行起来没有问题Python 如何在pytorch模型中初始化权重,python,deep-learning,pytorch,Python,Deep Learning,Pytorch,我这里有一个相当直接的问题。 我刚刚用下面的代码中所示的upConv顺序容器替换nn.Upsample,完成了网络的重新配置。我已经通过运行summaryUNetPP,3,128,128验证了一切都已就绪,运行起来没有问题 def weights_init(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: m.weight.data.normal_(0.0, 0.02)
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
class blockUNetPP(nn.Module):
def __init__(self, in_channels, middle_channels, out_channels):
super().__init__()
self.relu = nn.LeakyReLU(0.2, inplace=True)
self.conv1 = nn.Conv2d(in_channels, middle_channels, 3, padding=1)
self.bn1 = nn.BatchNorm2d(middle_channels)
self.conv2 = nn.Conv2d(middle_channels, out_channels, 3, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
return out
class upConv(nn.Module):
def __init__(self, in_ch, out_ch):
super().__init__()
self.upc = nn.Sequential(
nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
nn.Conv2d(in_ch, out_ch*2, 3, stride=1, padding=1),
nn.BatchNorm2d(out_ch*2),
nn.ReLU(inplace=True)
)
def forward(self, x):
out = self.upc(x)
return out
我的问题是,当我尝试开始培训模型时,我会遇到以下问题:
Traceback (most recent call last):
File "runTrain.py", line 90, in <module>
netG.apply(weights_init)
File "C:\Users\Anaconda3\envs\CFD\lib\site-packages\torch\nn\modules\module.py", line 289, in apply
module.apply(fn)
File "C:\Users\Anaconda3\envs\CFD\lib\site-packages\torch\nn\modules\module.py", line 290, in apply
fn(self)
File "D:\Thesis Models\Deep_learning_models\UNet\train\NetC.py", line 8, in weights_init
m.weight.data.normal_(0.0, 0.02)
File "C:\Users\Anaconda3\envs\CFD\lib\site-packages\torch\nn\modules\module.py", line 594, in __getattr__
type(self).__name__, name))
AttributeError: 'upConv' object has no attribute 'weight'
我已经找到了建议在容器模块上循环的方法,但我已经在用weights_initm做这项工作了。有人能解释一下我当前的设置有什么问题吗?您正在通过检查类名是否包含带有类名的Conv来决定如何初始化权重。查找“Conv”。您的类的名称为upConv,其中包括Conv,因此您尝试初始化其属性.weight,但该属性不存在 重命名类或使条件更严格,例如classname.find'Conv2d'。最严格的方法是检查它是否是nn.Conv2d的实例,而不是查看类的名称 def权重_initm: 如果isinstancem,nn.Conv2d: m、 重量.数据.正常值0.0,0.02 elif IsInstance,nn.2D: m、 重量。数据。正常值_1.0,0.02 m、 bias.data.fill_0
谢谢,现在开始运行。我猜实例是一种更健壮的方法。