Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Curves replaced by straight lines
The sigmoid and Swish need an exponential for every value, which is slow on small processors and awkward in integer arithmetic. Their “hard” versions replace the curve by straight pieces: hard-sigmoid is 0 below −3, 1 above 3, and a straight ramp in between. Hard-swish multiplies the input by that ramp. They look almost the same as the originals, but cost almost nothing to compute.
Flight computers, microcontrollers and neural accelerators often run 8-bit integer arithmetic with no fast exponential. Hard-sigmoid and hard-swish can be computed exactly with integer adds, clips and a multiply, so a model can be quantised without a lookup table. MobileNetV3, widely used for on-board vision, uses hard-swish for exactly this reason.
- 1. Hard-sigmoid at z = 0 is…
- 2. Hard-swish is…
- 3. The main reason to use hard versions is…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.Definitions by hand
- ReLU6: min(max(z, 0), 6).
- Hard-sigmoid (MobileNetV3, PyTorch): ReLU6(z + 3)/6. Values: −4 ↦ 0, 0 ↦ 0.5, 1.5 ↦ 0.75, 3 ↦ 1.
- Older Keras/Theano form: clip(0.2z + 0.5, 0, 1), which saturates at ±2.5 with slope 0.2 instead of ±3 with slope 1/6.
- Hard-swish: z·ReLU6(z + 3)/6. Values: −4 ↦ 0, −1 ↦ −1/3, 2 ↦ 5/3, 7 ↦ 7.
- Gradient of hard-swish: 0 for z < −3, (2z + 3)/6 for −3 ≤ z ≤ 3, and 1 for z > 3. At 0 the slope is 0.5, like SiLU; at 1.5 it is 1.
- Minimum of hard-swish: at z = −1.5, value −0.375, deeper than SiLU’s −0.278.
- 1. Hard-swish(2) =
- 2. Hard-swish's slope at z = 0 is…
- 3. The Keras hard-sigmoid saturates at…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.How close are they?
Exercise 1. The maximum gaps over [−8, 8]:
| pair | max gap | where |
|---|---|---|
| hard-sigmoid vs sigmoid | 0.069 | z = ±1.32 |
| Keras hard-sigmoid vs sigmoid | 0.076 | |
| hard-swish vs SiLU | 0.142 | z = ±3 |
| ReLU vs SiLU | 0.278 | z = ±1.28 |
Hard-swish is about twice as close to SiLU as ReLU is. The analytic gradient matches finite differences away from the kinks at ±3.
Integer implementation. If activations are stored as integers q with real value q·s and 3/s is an integer, hard-swish is q·clip(q + 3/s, 0, 6/s), followed by a single rescaling. That is only integer addition, clipping and multiplication, and practice problem 5 checks it is exact.
Using them.
- In PyTorch:
nn.Hardsigmoid()andnn.Hardswish(). - MobileNetV3 uses hard-swish only in the deeper layers, where its cost is amortised over fewer, smaller feature maps, and uses hard-sigmoid in its squeeze-and-excitation gates.
nn.Hardswish(); nn.Hardsigmoid()
x * F.relu6(x + 3) / 6- 1. Max gap between hard-swish and SiLU is about…
- 2. Hard-swish in fixed point needs…
- 3. MobileNetV3 uses hard-sigmoid in…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Piecewise-linear approximation. Hard-sigmoid is a linear interpolation of the sigmoid’s range that matches its value at 0 and saturates at ±3. Its slope 1/6 is less than the sigmoid’s 1/4 at the origin, a compromise that reduces the error in the tails. Hard-swish inherits the shape: identity-like for z > 3, zero for z < −3, and a quadratic, (z² + 3z)/6, in between.
Consequences.
- Gradients: hard-sigmoid’s gradient is exactly zero outside [−3, 3], unlike the sigmoid’s small tails, so gates can saturate completely. Hard-swish’s gradient is 0 for z < −3, like ReLU, so units can die there.
- Quantisation: the functions are exactly representable on a fixed-point grid whose step divides 3, so quantising them adds no error beyond the rounding of their inputs. A sigmoid or Swish needs a lookup table or polynomial whose error adds to the quantisation error.
- Speed: on CPUs without vectorised exponentials, and on integer NPUs, hard versions are several times cheaper. On GPUs the difference is usually negligible.
Why swapping can work. A trained network is robust to small changes in its activation function when the change is small compared with the variation already tolerated, for example by noise and quantisation. Hard-swish differs from SiLU by at most 0.14, and much less for most inputs; ReLU differs by up to 0.28 and by a systematic offset for every negative input. Exercise 2 measures the effect.
- 1. Between −3 and 3, hard-swish is…
- 2. Hard-swish's gradient for z < −3 is…
- 3. On a GPU, the speed advantage of hard-swish is usually…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Can you swap activations after training?
History.
- M. Courbariaux, Y. Bengio and J.-P. David (2015) used hard-sigmoid in BinaryConnect.
- Theano and Keras shipped the clip(0.2z + 0.5) form.
- A. Howard et al. (2019) introduced hard-swish in MobileNetV3, found by architecture search alongside the network.
- It is now standard in integer-quantised mobile vision models.
Exercise 2. A 3-hidden-layer network (width 64) trained with SiLU on spirals (radius up to 3, so pre-activations reach the kinks), minibatch SGD, 400 steps. Test accuracy on 2000 fresh points is measured:
- with SiLU;
- after replacing SiLU by hard-swish or by ReLU without retraining;
- after 30 fine-tuning steps with the new activation;
- for a hard-swish network trained from scratch.
| evaluation | test accuracy per seed |
|---|---|
| SiLU (as trained) | 0.995, 0.994, 0.994 |
| hard-swish, swapped in | 0.996, 0.995, 0.995 |
| hard-swish, swapped + 30 steps | 0.997, 0.997, 0.995 |
| ReLU, swapped in | 0.533, 0.516, 0.573 |
| ReLU, swapped + 30 steps | 0.707, 0.909, 0.867 |
| hard-swish from scratch | 0.996, 0.995, 0.994 |
- Hard-swish was a free swap. Accuracy changed by less than 0.002 without retraining, here and in three further seeds.
- ReLU was not. Swapping it in collapsed accuracy to 0.52–0.57 (chance is 0.33) in all six seeds. ReLU discards SiLU’s negative outputs and differs near zero, so every layer’s input distribution shifts, and the errors compound through depth.
- Short fine-tuning helped but did not recover. After 30 steps, ReLU networks reached 0.71–0.91.
- Deployment rule: an approximation that tracks the trained activation’s shape, including its negative part, can be substituted at deployment. A qualitatively different activation needs retraining, or at least validation and fine-tuning.
Hard activations in AI, robotics and aerospace
- On-board vision: MobileNetV3 and its descendants with hard-swish run on drone, satellite and rover processors with int8 accelerators.
- Microcontroller inference: TensorFlow Lite Micro and similar runtimes implement hard-swish with integer kernels.
- Certification and verification: piecewise-linear activations make networks amenable to exact verification by mixed-integer and SMT solvers, unlike sigmoid or Swish.
References
- A. Howard et al. (2019). “Searching for MobileNetV3.” Proc. ICCV.
- M. Courbariaux, Y. Bengio, J.-P. David (2015). “BinaryConnect: training deep neural networks with binary weights during propagations.” Advances in NeurIPS 28.
- B. Jacob et al. (2018). “Quantization and training of neural networks for efficient integer-arithmetic-only inference.” Proc. CVPR.