Intermediate
Scalar quantisation and memory
Write quantise(vec, levels) that maps each number from the range -1 to 1 onto a whole number from 0 to levels: floor((x + 1) / 2 * levels + 0.5) (import math for floor).
Write restore(codes, levels) that maps the codes back to numbers in -1 to 1: code / levels * 2 - 1.
Write memory_mb(n, dim, bits) that returns the megabytes (1 MB = 1,000,000 bytes, integer division) needed for n vectors of dim numbers stored with bits bits per number.
What Check tests
- 8-bit codes
- 1-bit codes keep only the sign
- The extremes restore exactly
- The error is at most half a step
- Memory at three precisions
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.