Foundations

ReLU

EasyRaw PyTorchAbout 10 min1 public example plus private tests

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Your task

Replace each negative input entry with zero and keep each positive entry unchanged; zero stays zero. Return the same shape, dtype and device.

Function signature

relu(x: torch.Tensor) -> torch.Tensor

Worked example

Worked example

Negative entries become zero; positive entries are unchanged. The return is [0, 0, 0.5, 2] with shape (4,).

import torch

x = torch.tensor([-2.0, -0.5, 0.5, 2.0], dtype=torch.float64, requires_grad=True)

result = relu(x=x)

Expected return

torch.tensor([0.0, 0.0, 0.5, 2.0], dtype=torch.float64)

Parameters

ParameterShape and meaning
xtorch.TensorAny shape, including empty axes.Values to threshold independently; no axis is reduced.

Return

torch.Tensor · Exactly x.shape.

Return Transformed values.

Required behavior

  • Compute f(x)=\max(x,0) independently at each entry.
  • Preserve all axes, including empty axes; do not reduce or add parameters.
  • At exactly zero, use derivative zero, matching the defined ReLU convention.

Why this matters

A gate controls a separate linear feature branch. Activating the wrong branch produces a different operation even when every shape matches.

Starter code

You write the body of this function in the BioTorch workspace. Your code runs in an isolated PyTorch sandbox; Run checks the public examples and Submit grades the full test suite, including gradients.

import torch


def relu(
    x: torch.Tensor,  # Any shape, including empty axes.
) -> torch.Tensor:
    """Return a tensor with shape x.shape."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement relu")

Ready to implement it?

3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Related exercises

Sources and further reading

A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.