在 pytorch 中使用 Scipy 函数时获取梯度的问题

问题描述

我已经浏览了这个 pytorch 页面上的示例:https://pytorch.org/tutorials/advanced/numpy_extensions_tutorial.html

在第二个示例中,他们使用 scipy 函数 scipy.signal.convolve2d,我尝试使用 scipy.optimize.quad

这是示例中 pytorch 模块中的前向函数,其中输入是需要 grad 的张量,在使用此函数获取输出后,他们仍然能够计算输入变量的梯度。这就是我正在尝试做的。请注意此处在输入中使用了 detach

class ScipyConv2dFunction(Function):
    @staticmethod
    def forward(ctx,input,filter,bias):
        # detach so we can cast to NumPy
        input,bias = input.detach(),filter.detach(),bias.detach()
        result = correlate2d(input.numpy(),filter.numpy(),mode='valid')
        result += bias.numpy()
        ctx.save_for_backward(input,bias)
        return torch.as_tensor(result,dtype=input.dtype)

示例代码

module = ScipyConv2d(3,3)
print("Filter and bias: ",list(module.parameters()))
input = torch.randn(10,10,requires_grad=True)
output = module(input)
print("Output from the convolution: ",output)
output.backward(torch.randn(8,8))
print("Gradient for the input map: ",input.grad)

所以我觉得我正在尝试做同样的事情,除了使用不同的 scipy 函数 quad 并且我认为它应该可以工作。这是代码

t_list = torch.tensor([0])
U = torch.rand(1,requires_grad=True )

def pytorch_objective(u):
  global U
  return torch.log(U) + torch.as_tensor( integrate.quad(func,t_list[-1].numpy(),u.detach().numpy() )[0] )

initial_x = torch.tensor([4.],requires_grad = True) 
value = pytorch_objective(initial_x)
value.backward()
print(initial_x.grad)

最后打印出None。我可以确认,如果我将目标函数简化为不使用 scipy,我们不会detach 张量而只是进行一些基本计算,梯度确实按预期工作。当然,我认为当我们 detach 变量 u (initial_x) 时,我们似乎会失去它的梯度。

有谁知道我在这里走错路了吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)