TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

196. PyTorch: Custom Autograd Function

Hard

Implement a custom ReLU activation by subclassing torch.autograd.Function with explicit forward and backward passes — and prove the backward works by computing input gradients through it.

You must provide two functions plus the ReLUFunction class:

  1. ReLUFunction(torch.autograd.Function) with forward(ctx, x) and backward(ctx, grad_output) static methods.
  2. relu_custom_forward(x): runs ReLUFunction.apply() on the input and returns the output as a nested list.
  3. backward_test(x): builds a tensor with requires_grad=True, applies ReLUFunction.apply(), calls .sum().backward(), and returns x.grad.detach().tolist(). The result is the binary mask (1.0 where x > 0, else 0.0).

A driver function relu_run(mode, x) (provided) dispatches to the right one based on mode ∈ {'forward', 'backward'}. The grader calls relu_run(...) per test case.

class ReLUFunction(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x): ...
    @staticmethod
    def backward(ctx, grad_output): ...

Why custom autograd? PyTorch's built-in ops have autograd support, but custom ops (e.g. fused kernels, novel activations) need explicit forward/backward. torch.autograd.Function is the hook — and the only way to verify your backward is correct is to run it through a real .backward() call, which is exactly what the second test case does.

Math

ReLU(x)=max(0,x),∂x∂​ReLU(x)=1[x>0]

Asked at

NumPy

import numpy as np

 

def relu_run(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?