- 在pytorch中想自定义求导函数,通过实现torch.autograd.Function并重写forward和backward函数,来定义自己的自动求导运算。参考官网上的demo:传送门 
- 直接上代码,定义一个ReLu来实现自动求导
import torch   class MyRelu(torch.autograd.Function):     @staticmethod     def forward(ctx, input):                                             ctx.save_for_backward(input)                           return input.clamp(min = 0)      @staticmethod     def backward(ctx, grad_output):                  input, = ctx.saved_tensors                           grad_input = grad_output.clone()                           grad_input[input < 0] = 0         return grad_input 
  - 进行输入数据并测试
dtype = torch.float device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')  generator=torch.Generator(device).manual_seed(42)    N, D_in, H, D_out = 64, 1000, 100, 10  x = torch.randn(N, D_in, device=device, dtype=dtype,generator=generator) y = torch.randn(N, D_out, device=device, dtype=dtype, generator=generator)  w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True, generator=generator) w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True, generator=generator)  learning_rate = 1e-6 for t in range(500):     relu = MyRelu.apply          y_pred = relu(x.mm(w1)).mm(w2) 	     loss = (y_pred - y).pow(2).sum()     if t % 100 == 99:         print(t, loss.item())          loss.backward()     with torch.no_grad():         w1 -= learning_rate * w1.grad         w2 -= learning_rate * w2.grad        	         w1.grad.zero_()         w2.grad.zero_() 
  - 暂时先做这些测试,如有问题,恳请指正