【问题标题】:Pytorch: "Model Weights not Changing"Pytorch:“模型权重不变”
【发布时间】:2019-03-07 00:54:33
【问题描述】:

谁能帮我理解为什么权重没有更新?

    unet = Unet()
    optimizer = torch.optim.Adam(unet.parameters(), lr=0.001)
    loss_fn = torch.nn.MSELoss()
    input =  Variable(torch.randn(32, 1, 64, 64, 64 ), requires_grad=True)
    target = Variable(torch.randn(32, 1, 64, 64, 64), requires_grad=False)

    optimizer.zero_grad()
    y_pred = unet(input)
    y = target[: , : , 20:44, 20:44, 20:44]

    loss = loss_fn(y_pred, y)
    print(unet.conv1.weight.data[0][0]) # weights of the first layer in the unet
    loss.backward()
    optimizer.step()
    print(unet.conv1.weight.data[0][0]) # weights havent changed

模型定义如下:

class Unet(nn.Module):

def __init__(self):
  super(Unet, self).__init__()

  # Down hill1
  self.conv1 = nn.Conv3d(1, 2, kernel_size=3,  stride=1)
  self.conv2 = nn.Conv3d(2, 2, kernel_size=3,  stride=1)

  # Down hill2
  self.conv3 = nn.Conv3d(2, 4, kernel_size=3,  stride=1)
  self.conv4 = nn.Conv3d(4, 4, kernel_size=3,  stride=1)

  #bottom
  self.convbottom1 = nn.Conv3d(4, 8, kernel_size=3,  stride=1)
  self.convbottom2 = nn.Conv3d(8, 8, kernel_size=3,  stride=1)

  #up hill1
  self.upConv0 = nn.Conv3d(8, 4, kernel_size=3,  stride=1)
  self.upConv1 = nn.Conv3d(4, 4, kernel_size=3,  stride=1)
  self.upConv2 = nn.Conv3d(4, 2, kernel_size=3,  stride=1)

  #up hill2
  self.upConv3 = nn.Conv3d(2, 2, kernel_size=3, stride=1)
  self.upConv4 = nn.Conv3d(2, 1, kernel_size=1, stride=1)

  self.mp = nn.MaxPool3d(kernel_size=3, stride=2, padding=1)
  # some more irrelevant properties...

前向函数如下:

def forward(self, input):
    # Use U-net Theory to Update the filters.
    # Example Approach...
    input = F.relu(self.conv1(input))
    input = F.relu(self.conv2(input))

    input = self.mp(input)

    input = F.relu(self.conv3(input))
    input = F.relu(self.conv4(input))

    input = self.mp(input)

    input = F.relu(self.convbottom1(input))
    input = F.relu(self.convbottom2(input))

    input = F.interpolate(input, scale_factor=2, mode='trilinear')

    input = F.relu(self.upConv0(input))
    input = F.relu(self.upConv1(input))

    input = F.interpolate(input, scale_factor=2, mode='trilinear')


    input = F.relu(self.upConv2(input))
    input = F.relu(self.upConv3(input))

    input = F.relu(self.upConv4(input))

    return input

我遵循了我能找到的任何示例和文档的方法,但我无法理解为什么这不起作用?

我可以弄清楚,在向后调用之后y_pred.grad 是不应该的。如果我们没有梯度,那么优化器当然不能在任何方向上改变权重,但为什么没有梯度呢?

【问题讨论】:

    标签: python machine-learning pytorch


    【解决方案1】:

    我将这个问题确定为“垂死的 ReLu 问题”,因为数据是 Hounsfield 单位,并且 Pytorch 初始权重的均匀分布意味着许多神经元将从 ReLu 的零区域开始,从而使它们瘫痪并依赖于其他神经元产生一个梯度,可以将它们拉出零区域。随着训练的进行,所有神经元都被推入 ReLu 的零区域,这不太可能发生。

    这个问题有几种解决方案。您可以使用 Leaky_relu 或其他没有零区域的激活函数。

    您还可以使用批量归一化对输入数据进行归一化,并将权重初始化为仅属于正类。

    第二个解决方案可能是最优化的解决方案,因为两者都可以解决问题,但leaky_relu 会延长训练时间,而 Batch 归一化会起到相反的作用并提高准确性。另一方面,Leaky_relu 是一个简单的解决方案,而另一个解决方案需要一些额外的工作。

    对于 Hounsfield 数据,还可以在输入中添加常数 1000,从而消除数据中的负单位。这仍然需要与 Pytorch 的标准初始化不同的权重初始化。

    【讨论】:

    • 你能展示一些你解决这个问题的代码吗?
    【解决方案2】:

    我认为不应该使用您使用的命令打印权重。试试print(unet.conv1.state_dict()["weight"]) 而不是print(unet.conv1.weight.data[0][0])

    【讨论】:

    • 确实打印出不同的结果。你知道unet.conv1.state_dict()["weight"] 和unet.conv1.weight 的区别是什么吗?
    • 其实我觉得他们是一样的。我只是注意到,并非每次权重都发生变化时,两者都会发生变化。像50/50的时间。现在我必须考虑是否应该发生。我还检查了两个打印语句是否产生相同的结果。
    猜你喜欢
    • 1970-01-01
    • 2021-06-13
    • 2021-03-28
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 2022-10-07
    • 2021-09-05
    相关资源
    最近更新 更多