-
Notifications
You must be signed in to change notification settings - Fork 0
/
coders.py
1237 lines (1029 loc) · 56.4 KB
/
coders.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Coders implemented:
* SimpleCoder
* StochasticCoder
* BitBackCoder
* ISBitsBackCoder
* AISBitsBackCoder
* CISBitsBackCoder
* SMCBitsBackCoder
"""
from __future__ import division
from __future__ import print_function
import numpy as np
from . import rans, util
from .rans import RANSStack
from scipy.special import logsumexp
import time
import random
CODER_LIST = [
"SimpleCoder",
"StochasticCoder",
"BitsBackCoder",
"AISBitsBackCoder",
"ISBitsBackCoder",
"CISBitsBackCoder",
"SMCBitsBackCoder",
]
PRINT_FREQ = 10
class Coder(object):
"""The base coder class"""
def __init__(self, stack=None, multidim=True, **kwargs):
"""Initialize the coder.
Args:
stack: the pre-initialized rANS stack
multidim: whether the symbol/latent are vector- or scalar-valued
"""
self.stack = RANSStack(**kwargs) if stack is None else stack # initialize the rANS stack
self.num_encoded = 0 # number of encoded symbols
self.multidim = multidim
if self.multidim:
self.pop = self.pop_fun_multi
self.append = self.append_fun_multi
self.sample = self.sample_fun_multi
else:
self.pop = self.pop_fun
self.append = self.append_fun
self.sample = self.sample_fun
@property
def bit_length(self):
"""Return the **total** bit length of the rANS message"""
return self.stack.bit_length
@property
def net_bit_length(self):
"""Return the **net** bit length of the rANS message"""
return self.stack.net_bit_length
def reset(self):
"""Reset the coder"""
self.num_encoded = 0
self.stack.reset()
def pop_fun(self, stack, count_stat_func):
"""Pop a scalar-valued symbol from the stack
Args:
stack: the rANS stack object
count_stat_func: the statistical function of the distribution for popping the symbol
Returns:
sym: the popped scalar-valued symbol
stack: the rANS stack
"""
stack.update_count_stat_func(count_stat_func, mprec=count_stat_func.precision)
sym = stack.pop()
return sym, stack
def append_fun(self, sym, stack, count_stat_func):
"""Append a scalar-valued symbol to the stack
Args:
sym: the scalar-valued symbol to push
stack: the rANS stack object
count_stat_func: the statistical function of the distribution for pushing the symbol
Returns:
stack: the rANS stack
"""
stack.update_count_stat_func(count_stat_func, mprec=count_stat_func.precision)
stack.append(int(sym))
return stack
def sample_fun(self, count_stat_func):
"""Sample a scalar-valued symbol
Args:
count_stat_func: the statistical function of the distribution for sampling the symbol
Returns:
sym: the sampled scalar-valued symbol
"""
if not self.stack.use_statfunc:
count = count_stat_func.count
dist = np.array(count) / float(sum(count))
sym = np.random.choice(len(dist), p=dist)
else:
cdf, ppf = count_stat_func.stat_func
max_cumcounts = cdf(-1)
# sample an integert in the range [0, 1 << mprec), and get the latent
sym = ppf(np.random.randint(max_cumcounts))
return sym
def get_count_stat_func_multi(self, count_stat_func):
"""Get multi-dim statistical functions as a list"""
if isinstance(count_stat_func, list) and isinstance(count_stat_func[0], util.CountStatFunc):
count_stat_funcs = count_stat_func
else:
assert isinstance(count_stat_func, util.CountStatFuncMulti)
count_stat_funcs = count_stat_func.count_stat_funcs
return count_stat_funcs
def pop_fun_multi(self, stack, count_stat_func):
"""Pop a vector-valued symbol from the stack
Args:
stack: the rANS stack object
count_stat_func: the (multi-dim) statistical function of the distribution for popping the symbol
Returns:
sym: the popped vector-valued symbol
stack: the rANS stack
"""
sym = []
count_stat_funcs = self.get_count_stat_func_multi(count_stat_func)
for f in count_stat_funcs:
s, stack = self.pop_fun(stack, f)
sym.append(s)
sym = np.stack(sym)
return sym, stack
def append_fun_multi(self, sym, stack, count_stat_func):
"""Append a vector-valued symbol to the stack
Args:
sym: the vector-valued symbol to push
stack: the rANS stack object
count_stat_func: the (multi-dim) statistical function of the distribution for pushing the symbol
Returns:
stack: the rANS stack
"""
sym = sym.flatten()
count_stat_funcs = self.get_count_stat_func_multi(count_stat_func)
# GO IN REVERSED ORDER!
for i, s in reversed(list(enumerate(sym))):
f = count_stat_funcs[i]
stack = self.append_fun(s, stack, f)
return stack
def sample_fun_multi(self, count_stat_func):
"""Sample a vector-valued symbol
Args:
count_stat_func: the (multi-dim) statistical function of the distribution for sampling the symbol
Returns:
sym: the sampled vector-valued symbol
"""
sym = []
# TODO: parallelize this computation
count_stat_funcs = self.get_count_stat_func_multi(count_stat_func)
for f in count_stat_funcs:
sym.append(self.sample_fun(f))
sym = np.stack(sym)
return sym
def encode_sym(self, sym):
"""Encoding a single symbol (either scalar- or vector-valued)
This method needs to be implemented by all coders.
"""
raise NotImplementedError
def decode_sym(self):
"""Decoding a single symbol (either scalar- or vector-valued)
This method needs to be implemented by all coders.
"""
raise NotImplementedError
def encode(self, seq, print_progress=False):
"""Encoding a message (a sequence of symbols)"""
if print_progress:
print("..Start encoding")
start_time = time.time()
for i, sym in enumerate(seq):
self.encode_sym(sym)
self.num_encoded += 1
if print_progress and self.num_encoded % PRINT_FREQ == 0:
print("\r..encoded {}/{} symbols, net length: {:.3f} bits/dim, "
"total length {:.3f} bits/dim, encoding speed {:.3f}s/sym"
.format(self.num_encoded, len(seq),
self.net_bit_length / np.prod(sym.shape) / self.num_encoded,
self.bit_length / np.prod(sym.shape) / self.num_encoded,
(time.time() - start_time) / self.num_encoded))
def decode(self, num, print_progress=False):
"""Decoding a message (a sequence of symbols)"""
if print_progress:
print("..Start decoding")
start_time = time.time()
seq = []
num_decoded = 0
while num_decoded < num:
sym = self.decode_sym()
seq.insert(0, sym)
self.num_encoded -= 1
num_decoded += 1
if print_progress and self.num_encoded % PRINT_FREQ == 0:
print("\r..decoded {}/{} symbols, decoding speed {:.3f}s/sym"
.format(num_decoded, num,
(time.time() - start_time) / num_decoded))
return seq
class SimpleCoder(Coder):
"""The simple coder which assumes access to the marginal distribution p(x)"""
def __init__(self, count_stat_func, **kwargs):
super(SimpleCoder, self).__init__(**kwargs)
self.count_stat_func = count_stat_func # the marginal count stat func
def encode_sym(self, sym):
self.stack = self.append(sym, self.stack, self.count_stat_func)
def decode_sym(self):
sym, self.stack = self.pop(self.stack, self.count_stat_func)
return sym
class LatentVariableCoder(Coder):
"""The base latent variable coder"""
def __init__(self, get_prior_count_stat_func=None, get_cond_count_stat_func=None, get_prop_count_stat_func=None,
**kwargs):
"""Initialize the latent variable coder.
Args:
get_prior_count_stat_func: function for getting the stat func of prior dist., signature: None -> p(z)
get_cond_count_stat_func: function for getting the stat func of conditional dist., signature: z -> p(x|z)
get_prop_count_stat_func: function for getting the stat func of proposal dist., signature: x -> q(z|x)
"""
super(LatentVariableCoder, self).__init__(**kwargs)
self.get_prior_count_stat_func = get_prior_count_stat_func
self.get_cond_count_stat_func = get_cond_count_stat_func
self.get_prop_count_stat_func = get_prop_count_stat_func
class StochasticCoder(LatentVariableCoder):
"""The naive stochastic coder without bits-back
Note that this coder suffers from discretization error which increases the net bitrate by -log(precision).
"""
def __init__(self, **kwargs):
super(StochasticCoder, self).__init__(**kwargs)
def encode_sym(self, sym):
prop_count_stat_func = self.get_prop_count_stat_func(sym)
# sample z ~ q(z|x)
latent = self.sample(prop_count_stat_func)
# encode x with p(x|z)
self.stack = self.append(sym, self.stack, self.get_cond_count_stat_func(latent))
# encode z with p(z)
self.stack = self.append(latent, self.stack, self.get_prior_count_stat_func())
def decode_sym(self):
# decode z with p(z)
latent, self.stack = self.pop(self.stack, self.get_prior_count_stat_func())
# decode x with p(x|z)
sym, self.stack = self.pop(self.stack, self.get_cond_count_stat_func(latent))
return sym
class BitsBackCoder(LatentVariableCoder):
"""The bits-back coder (BB-ELBO)"""
def __init__(self, **kwargs):
super(BitsBackCoder, self).__init__(**kwargs)
def encode_sym(self, sym):
prop_count_stat_func = self.get_prop_count_stat_func(sym)
# decode z with q(z|x)
latent, self.stack = self.pop(self.stack, prop_count_stat_func)
# encode x with p(x|z)
cond_count_stat_func = self.get_cond_count_stat_func(latent)
self.stack = self.append(sym, self.stack, cond_count_stat_func)
# encode z with p(z)
prior_count_stat_func = self.get_prior_count_stat_func()
self.stack = self.append(latent, self.stack, prior_count_stat_func)
def decode_sym(self):
# decode z with p(z)
latent, self.stack = self.pop(self.stack, self.get_prior_count_stat_func())
# decode x with p(x|z)
sym, self.stack = self.pop(self.stack, self.get_cond_count_stat_func(latent))
# encode z with q(z|x)
self.stack = self.append(latent, self.stack, self.get_prop_count_stat_func(sym))
return sym
class AISBitsBackCoder(BitsBackCoder):
"""The bits-back coder with annealed importance sampling (BB-AIS)
As the coding scheme of BB-AIS is very similar to Bits-Back for hierarchical models (see
https://arxiv.org/pdf/1905.06845.pdf for details). We also implement the BitSwap version for reducing the
initial bit cost.
"""
def __init__(self, get_joint_count_stat_func,
betas, get_trans_count_stat_func=None, bitswap=False, **kwargs):
""" Initialize the BB-AIS coder.
Args:
get_joint_count_stat_func: function for getting the stat func of joint dist.
betas: the array of length `num of ais steps + 1` specifying the intermediate distributions as
``f_i(z) \propto q(z| x)^(1-betas[i]) * p(x,z)^betas[i]``
get_trans_count_stat_func: function for getting the stat func of transition dist. that leaves the
intermediate dist invariant
bitswap: whether apply the BitSwap trick for reducing the initial bit cost
"""
super(AISBitsBackCoder, self).__init__(**kwargs)
assert betas[0] == 1
assert betas[-1] == 0
self.get_joint_count_stat_func = get_joint_count_stat_func # p(x, z)
self.betas = betas #
self.bitswap = bitswap
if get_trans_count_stat_func is None:
self.get_trans_count_stat_func = self.mh_transition_count_stat_func
else:
self.get_trans_count_stat_func = get_trans_count_stat_func
assert not self.multidim, "BB-AIS is not supported for high-dimension yet!"
assert not self.stack.use_statfunc, "Using stat func for BB-AIS coder is not implemented yet! Please specify " \
"the stat funcs in counts!"
def mh_transition_count_stat_func(self, latent, counts, **kwargs):
"""The Metropolis-Hastings transition kernel with a uniform proposal"""
n = len(counts)
transition_counts = [0] * n
i = latent
for j in range(n):
if i != j:
transition_counts[j] = min(1, counts[j] / counts[i]) / (n - 1)
transition_counts[i] = 1 - sum(transition_counts)
return util.Categorical(self.stack.default_mprec, transition_counts, use_make_sum_to_M=True)
def get_inter_counts(self, sym, i):
"""Get the counts for intermediate distributions"""
b = self.betas[i]
prop_b = np.power(self.get_prop_count_stat_func(sym).prob, 1 - b)
joint_b = np.power(self.get_joint_count_stat_func(x=sym).prob, b)
return prop_b * joint_b
def encode_sym(self, sym):
if self.bitswap:
return self.encode_bitswap(sym)
else:
return self.encode_vanilla(sym)
def decode_sym(self):
if self.bitswap:
return self.decode_bitswap()
else:
return self.decode_vanilla()
def encode_vanilla(self, sym):
n = len(self.betas) - 1
i = n - 1
prop_count_stat_func = self.get_prop_count_stat_func(sym)
# decode z_n-1 ~ p_n(z)
latent, self.stack = self.pop(self.stack, prop_count_stat_func)
latents = [latent]
while i > 0:
inter_counts = self.get_inter_counts(sym, i)
# compute T_i(.|z_i)
mh_count_stat_func = self.get_trans_count_stat_func(latent, inter_counts)
# decode z_i-1 ~ T_i(z | z_i) for i = n-1, ..., 1
latent, self.stack = self.pop(self.stack, mh_count_stat_func)
latents.insert(0, latent)
i -= 1
i = n - 1
while i > 0:
inter_counts = self.get_inter_counts(sym, i)
mh_count_stat_func = self.get_trans_count_stat_func(latents[i - 1], inter_counts,
reversal=True)
# encode z_i with revT_i(z | z_i-1) for i = n-1, ..., 1
latent = latents.pop()
self.stack = self.append(latent, self.stack, mh_count_stat_func)
i -= 1
latent = latents.pop()
# encode x with p(x | z_0)
cond_count_stat_func = self.get_joint_count_stat_func(z=latent)
self.stack = self.append(sym, self.stack, cond_count_stat_func)
# encode z_0 with p(z_0), note f_0(z) = p(z) p(x | z)
latent_count_stat_func = self.get_prior_count_stat_func()
self.stack = self.append(latent, self.stack, latent_count_stat_func)
def decode_vanilla(self):
n = len(self.betas) - 1
# decode z_0 with p(z_0), note f_0(z) = p(z) p(x | z)
latent, self.stack = self.pop(self.stack, self.get_prior_count_stat_func())
# decode x with p(x | z_0)
sym, self.stack = self.pop(self.stack, self.get_joint_count_stat_func(z=latent))
latents = [latent]
i = 1
while i < n:
inter_counts = self.get_inter_counts(sym, i)
mh_count_stat_func = self.get_trans_count_stat_func(latents[i - 1], inter_counts,
reversal=True)
# decode z_i with revT_i(z | z_i-1) for i = n-1, ..., 1
latent, self.stack = self.pop(self.stack, mh_count_stat_func)
latents.append(latent)
i += 1
i = 1
while i < n:
inter_counts = self.get_inter_counts(sym, i)
mh_count_stat_func = self.get_trans_count_stat_func(latents[i], inter_counts)
# encode z_i-1 with T_i(z | z_i) for i = n-1, ..., 1
self.stack = self.append(latents[i - 1], self.stack, mh_count_stat_func)
i += 1
# encode z_n-1 ~ p_n(z)
self.stack = self.append(latents[n - 1],
self.stack,
self.get_prop_count_stat_func(sym))
return sym
def encode_bitswap(self, sym):
n = len(self.betas) - 1
i = n - 1
prop_count_stat_func = self.get_prop_count_stat_func(sym)
# decode z_n-1 ~ p_n(z)
latent, self.stack = self.pop(self.stack, prop_count_stat_func)
while i > 0:
inter_counts = self.get_inter_counts(sym, i)
mh_count_stat_func = self.get_trans_count_stat_func(latent, inter_counts)
# decode z_i-1 ~ T_i(z | z_i) for i = n-1, ..., 1
next_latent, self.stack = self.pop(self.stack, mh_count_stat_func)
# sample z1 given z2 using T2, encode z2 given z1 with revT2
# the bits are clean, if z2 | z1 has distribution revT2 as n->infty
# because f1 gets infinitesimally close to f2, beta1 -> beta2
mh_count_stat_func = self.get_trans_count_stat_func(next_latent, inter_counts,
reversal=True)
# encode z_i with revT_i(z | z_i-1) for i = n-1, ..., 1
self.stack = self.append(latent, self.stack, mh_count_stat_func)
# not clean bits for finite n
i -= 1
latent = next_latent
# encode x with p(x | z_0)
cond_count_stat_func = self.get_joint_count_stat_func(z=latent)
self.stack = self.append(sym, self.stack, cond_count_stat_func)
# encode z_0 with p(z_0), note f_0(z) = p(z) p(x | z)
latent_count_stat_func = self.get_prior_count_stat_func()
self.stack = self.append(latent, self.stack, latent_count_stat_func)
def decode_bitswap(self):
n = len(self.betas) - 1
# decode z_0 with p(z_0), note f_0(z) = p(z) p(x | z)
latent, self.stack = self.pop(self.stack, self.get_prior_count_stat_func())
# decode x with p(x | z_0)
sym, self.stack = self.pop(self.stack, self.get_joint_count_stat_func(z=latent))
i = 1
while i < n:
inter_counts = self.get_inter_counts(sym, i)
mh_count_stat_func = self.get_trans_count_stat_func(latent, inter_counts, reversal=True)
# decode z_i with revT_i(z | z_i-1) for i = n-1, ..., 1
next_latent, self.stack = self.pop(self.stack, mh_count_stat_func)
mh_count_stat_func = self.get_trans_count_stat_func(next_latent, inter_counts)
# encode z_i-1 with T_i(z | z_i) for i = n-1, ..., 1
self.stack = self.append(latent, self.stack, mh_count_stat_func)
latent = next_latent
i += 1
# encode z_n-1 ~ p_n(z)
self.stack = self.append(latent, self.stack, self.get_prop_count_stat_func(sym))
return sym
class ISBitsBackCoder(BitsBackCoder):
"""The bits-back coder with importance sampling (BB-IS)"""
def __init__(self, num_particles, batch_compute=False, **kwargs):
"""Initialize the BB-IS coder.
Args:
num_particles: the number of particles of BB-IS for compression
batch_compute: whether compute the statistical functions of conditional likelihood in a batch over
particles. This might lead to decode check failure since the batched computation results (at encoding time)
may be slightly different from the unbatched ones computed individually (at decoding time) in PyTorch due
to non-determinism.
"""
super(ISBitsBackCoder, self).__init__(**kwargs)
self.num_particles = num_particles
self.batch_compute = batch_compute
# the uniform distribution for encoding the special particle index j
self.iw_uniform_count_stat_func = util.Categorical(self.stack.default_mprec, np.ones(num_particles),
use_make_sum_to_M=True)
def log_importance_weight(self, x, z, cond_count_stat_func, latent_count_stat_func, prop_count_stat_func):
"""Compute the log-importance weight of a single particle"""
p_z = latent_count_stat_func.get_log_prob(z)
q_z = prop_count_stat_func.get_log_prob(z)
p_x = cond_count_stat_func.get_log_prob(x)
return p_x + p_z - q_z
def importance_weights(self, sym, latents, prop_count_stat_func, latent_count_stat_func, cond_count_stat_funcs):
"""Compute the importance weights of particles."""
# TODO: parallelize the computation here over particles
log_w_unnorm = np.array([self.log_importance_weight(sym, latent,
cond_count_stat_func,
latent_count_stat_func,
prop_count_stat_func
) for latent, cond_count_stat_func in
zip(latents, cond_count_stat_funcs)])
# log_sum_exp trick
max_log_w = np.max(log_w_unnorm)
log_w = log_w_unnorm - max_log_w
w = np.exp(log_w)
log_sum_w = logsumexp(log_w_unnorm)
return w, log_sum_w
def encode_sym(self, sym):
# POP STEP
# compute q(z|x) and p(z) which could be reused
prop_count_stat_func = self.get_prop_count_stat_func(sym)
prior_count_stat_func = self.get_prior_count_stat_func()
latents = []
for _ in range(self.num_particles):
# decode z_i with q(z|x)
latent, self.stack = self.pop(self.stack, prop_count_stat_func)
latents.append(latent)
# compute p(x|z_i) for each i where p(x|z_j) could be reused
if self.batch_compute and self.num_particles > 1:
cond_count_stat_funcs = list(self.get_cond_count_stat_func(latents))
else:
cond_count_stat_funcs = [self.get_cond_count_stat_func(latent) for latent in latents]
# decode j with Cat(w)
iw_counts, log_sum_w = self.importance_weights(sym, latents, prop_count_stat_func,
prior_count_stat_func, cond_count_stat_funcs)
iw_count_stat_func = util.Categorical(self.stack.default_mprec, iw_counts, use_make_sum_to_M=True)
j, self.stack = self.pop_fun(self.stack, iw_count_stat_func)
# APPEND STEP
# encode z_k with q(z|x) for all k!=j
for k in range(self.num_particles):
if k != j:
self.stack = self.append(latents[k], self.stack, prop_count_stat_func)
# encode j with Cat(1/num_particles)
self.stack = self.append_fun(j, self.stack, self.iw_uniform_count_stat_func)
# encode x with p(x|z_j)
self.stack = self.append(sym, self.stack, cond_count_stat_funcs[j])
# encode z_j with p(z)
self.stack = self.append(latents[j], self.stack, prior_count_stat_func)
def decode_sym(self):
# POP STEP
# decode z_j with p(z)
prior_count_stat_func = self.get_prior_count_stat_func()
latent_j, self.stack = self.pop(self.stack, prior_count_stat_func)
# compute p(x|z_j) which could be reused
cond_count_stat_func_j = self.get_cond_count_stat_func(latent_j)
# decode x with p(x|z_j)
sym, self.stack = self.pop(self.stack, cond_count_stat_func_j)
# decode j with Cat(1/num_particles)
j, self.stack = self.pop_fun(self.stack, self.iw_uniform_count_stat_func)
# decode z_k with q(z|x) for all k!=j
latents = []
prop_count_stat_func = self.get_prop_count_stat_func(sym) # q(z|x)
for k in range(self.num_particles - 1):
latent, self.stack = self.pop(self.stack, prop_count_stat_func)
latents.insert(0, latent)
latents.insert(j, latent_j)
# compute p(x|z_i) for each i
if self.batch_compute and self.num_particles > 1:
cond_count_stat_funcs = list(self.get_cond_count_stat_func(latents))
else:
cond_count_stat_funcs = [self.get_cond_count_stat_func(latent) for latent in latents]
# APPEND STEP
# encode j with Cat(w)
iw_counts, _ = self.importance_weights(sym, latents, prop_count_stat_func, prior_count_stat_func,
cond_count_stat_funcs)
iw_count_stat_func = util.Categorical(self.stack.default_mprec, iw_counts, use_make_sum_to_M=True)
self.stack = self.append_fun(j, self.stack, iw_count_stat_func)
# encode z_i with q(z|x)
for latent in reversed(latents):
self.stack = self.append(latent, self.stack, prop_count_stat_func)
return sym
unif_count_stat_func_cache = {} # cache the uniform count stat func
class CISBitsBackCoder(ISBitsBackCoder):
"""The bits-back coder with coupled importance sampling (BB-CIS)"""
def __init__(self, bijective_operators, **kwargs):
"""Intialize the BB-CIS coder
Args:
bijective_operators: the bijective operators applying to the single decoded uniform. The number of operators
should equal to the number of particles and each operator should be a tuple (operator, inverse operator).
"""
super(CISBitsBackCoder, self).__init__(**kwargs)
self.operators = bijective_operators
assert isinstance(self.operators, list)
assert len(self.operators) == self.num_particles, \
"The number of operators should equal to the number of particles"
assert all([isinstance(op, tuple) and len(op) == 2 for op in self.operators]), \
"Each operator should be a two-tuple as (operator, inverse operator)"
if self.multidim:
self.get_coupled_uniform_count_stat_func = self._get_coupled_uniform_count_stat_func_multi
else:
self.get_coupled_uniform_count_stat_func = self._get_coupled_uniform_count_stat_func
def _get_coupled_uniform_count_stat_func(self, prop_count_stat_func, z=None):
"""Get the statistical function of the uniform distribution for the scalar-valued coupled uniform
If `z` is None, the uniform distribution is in [0, 2^prop_mprec), otherwise [prop_cdf(z), prop_cdf(z+1)). The
lower bound of the range (i.e., either 0 or prop_cdf(z)) is also returned, which is used as the bias for
encoding the coupled uniform
"""
if z is None: # return a uniform distribution over [1, 2^prop_mprec)
prop_mprec = prop_count_stat_func.precision
if prop_mprec in unif_count_stat_func_cache:
unif_count_stat_func = unif_count_stat_func_cache[prop_mprec]
else:
uniform_count = np.ones(1 << prop_mprec)
unif_count_stat_func = util.Categorical(prop_mprec,
prob=uniform_count / sum(uniform_count),
count_bucket=uniform_count,
cumulative_bucket=np.insert(np.cumsum(uniform_count),
0, 0))
unif_count_stat_func_cache[prop_mprec] = unif_count_stat_func
lower = 0
else: # return a uniform distribution over [cdf(z), cdf(z+1))
lower = prop_count_stat_func.cdf(z)
count_z = prop_count_stat_func.cdf(z + 1) - lower
mprec = int(np.ceil(np.log2(count_z)))
if 2 ** mprec != count_z:
# if count_z is not a power of 2, after discretizd distribution is not uniform
# thus increase mprec to reduce discretization error
mprec += 10
uniform_count = np.ones(count_z)
unif_count_stat_func = util.Categorical(mprec,
prob=uniform_count / sum(uniform_count))
else:
uniform_count = np.ones(1 << mprec)
unif_count_stat_func = util.Categorical(mprec,
prob=uniform_count / sum(uniform_count),
count_bucket=uniform_count,
cumulative_bucket=np.insert(np.cumsum(uniform_count),
0, 0))
unif_count_stat_func_cache[mprec] = unif_count_stat_func
return unif_count_stat_func, lower
def _get_coupled_uniform_count_stat_func_multi(self, prop_count_stat_func, z=None):
"""Get the statistical functions of the uniform distributions for the vector-valued coupled uniform"""
# TODO: this can be parallized
if z is None:
dim = prop_count_stat_func.dim
count_stat_func, lower = self._get_coupled_uniform_count_stat_func(prop_count_stat_func, None)
unif_count_stat_funcs = [count_stat_func] * dim
lowers = [lower] * dim
else:
unif_count_stat_funcs = []
lowers = []
for i, f in enumerate(prop_count_stat_func.count_stat_funcs):
count_stat_func, lower = self._get_coupled_uniform_count_stat_func(f, z[i])
unif_count_stat_funcs.append(count_stat_func)
lowers.append(lower)
return unif_count_stat_funcs, np.stack(lowers)
def get_all_latents(self, prop_count_stat_func, u_j, j):
"""Get all latents from a single uniform"""
# get the underlying base latent by inverse mapping
u_base = self.operators[j][1](u_j, prop_count_stat_func)
assert np.allclose(u_j, self.operators[j][0](u_base, prop_count_stat_func))
us = []
# all the other particles are transformed from the base latent by forward mapping
for k in range(self.num_particles):
if k != j:
us.append(self.operators[k][0](u_base, prop_count_stat_func))
us.insert(j, u_j)
latents = []
# TODO: this can be parallelized
for k in range(self.num_particles):
if self.multidim:
latent = []
for i in range(len(u_j)):
z = prop_count_stat_func.ppf[i](us[k][i])
latent.append(z)
latents.append(np.stack(latent))
else:
latent = prop_count_stat_func.ppf(us[k])
latents.append(latent)
return latents, us
def encode_sym(self, sym):
# POP STEP
# compute q(z|x) and p(z) which could be reused
prop_count_stat_func = self.get_prop_count_stat_func(sym) # q(z|x)
prior_count_stat_func = self.get_prior_count_stat_func()
# decode coupled uniform u with [0, 2^prop_mprec)
u_count_stat_func, _ = self.get_coupled_uniform_count_stat_func(prop_count_stat_func)
u, self.stack = self.pop(self.stack, u_count_stat_func)
latents, us = self.get_all_latents(prop_count_stat_func, u, 0)
# compute p(x|z_i) for each i where p(x| z_j) could be reused
if self.batch_compute and self.num_particles > 1:
cond_count_stat_funcs = list(self.get_cond_count_stat_func(latents))
else:
cond_count_stat_funcs = [self.get_cond_count_stat_func(latent) for latent in latents]
# decode j with Cat(w)
iw_counts, log_sum_w = self.importance_weights(sym, latents, prop_count_stat_func,
prior_count_stat_func, cond_count_stat_funcs)
iw_count_stat_func = util.Categorical(self.stack.default_mprec, iw_counts, use_make_sum_to_M=True)
j, self.stack = self.pop_fun(self.stack, iw_count_stat_func)
# APPEND STEP
# encode u_j with [cdf(z_j), cdf(z_j)+1)
u_j, latent_j = us[j], latents[j]
u_j_count_stat_func, lower_j = self.get_coupled_uniform_count_stat_func(prop_count_stat_func, z=latent_j)
self.stack = self.append(u_j - lower_j, self.stack, u_j_count_stat_func)
# encode j with Cat(1/num_particles)
self.stack = self.append_fun(j, self.stack, self.iw_uniform_count_stat_func)
# encode x with p(x|z_j)
self.stack = self.append(sym, self.stack, cond_count_stat_funcs[j])
# encode z_j with p(z)
self.stack = self.append(latent_j, self.stack, prior_count_stat_func)
def decode_sym(self):
# POP STEP
# decode z_j with p(z)
prior_count_stat_func = self.get_prior_count_stat_func()
latent_j, self.stack = self.pop(self.stack, prior_count_stat_func)
# compute p(x|z_j) which could be reused
cond_count_stat_func_j = self.get_cond_count_stat_func(latent_j)
# decode x with p(x|z_j)
sym, self.stack = self.pop(self.stack, cond_count_stat_func_j)
# decode j with Cat(1/num_particles)
j, self.stack = self.pop_fun(self.stack, self.iw_uniform_count_stat_func)
prop_count_stat_func = self.get_prop_count_stat_func(sym) # q(z|x)
# decode u_j with [cdf(z_j), cdf(z_j)+1)
u_j_count_stat_func, lower_j = self.get_coupled_uniform_count_stat_func(prop_count_stat_func, z=latent_j)
u_j, self.stack = self.pop(self.stack, u_j_count_stat_func)
u_j += lower_j
latents, us = self.get_all_latents(prop_count_stat_func, u_j, j)
# compute p(x|z_i) for each i
if self.batch_compute and self.num_particles > 1:
cond_count_stat_funcs = list(self.get_cond_count_stat_func(latents))
else:
cond_count_stat_funcs = [self.get_cond_count_stat_func(latent) for latent in latents]
# APPEND STEP
# encode j with Cat(w)
iw_counts, _ = self.importance_weights(sym, latents, prop_count_stat_func, prior_count_stat_func,
cond_count_stat_funcs)
iw_count_stat_func = util.Categorical(self.stack.default_mprec, iw_counts, use_make_sum_to_M=True)
self.stack = self.append_fun(j, self.stack, iw_count_stat_func)
# encode coupled uniform u with [0, 2^prop_mprec)
u = us[0]
u_count_stat_func, _ = self.get_coupled_uniform_count_stat_func(prop_count_stat_func)
self.stack = self.append(u, self.stack, u_count_stat_func)
return sym
class SMCBitsBackCoder(ISBitsBackCoder):
"""The bits-back coder with sequential Monte Carlo (BB-SMC)"""
def __init__(self, num_particles, get_trans_count_stat_func, default_symlen, resample=True, adaptive=False,
resample_crit=None, init_state=None, update_state=None, **kwargs):
"""Initialize the BB-SMC coder.
Args:
num_particles: the number of particles of the SMC coder
get_trans_count_stat_func: function for getting the stat func of transition dist., signature: z_1:n-1, state -> p(z_n|...)
default_symlen: the default length of a single symbol (actualluy a sequence of symbols), can be overrided in
encoding/decoding stages
resample: whether apply resampling for compression. If False, it reduces to BB-IS
adaptive: whether apply adaptive resampling
resample_crit: the criterion for adaptive resampling
init_state: the function for initializing the state (e.g., used with VRNN)
update_state: the function for updating the state (e.g., used with VRNN)
BB-SMC is also amenable for the coupling technique for reducing the initial bit cost, see detailed discussion
in our paper.
"""
super(SMCBitsBackCoder, self).__init__(num_particles, **kwargs)
self.get_trans_count_stat_func = get_trans_count_stat_func
assert self.get_prior_count_stat_func is None, "Please use `get_trans_count_stat_func` instead for SMC!"
self.default_symlen = default_symlen
self.resample = resample
self.adaptive = adaptive
self.resample_crit = resample_crit
if init_state is None: # if we do not use a VRNN model
self.init_state = lambda: None
else:
self.init_state = init_state
if update_state is None: # if we do not use a VRNN model
self.update_state = lambda cur_state=None, x_n=None, z_n=None: None
else:
self.update_state = update_state
if self.adaptive:
assert self.resample
if self.resample_crit is None:
def ess_crit(w):
return (1.0 / np.sum(w ** 2)) < (len(w) / 2)
self.resample_crit = ess_crit
def importance_weights(self, sym, latents, prop_count_stat_funcs, latent_count_stat_funcs, cond_count_stat_funcs,
weights=None):
"""Compute the incremental importance weights of particles."""
log_w_unnorm = np.array([self.log_importance_weight(sym, latent,
cond_count_stat_func,
latent_count_stat_func,
prop_count_stat_func
) for
latent, cond_count_stat_func, latent_count_stat_func, prop_count_stat_func in
zip(latents, cond_count_stat_funcs, latent_count_stat_funcs, prop_count_stat_funcs)])
# log_sum_exp trick
max_log_w = np.max(log_w_unnorm)
log_w = log_w_unnorm - max_log_w
w = np.exp(log_w) # incremental importance weights
if weights is None:
weights = np.ones((self.num_particles,), np.float) / self.num_particles
log_sum_w = logsumexp(log_w_unnorm, b=weights) # log sum of accumulative weights
return w, log_sum_w
def encode(self, seq, print_progress=False):
"""Encoding a message (a sequence of `symbols`) and each `symbol` is a sequence of symbols"""
if print_progress:
print("..Start encoding")
start_time = time.time()
total_encoded_symlens = 0
total_symlens = sum([len(sym) for sym in seq])
# For SMC, a single symbol corresponds to a sequence (x_1, x_2, ..., x_length)
for t, sym in enumerate(seq):
sym_length = len(sym)
# To enable both single- or multi- dimensional latents, here we set the array `z` and `z_prev` to be type
# object, it could be either `int` (scalar) or `np.ndarray` (multidim).
# TODO: there should be some better ways to implement this
z = np.empty((self.num_particles, sym_length),
dtype=object) # simulated particles, total size will be N x T
z_prev = np.empty((self.num_particles, sym_length),
dtype=object) # ancestral lineage of each particle, total size will be N x T
z_states = np.empty((self.num_particles,), dtype=object) # current particle states, `None`s if not needed
A = np.zeros((self.num_particles, sym_length - 1),
dtype=np.int) # resampling parent indices, total size will be N x (T - 1)
# We store the statistical functions of all distributions computed in the POP step which will be reused
# in the APPEND step
q_count_stat_funcs = np.empty((self.num_particles, sym_length), dtype=object) # proposal stat funcs
f_count_stat_funcs = np.empty((self.num_particles, sym_length), dtype=object) # prior/transition stat funcs
g_count_stat_funcs = np.empty((self.num_particles, sym_length), dtype=object) # cond likelihood stat funcs
iw_count_stat_funcs = np.empty((sym_length - 1,), dtype=object) # categorical stat funcs
# cumulative importance weights
w_cum = np.ones((self.num_particles,), np.float) / self.num_particles
# resample decisions used for adaptive resampling
resample_decision = np.zeros((sym_length - 1,), dtype=bool)
# POP STEP
for n in range(0, sym_length):
if n != 0:
do_resample = self.resample and (
(not self.adaptive) or (self.adaptive and self.resample_crit(w_cum)))
if do_resample:
# decode A_n-1 ~ Cat(w_n-1) for each particle
A_n = []
iw_count_stat_func = util.Categorical(self.stack.default_mprec, w_cum, use_make_sum_to_M=True)
for i in range(self.num_particles):
parent, self.stack = self.pop_fun(self.stack, iw_count_stat_func)
A_n.append(parent)
A_n = np.array(A_n)
z_prev = z_prev[A_n] # re-assign the ancestral particles
z_states = z_states[A_n] # re-assign the particle states
A[:, n - 1] = A_n
w_cum = np.ones((self.num_particles,), np.float) / self.num_particles # reset weights
resample_decision[n - 1] = True
iw_count_stat_funcs[n - 1] = iw_count_stat_func
else:
# no resampling, simply inherit from itself for each particle
A_n = np.arange(self.num_particles)
A[:, n - 1] = A_n
resample_decision[n - 1] = False
x_n = sym[n]
z_n = []
q_count_stat_funcs_n = []
f_count_stat_funcs_n = []
g_count_stat_funcs_n = []
for i in range(self.num_particles):
# compute q(z_n | x_n, z_1:n-1^i), p(z_n | z_1:n-1^i) for each i which could be reused
if n == 0:
# get prior dist
cur_state = self.init_state()
latent_count_stat_func = self.get_trans_count_stat_func(None,
cur_state) # p(z_0^i)
else:
# get transition dist
cur_state = z_states[i]
latent_count_stat_func = self.get_trans_count_stat_func(z_prev[i, n - 1],
cur_state) # p(z_n^i | z_1:n-1^i)
prop_count_stat_func = self.get_prop_count_stat_func(x_n, cur_state) # q(z_n^i | x_n, z_1:n-1^i)
# decode z_n^i ~ q(z_n | x_n, z_1:n-1^i) i.i.d. for each particle
latent, self.stack = self.pop(self.stack, prop_count_stat_func)
# compute p(x_n | z_n^i) for each i which could be reused
cond_count_stat_func = self.get_cond_count_stat_func(latent, cur_state)
next_state = self.update_state(cur_state, x_n, latent)
z_states[i] = next_state
z_n.append(latent)
q_count_stat_funcs_n.append(prop_count_stat_func)
f_count_stat_funcs_n.append(latent_count_stat_func)