-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweighted.py
More file actions
52 lines (44 loc) · 1.64 KB
/
weighted.py
File metadata and controls
52 lines (44 loc) · 1.64 KB
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
51
from collections import defaultdict
import numpy as np
import TensionFlowFakhir.TensionFlow as tf #from pip install TensionFlowFakhir
from utils import run_model
class LearnedWeighted:
def __init__(self, n, lr=1e2):
self.n = n
self.lr = lr
self.freqs = defaultdict(lambda: [1, 1])
self.context = '0' * n
self.weights = np.ones(n)
self.weights = tf.Neuron(self.weights)
self.loss = None
def get_prob(self):
probs = []
for i in range(self.n):
probs.append(self.freqs[self.context[i:]][0] / sum(self.freqs[self.context[i:]]))
probs = tf.Neuron(np.array(probs))
prob_sum = (probs * (self.weights / self.weights.sum()[0])).sum()
self.loss = prob_sum
return prob_sum.value[0]
def update(self, bit):
if bit == '1':
for i in range(self.n):
self.freqs[self.context[i:]][1] += 1
self.loss = -((1 - self.loss).log2())
else:
for i in range(self.n):
self.freqs[self.context[i:]][0] += 1
self.loss = -(self.loss.log2())
self.context += bit
self.context = self.context[-self.n:]
self.loss.backward()
self.weights.value -= self.weights.grad * self.lr
self.loss.backward_zero_grad()
def reset(self):
self.__init__(self.n, self.lr)
if __name__ == '__main__':
n = 24
lr = 1e3
data = open('files/enwik3', 'rb').read()
weighted_contexts = LearnedWeighted(n, lr)
compressed_size, theoretical_compression = run_model(weighted_contexts, data)
print(compressed_size, theoretical_compression)