如何在 PyTorch 中计算幂函数指数的梯度



我正在尝试计算

out = x.sign()*torch.pow(x.abs(), alpha)

关于阿尔法。

到目前为止,我尝试了以下方法:

class Power(nn.Module):
  def __init__(self, alpha=2.):
    super(Power, self).__init__()
    self.alpha = nn.Parameter(torch.tensor(alpha))
  def forward(self, x):
    return x.sign()*torch.abs(x)**self.alpha

但是这门课一直让我nan我的网络培训。我希望看到类似grad=out*torch.log(x)的东西,但无法到达。例如,此代码不返回任何内容:

alpha_rooting = Power()
x = torch.randn((1), device='cpu', dtype=torch.float)
out = (alpha_rooting(x)).sum()
out.backward()
print(out.grad)

我试图使用autograd也没有运气。我应该如何解决这个问题?谢谢。

您编写的Power()类按预期工作。您实际如何使用它存在问题。渐变存储在该变量的.grad中,而不是上面使用的out变量中。您可以按如下方式更改代码。

alpha_rooting = Power()
x = torch.randn((1), device='cpu', dtype=torch.float)
out = (alpha_rooting(x)).sum()
# compute gradients of all parameters with respect to out (dout/dparam)
out.backward()
# print gradient of alpha
# Note that gradients are store in .grad of parameter not out variable
print(alpha_rooting.alpha.grad)
# compare if it is approximately correct to exact grads
err = (alpha_rooting.alpha.grad - out*torch.log(x))**2 
if (err <1e-8):
    print("Gradients are correct")

最新更新