Advanced
Quantise to int8 and back
Write round_half_away(x) that rounds to the nearest whole number with exact halves going away from zero (2.5 gives 3, -2.5 gives -3). Python's built-in round rounds halves to even, so do not use it.
Write quantize(ws) returning (qs, scale): scale is the largest absolute weight divided by 127 (use 1.0 when every weight is 0), and each q is round_half_away(w / scale) limited to the range -127 to 127.
Write dequantize(qs, scale) returning each q * scale.
What Check tests
- Halves round away from zero
- The scale is the largest absolute weight divided by 127
- The weights become whole numbers on the grid
- Dequantising gives back values within half a step
- All-zero weights do not crash
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.