Foundations

SiLU

EasyRaw PyTorchAbout 10 min1 public example plus private tests

ESM-CESM3

Write and run your solution Sign in

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

Your task

Multiply each input entry by its sigmoid gain, 1/(1+exp(-x)). Positive entries keep more of their magnitude and negative entries are smoothly attenuated; preserve every input axis.

Function signature

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

Worked example

Follow one activation

For x=[-1,0,1], sigmoid gains are approximately [0.268941,0.5,0.731059], so SiLU returns approximately [-0.268941,0,0.731059]. The gain is computed from the same entry that it multiplies.

Parameters

ParameterShape and meaning
xtorch.TensorAny shape, including empty axes.Values to activate independently at each position.

Return

torch.Tensor · Exactly x.shape.

Return Transformed values.

Required behavior

  • f(x)=x/(1+e^{-x}). Evaluate the sigmoid stably at large positive and negative values.
  • All axes, including empty axes, are preserved. Do not introduce learned parameters or a reduction.
  • Use CPU float32/float64 tensors with one shared floating dtype/device. Accept noncontiguous input layouts and do not mutate supplied tensors.
  • Use raw tensor operations, not a ready-made implementation of the requested primitive. Supplied parameters and random variates must not be reinitialized.

Why this matters

SiLU smoothly scales each value by a sigmoid gate. Unlike ReLU, it preserves a small negative output for negative inputs.

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 silu(
    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 silu")

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.