Implement SiLU (also known as Swish) in PyTorch using primitive tensor ops only.
Signature: def silu(x: torch.Tensor) -> torch.Tensor
The rule: you may NOT call F.silu, nn.SiLU, torch.sigmoid, or F.sigmoid. Implement sigmoid yourself inline. We verify your output matches F.silu(x).
Allowed primitives: .exp(), basic arithmetic.
Formula:
silu(x) = x * sigmoid(x) = x / (1 + exp(-x))
PyTorch idioms vs the NumPy version:
np.exp(-x) directly without overflow handling, relying on float64. In float32 you need to be careful: (-x).exp() for large negative x (very negative input) would overflow. For the test inputs here, the simple x / (1 + (-x).exp()) form is fine; for production you'd use the same where-based stable pattern as the sigmoid problem.Math
Related problems
Asked at
import numpy as np
def silu(...):
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?