-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext_mixing_models.py
50 lines (37 loc) · 1.36 KB
/
context_mixing_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
sys.path.append('..')
from arithmetic_compressor.models import ContextMix_Linear, ContextMix_Logistic
from arithmetic_compressor import AECompressor, util
# 1
def context_mixing_linear_model():
data = [0]*150+[1]*500
N = len(data)
entropy = round(util.h(data))
print('\n==Context Mixing (Linear)==')
print(f"To compress: '{data}' (len={len(data)})")
print(f"Information content(entropy): {entropy}")
model = ContextMix_Linear()
arit_coder = AECompressor(model)
encoded = arit_coder.compress(data)
ratio = (1 - len(encoded)/entropy) * 100
print(f"Compressed: {encoded} (len={len(encoded)})")
print(f"Compression ratio: {ratio}%")
assert arit_coder.decompress(encoded, N) == data
# 2
def context_mixing_logistic_model():
data = [0]*150+[1]*500
N = len(data)
entropy = round(util.h(data))
print('\n==Context Mixing (Neural Network / Logistic)==')
print(f"To compress: '{data}' (len={len(data)})")
print(f"Information content(entropy): {entropy}")
model = ContextMix_Logistic(0.1)
arit_coder = AECompressor(model)
encoded = arit_coder.compress(data)
ratio = (1 - len(encoded)/entropy) * 100
print(f"Compressed: {encoded} (len={len(encoded)})")
print(f"Compression ratio: {ratio}%")
assert arit_coder.decompress(encoded, N) == data
if __name__ == '__main__':
context_mixing_linear_model()
context_mixing_logistic_model()