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:
ReLUFunction(torch.autograd.Function) with forward(ctx, x) and backward(ctx, grad_output) static methods.relu_custom_forward(x): runs ReLUFunction.apply() on the input and returns the output as a nested list.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
Asked at
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.
Already premium?