18/31
Part 4 of 6 · A Function of your ownLearn and do
Autograd is off inside backward, so write the formula
Why write the formula at all, when autograd could work it out?
class Peek(torch.autograd.Function):
@staticmethod
def forward(ctx: FunctionCtx, a: torch.Tensor) -> torch.Tensor:
print("forward, grad mode on:", torch.is_grad_enabled())
return a * 2
@staticmethod
def backward(ctx: FunctionCtx, grad_out: torch.Tensor) -> torch.Tensor:
print("backward, grad mode on:", torch.is_grad_enabled())
return grad_out * 2
Peek.apply(torch.ones(3, requires_grad=True)).sum().backward()
Is grad mode, autograd's recording switch, on inside a custom forward, and inside its backward?
Pick one to see what it prints.
Pick an answer above to see the rest
gradgradcheck checks the gradient of your backward itself. Does MatMul pass?
Pick one to see what it prints.
Pick an answer above to see the rest