HuffmanEncodingCompression<T>
Models & Types · AiDotNet.ModelCompression
Implements Huffman encoding compression for model weights using variable-length encoding.
Inheritance: ModelCompressionBase<T> → HuffmanEncodingCompression<T>
For Beginners
Huffman encoding is like creating custom abbreviations.
Imagine you’re taking notes in a lecture:
- Words you hear often (like “the”, “and”, “is”) you abbreviate with single letters
- Rare words you write out in full
- This makes your notes much shorter overall
For neural networks:
- Some weight values appear much more frequently than others
- Frequent values get short binary codes (like “01”)
- Rare values get longer codes (like “110101”)
- Since frequent values appear often, using short codes saves a lot of space
The magic is that Huffman encoding is “lossless”:
- You can perfectly reconstruct the original values
- No accuracy is lost (unlike clustering which is lossy)
- It’s often combined with clustering for even better compression
Example:
- Value appearing 1000 times: code “1” (1 bit each = 1000 bits total)
- Value appearing 10 times: code “01001” (5 bits each = 50 bits total)
- Total: 1050 bits instead of possibly much more with fixed-length codes
How It Works
Huffman encoding is a lossless compression technique that assigns shorter codes to more frequent values and longer codes to less frequent values. This is particularly effective when combined with weight clustering, where cluster indices have non-uniform frequency distributions.
Constructors
| Constructor | Summary |
|---|---|
HuffmanEncodingCompression(int precision) |
Initializes a new instance of the HuffmanEncodingCompression class. |
Methods
| Method | Summary |
|---|---|
ValueTuple<Vector<T>, ICompressionMetadata<T>> Compress(Vector<T> weights) |
Compresses weights using Huffman encoding. |
Vector<T> Decompress(Vector<T> compressedWeights, ICompressionMetadata<T> metadata) |
Decompresses weights by decoding the Huffman-encoded bit stream. |
long GetCompressedSize(Vector<T> compressedWeights, ICompressionMetadata<T> metadata) |
Gets the compressed size including the Huffman tree and encoded bits. |