aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/bayesflow/python/kernel_tests/csiszar_divergence_test.py
blob: 2e94b7206de4f7c40c89f083f3bfa2a22bb7b917 (plain)
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
1001
1002
1003
1004
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for Csiszar Divergence Ops."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.contrib.bayesflow.python.ops import csiszar_divergence_impl
from tensorflow.contrib.distributions.python.ops import mvn_diag as mvn_diag_lib
from tensorflow.contrib.distributions.python.ops import mvn_full_covariance as mvn_full_lib
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gradients_impl
from tensorflow.python.ops import linalg_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.ops.distributions import kullback_leibler
from tensorflow.python.ops.distributions import normal as normal_lib
from tensorflow.python.platform import test


cd = csiszar_divergence_impl


def tridiag(d, diag_value, offdiag_value):
  """d x d matrix with given value on diag, and one super/sub diag."""
  diag_mat = linalg_ops.eye(d) * (diag_value - offdiag_value)
  three_bands = array_ops.matrix_band_part(
      array_ops.fill([d, d], offdiag_value), 1, 1)
  return diag_mat + three_bands


class AmariAlphaTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    for alpha in [-1., 0., 1., 2.]:
      for normalized in [True, False]:
        with self.test_session(graph=ops.Graph()):
          self.assertAllClose(
              cd.amari_alpha(0., alpha=alpha,
                             self_normalized=normalized).eval(),
              0.)

  def test_correct_when_alpha0(self):
    with self.test_session():
      self.assertAllClose(
          cd.amari_alpha(self._logu, alpha=0.).eval(),
          -self._logu)

      self.assertAllClose(
          cd.amari_alpha(self._logu, alpha=0., self_normalized=True).eval(),
          -self._logu + (self._u - 1.))

  def test_correct_when_alpha1(self):
    with self.test_session():
      self.assertAllClose(
          cd.amari_alpha(self._logu, alpha=1.).eval(),
          self._u * self._logu)

      self.assertAllClose(
          cd.amari_alpha(self._logu, alpha=1., self_normalized=True).eval(),
          self._u * self._logu - (self._u - 1.))

  def test_correct_when_alpha_not_01(self):
    for alpha in [-2, -1., -0.5, 0.5, 2.]:
      with self.test_session(graph=ops.Graph()):
        self.assertAllClose(
            cd.amari_alpha(self._logu,
                           alpha=alpha,
                           self_normalized=False).eval(),
            ((self._u**alpha - 1)) / (alpha * (alpha - 1.)))

        self.assertAllClose(
            cd.amari_alpha(self._logu,
                           alpha=alpha,
                           self_normalized=True).eval(),
            ((self._u**alpha - 1.)
             - alpha * (self._u - 1)) / (alpha * (alpha - 1.)))


class KLReverseTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    for normalized in [True, False]:
      with self.test_session(graph=ops.Graph()):
        self.assertAllClose(
            cd.kl_reverse(0., self_normalized=normalized).eval(),
            0.)

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.kl_reverse(self._logu).eval(),
          -self._logu)

      self.assertAllClose(
          cd.kl_reverse(self._logu, self_normalized=True).eval(),
          -self._logu + (self._u - 1.))


class KLForwardTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    for normalized in [True, False]:
      with self.test_session(graph=ops.Graph()):
        self.assertAllClose(
            cd.kl_forward(0., self_normalized=normalized).eval(),
            0.)

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.kl_forward(self._logu).eval(),
          self._u * self._logu)

      self.assertAllClose(
          cd.kl_forward(self._logu, self_normalized=True).eval(),
          self._u * self._logu - (self._u - 1.))


class JensenShannonTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.jensen_shannon(0.).eval(), np.log(0.25))

  def test_symmetric(self):
    with self.test_session():
      self.assertAllClose(
          cd.jensen_shannon(self._logu).eval(),
          cd.symmetrized_csiszar_function(
              self._logu, cd.jensen_shannon).eval())

      self.assertAllClose(
          cd.jensen_shannon(self._logu, self_normalized=True).eval(),
          cd.symmetrized_csiszar_function(
              self._logu,
              lambda x: cd.jensen_shannon(x, self_normalized=True)).eval())

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.jensen_shannon(self._logu).eval(),
          (self._u * self._logu
           - (1 + self._u) * np.log1p(self._u)))

      self.assertAllClose(
          cd.jensen_shannon(self._logu, self_normalized=True).eval(),
          (self._u * self._logu
           - (1 + self._u) * np.log((1 + self._u) / 2)))


class ArithmeticGeometricMeanTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.arithmetic_geometric(0.).eval(), np.log(4))
      self.assertAllClose(
          cd.arithmetic_geometric(0., self_normalized=True).eval(), 0.)

  def test_symmetric(self):
    with self.test_session():
      self.assertAllClose(
          cd.arithmetic_geometric(self._logu).eval(),
          cd.symmetrized_csiszar_function(
              self._logu, cd.arithmetic_geometric).eval())

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.arithmetic_geometric(self._logu).eval(),
          (1. + self._u) * np.log((1. + self._u) / np.sqrt(self._u)))

      self.assertAllClose(
          cd.arithmetic_geometric(self._logu, self_normalized=True).eval(),
          (1. + self._u) * np.log(0.5 * (1. + self._u) / np.sqrt(self._u)))


class TotalVariationTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.total_variation(0.).eval(), 0.)

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.total_variation(self._logu).eval(),
          0.5 * np.abs(self._u - 1))


class PearsonTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.pearson(0.).eval(), 0.)

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.pearson(self._logu).eval(),
          np.square(self._u - 1))


class SquaredHellingerTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.squared_hellinger(0.).eval(), 0.)

  def test_symmetric(self):
    with self.test_session():
      self.assertAllClose(
          cd.squared_hellinger(self._logu).eval(),
          cd.symmetrized_csiszar_function(
              self._logu, cd.squared_hellinger).eval())

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.squared_hellinger(self._logu).eval(),
          np.square(np.sqrt(self._u) - 1))


class TriangularTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.triangular(0.).eval(), 0.)

  def test_symmetric(self):
    with self.test_session():
      self.assertAllClose(
          cd.triangular(self._logu).eval(),
          cd.symmetrized_csiszar_function(
              self._logu, cd.triangular).eval())

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.triangular(self._logu).eval(),
          np.square(self._u - 1) / (1 + self._u))


class TPowerTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.t_power(0., t=-0.1).eval(), 0.)
      self.assertAllClose(cd.t_power(0., t=0.5).eval(), 0.)
      self.assertAllClose(cd.t_power(0., t=1.1).eval(), 0.)
      self.assertAllClose(
          cd.t_power(0., t=-0.1, self_normalized=True).eval(), 0.)
      self.assertAllClose(
          cd.t_power(0., t=0.5, self_normalized=True).eval(), 0.)
      self.assertAllClose(
          cd.t_power(0., t=1.1, self_normalized=True).eval(), 0.)

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.t_power(self._logu, t=np.float64(-0.1)).eval(),
          self._u ** -0.1 - 1.)
      self.assertAllClose(
          cd.t_power(self._logu, t=np.float64(0.5)).eval(),
          -self._u ** 0.5 + 1.)
      self.assertAllClose(
          cd.t_power(self._logu, t=np.float64(1.1)).eval(),
          self._u ** 1.1 - 1.)

  def test_correct_self_normalized(self):
    with self.test_session():
      self.assertAllClose(
          cd.t_power(self._logu, t=np.float64(-0.1),
                     self_normalized=True).eval(),
          self._u ** -0.1 - 1. + 0.1 * (self._u - 1.))
      self.assertAllClose(
          cd.t_power(self._logu, t=np.float64(0.5),
                     self_normalized=True).eval(),
          -self._u ** 0.5 + 1. + 0.5 * (self._u - 1.))
      self.assertAllClose(
          cd.t_power(self._logu, t=np.float64(1.1),
                     self_normalized=True).eval(),
          self._u ** 1.1 - 1. - 1.1 * (self._u - 1.))


class Log1pAbsTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.log1p_abs(0.).eval(), 0.)

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.log1p_abs(self._logu).eval(),
          self._u**(np.sign(self._u - 1)) - 1)


class JeffreysTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.jeffreys(0.).eval(), 0.)

  def test_symmetric(self):
    with self.test_session():
      self.assertAllClose(
          cd.jeffreys(self._logu).eval(),
          cd.symmetrized_csiszar_function(
              self._logu, cd.jeffreys).eval())

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.jeffreys(self._logu).eval(),
          0.5 * (self._u * self._logu - self._logu))


class ChiSquareTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(cd.chi_square(0.).eval(), 0.)

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.chi_square(self._logu).eval(),
          self._u**2 - 1)


class ModifiedGanTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10, 100)
    self._u = np.exp(self._logu)

  def test_at_zero(self):
    with self.test_session():
      self.assertAllClose(
          cd.modified_gan(0.).eval(), np.log(2))
      self.assertAllClose(
          cd.modified_gan(0., self_normalized=True).eval(), np.log(2))

  def test_correct(self):
    with self.test_session():
      self.assertAllClose(
          cd.modified_gan(self._logu).eval(),
          np.log1p(self._u) - self._logu)

      self.assertAllClose(
          cd.modified_gan(self._logu, self_normalized=True).eval(),
          np.log1p(self._u) - self._logu + 0.5 * (self._u - 1))


class SymmetrizedCsiszarFunctionTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10., 100)
    self._u = np.exp(self._logu)

  def test_jensen_shannon(self):
    with self.test_session():

      # The following functions come from the claim made in the
      # symmetrized_csiszar_function docstring.
      def js1(logu):
        return (-logu
                - (1. + math_ops.exp(logu)) * (
                    nn_ops.softplus(logu)))

      def js2(logu):
        return 2. * (math_ops.exp(logu) * (
            logu - nn_ops.softplus(logu)))

      self.assertAllClose(
          cd.symmetrized_csiszar_function(self._logu, js1).eval(),
          cd.jensen_shannon(self._logu).eval())

      self.assertAllClose(
          cd.symmetrized_csiszar_function(self._logu, js2).eval(),
          cd.jensen_shannon(self._logu).eval())

  def test_jeffreys(self):
    with self.test_session():
      self.assertAllClose(
          cd.symmetrized_csiszar_function(self._logu, cd.kl_reverse).eval(),
          cd.jeffreys(self._logu).eval())

      self.assertAllClose(
          cd.symmetrized_csiszar_function(self._logu, cd.kl_forward).eval(),
          cd.jeffreys(self._logu).eval())


class DualCsiszarFunctionTest(test.TestCase):

  def setUp(self):
    self._logu = np.linspace(-10., 10., 100)
    self._u = np.exp(self._logu)

  def test_kl_forward(self):
    with self.test_session():
      self.assertAllClose(
          cd.dual_csiszar_function(self._logu, cd.kl_forward).eval(),
          cd.kl_reverse(self._logu).eval())

  def test_kl_reverse(self):
    with self.test_session():
      self.assertAllClose(
          cd.dual_csiszar_function(self._logu, cd.kl_reverse).eval(),
          cd.kl_forward(self._logu).eval())


class MonteCarloCsiszarFDivergenceTest(test.TestCase):

  def test_kl_forward(self):
    with self.test_session() as sess:
      q = normal_lib.Normal(
          loc=np.ones(6),
          scale=np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0]))

      p = normal_lib.Normal(loc=q.loc + 0.1, scale=q.scale - 0.2)

      approx_kl = cd.monte_carlo_csiszar_f_divergence(
          f=cd.kl_forward,
          p_log_prob=p.log_prob,
          q=q,
          num_draws=int(1e5),
          seed=1)

      approx_kl_self_normalized = cd.monte_carlo_csiszar_f_divergence(
          f=lambda logu: cd.kl_forward(logu, self_normalized=True),
          p_log_prob=p.log_prob,
          q=q,
          num_draws=int(1e5),
          seed=1)

      exact_kl = kullback_leibler.kl_divergence(p, q)

      [approx_kl_, approx_kl_self_normalized_, exact_kl_] = sess.run([
          approx_kl, approx_kl_self_normalized, exact_kl])

      self.assertAllClose(approx_kl_, exact_kl_,
                          rtol=0.08, atol=0.)

      self.assertAllClose(approx_kl_self_normalized_, exact_kl_,
                          rtol=0.02, atol=0.)

  def test_kl_reverse(self):
    with self.test_session() as sess:

      q = normal_lib.Normal(
          loc=np.ones(6),
          scale=np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0]))

      p = normal_lib.Normal(loc=q.loc + 0.1, scale=q.scale - 0.2)

      approx_kl = cd.monte_carlo_csiszar_f_divergence(
          f=cd.kl_reverse,
          p_log_prob=p.log_prob,
          q=q,
          num_draws=int(1e5),
          seed=1)

      approx_kl_self_normalized = cd.monte_carlo_csiszar_f_divergence(
          f=lambda logu: cd.kl_reverse(logu, self_normalized=True),
          p_log_prob=p.log_prob,
          q=q,
          num_draws=int(1e5),
          seed=1)

      exact_kl = kullback_leibler.kl_divergence(q, p)

      [approx_kl_, approx_kl_self_normalized_, exact_kl_] = sess.run([
          approx_kl, approx_kl_self_normalized, exact_kl])

      self.assertAllClose(approx_kl_, exact_kl_,
                          rtol=0.07, atol=0.)

      self.assertAllClose(approx_kl_self_normalized_, exact_kl_,
                          rtol=0.02, atol=0.)

  def test_kl_reverse_multidim(self):

    with self.test_session() as sess:
      d = 5  # Dimension

      p = mvn_full_lib.MultivariateNormalFullCovariance(
          covariance_matrix=tridiag(d, diag_value=1, offdiag_value=0.5))

      q = mvn_diag_lib.MultivariateNormalDiag(scale_diag=[0.5]*d)

      approx_kl = cd.monte_carlo_csiszar_f_divergence(
          f=cd.kl_reverse,
          p_log_prob=p.log_prob,
          q=q,
          num_draws=int(1e5),
          seed=1)

      approx_kl_self_normalized = cd.monte_carlo_csiszar_f_divergence(
          f=lambda logu: cd.kl_reverse(logu, self_normalized=True),
          p_log_prob=p.log_prob,
          q=q,
          num_draws=int(1e5),
          seed=1)

      exact_kl = kullback_leibler.kl_divergence(q, p)

      [approx_kl_, approx_kl_self_normalized_, exact_kl_] = sess.run([
          approx_kl, approx_kl_self_normalized, exact_kl])

      self.assertAllClose(approx_kl_, exact_kl_,
                          rtol=0.02, atol=0.)

      self.assertAllClose(approx_kl_self_normalized_, exact_kl_,
                          rtol=0.08, atol=0.)

  def test_kl_forward_multidim(self):

    with self.test_session() as sess:
      d = 5  # Dimension

      p = mvn_full_lib.MultivariateNormalFullCovariance(
          covariance_matrix=tridiag(d, diag_value=1, offdiag_value=0.5))

      # Variance is very high when approximating Forward KL, so we make
      # scale_diag larger than in test_kl_reverse_multidim. This ensures q
      # "covers" p and thus Var_q[p/q] is smaller.
      q = mvn_diag_lib.MultivariateNormalDiag(scale_diag=[1.]*d)

      approx_kl = cd.monte_carlo_csiszar_f_divergence(
          f=cd.kl_forward,
          p_log_prob=p.log_prob,
          q=q,
          num_draws=int(1e5),
          seed=1)

      approx_kl_self_normalized = cd.monte_carlo_csiszar_f_divergence(
          f=lambda logu: cd.kl_forward(logu, self_normalized=True),
          p_log_prob=p.log_prob,
          q=q,
          num_draws=int(1e5),
          seed=1)

      exact_kl = kullback_leibler.kl_divergence(p, q)

      [approx_kl_, approx_kl_self_normalized_, exact_kl_] = sess.run([
          approx_kl, approx_kl_self_normalized, exact_kl])

      self.assertAllClose(approx_kl_, exact_kl_,
                          rtol=0.06, atol=0.)

      self.assertAllClose(approx_kl_self_normalized_, exact_kl_,
                          rtol=0.05, atol=0.)

  def test_score_trick(self):

    with self.test_session() as sess:
      d = 5  # Dimension
      num_draws = int(1e5)
      seed = 1

      p = mvn_full_lib.MultivariateNormalFullCovariance(
          covariance_matrix=tridiag(d, diag_value=1, offdiag_value=0.5))

      # Variance is very high when approximating Forward KL, so we make
      # scale_diag larger than in test_kl_reverse_multidim. This ensures q
      # "covers" p and thus Var_q[p/q] is smaller.
      s = array_ops.constant(1.)
      q = mvn_diag_lib.MultivariateNormalDiag(
          scale_diag=array_ops.tile([s], [d]))

      approx_kl = cd.monte_carlo_csiszar_f_divergence(
          f=cd.kl_reverse,
          p_log_prob=p.log_prob,
          q=q,
          num_draws=num_draws,
          seed=seed)

      approx_kl_self_normalized = cd.monte_carlo_csiszar_f_divergence(
          f=lambda logu: cd.kl_reverse(logu, self_normalized=True),
          p_log_prob=p.log_prob,
          q=q,
          num_draws=num_draws,
          seed=seed)

      approx_kl_score_trick = cd.monte_carlo_csiszar_f_divergence(
          f=cd.kl_reverse,
          p_log_prob=p.log_prob,
          q=q,
          num_draws=num_draws,
          use_reparametrization=False,
          seed=seed)

      approx_kl_self_normalized_score_trick = (
          cd.monte_carlo_csiszar_f_divergence(
              f=lambda logu: cd.kl_reverse(logu, self_normalized=True),
              p_log_prob=p.log_prob,
              q=q,
              num_draws=num_draws,
              use_reparametrization=False,
              seed=seed))

      exact_kl = kullback_leibler.kl_divergence(q, p)

      grad_sum = lambda fs: gradients_impl.gradients(fs, s)[0]

      [
          approx_kl_grad_,
          approx_kl_self_normalized_grad_,
          approx_kl_score_trick_grad_,
          approx_kl_self_normalized_score_trick_grad_,
          exact_kl_grad_,
          approx_kl_,
          approx_kl_self_normalized_,
          approx_kl_score_trick_,
          approx_kl_self_normalized_score_trick_,
          exact_kl_,
      ] = sess.run([
          grad_sum(approx_kl),
          grad_sum(approx_kl_self_normalized),
          grad_sum(approx_kl_score_trick),
          grad_sum(approx_kl_self_normalized_score_trick),
          grad_sum(exact_kl),
          approx_kl,
          approx_kl_self_normalized,
          approx_kl_score_trick,
          approx_kl_self_normalized_score_trick,
          exact_kl,
      ])

      # Test average divergence.
      self.assertAllClose(approx_kl_, exact_kl_,
                          rtol=0.02, atol=0.)

      self.assertAllClose(approx_kl_self_normalized_, exact_kl_,
                          rtol=0.08, atol=0.)

      self.assertAllClose(approx_kl_score_trick_, exact_kl_,
                          rtol=0.02, atol=0.)

      self.assertAllClose(approx_kl_self_normalized_score_trick_, exact_kl_,
                          rtol=0.08, atol=0.)

      # Test average gradient-divergence.
      self.assertAllClose(approx_kl_grad_, exact_kl_grad_,
                          rtol=0.007, atol=0.)

      self.assertAllClose(approx_kl_self_normalized_grad_, exact_kl_grad_,
                          rtol=0.011, atol=0.)

      self.assertAllClose(approx_kl_score_trick_grad_, exact_kl_grad_,
                          rtol=0.018, atol=0.)

      self.assertAllClose(
          approx_kl_self_normalized_score_trick_grad_, exact_kl_grad_,
          rtol=0.017, atol=0.)


class CsiszarVIMCOTest(test.TestCase):

  def _csiszar_vimco_helper(self, logu):
    """Numpy implementation of `csiszar_vimco_helper`."""

    # Since this is a naive/intuitive implementation, we compensate by using the
    # highest precision we can.
    logu = np.float128(logu)
    n = logu.shape[0]
    u = np.exp(logu)
    loogeoavg_u = []  # Leave-one-out geometric-average of exp(logu).
    for j in range(n):
      loogeoavg_u.append(np.exp(np.mean(
          [logu[i, ...] for i in range(n) if i != j],
          axis=0)))
    loogeoavg_u = np.array(loogeoavg_u)

    loosum_u = []  # Leave-one-out sum of exp(logu).
    for j in range(n):
      loosum_u.append(np.sum(
          [u[i, ...] for i in range(n) if i != j],
          axis=0))
    loosum_u = np.array(loosum_u)

    # Natural log of the average u except each is swapped-out for its
    # leave-`i`-th-out Geometric average.
    log_sooavg_u = np.log(loosum_u + loogeoavg_u) - np.log(n)

    log_avg_u = np.log(np.mean(u, axis=0))
    return log_avg_u, log_sooavg_u

  def _csiszar_vimco_helper_grad(self, logu, delta):
    """Finite difference approximation of `grad(csiszar_vimco_helper, logu)`."""

    # This code actually estimates the sum of the Jacobiab because that's what
    # TF's `gradients` does.
    np_log_avg_u1, np_log_sooavg_u1 = self._csiszar_vimco_helper(
        logu[..., None] + np.diag([delta]*len(logu)))
    np_log_avg_u, np_log_sooavg_u = self._csiszar_vimco_helper(
        logu[..., None])
    return [
        (np_log_avg_u1 - np_log_avg_u) / delta,
        np.sum(np_log_sooavg_u1 - np_log_sooavg_u, axis=0) / delta,
    ]

  def test_vimco_helper_1(self):
    """Tests that function calculation correctly handles batches."""

    logu = np.linspace(-100., 100., 100).reshape([10, 2, 5])
    with self.test_session() as sess:
      np_log_avg_u, np_log_sooavg_u = self._csiszar_vimco_helper(logu)
      [log_avg_u, log_sooavg_u] = sess.run(cd.csiszar_vimco_helper(logu))
      self.assertAllClose(np_log_avg_u, log_avg_u,
                          rtol=1e-8, atol=0.)
      self.assertAllClose(np_log_sooavg_u, log_sooavg_u,
                          rtol=1e-8, atol=0.)

  def test_vimco_helper_2(self):
    """Tests that function calculation correctly handles overflow."""

    # Using 700 (rather than 1e3) since naive numpy version can't handle higher.
    logu = np.float32([0., 700, -1, 1])
    with self.test_session() as sess:
      np_log_avg_u, np_log_sooavg_u = self._csiszar_vimco_helper(logu)
      [log_avg_u, log_sooavg_u] = sess.run(cd.csiszar_vimco_helper(logu))
      self.assertAllClose(np_log_avg_u, log_avg_u,
                          rtol=1e-6, atol=0.)
      self.assertAllClose(np_log_sooavg_u, log_sooavg_u,
                          rtol=1e-5, atol=0.)

  def test_vimco_helper_3(self):
    """Tests that function calculation correctly handles underlow."""

    logu = np.float32([0., -1000, -1, 1])
    with self.test_session() as sess:
      np_log_avg_u, np_log_sooavg_u = self._csiszar_vimco_helper(logu)
      [log_avg_u, log_sooavg_u] = sess.run(cd.csiszar_vimco_helper(logu))
      self.assertAllClose(np_log_avg_u, log_avg_u,
                          rtol=1e-5, atol=0.)
      self.assertAllClose(np_log_sooavg_u, log_sooavg_u,
                          rtol=1e-4, atol=1e-15)

  def test_vimco_helper_gradient_using_finite_difference_1(self):
    """Tests that gradient calculation correctly handles batches."""

    logu_ = np.linspace(-100., 100., 100).reshape([10, 2, 5])
    with self.test_session() as sess:
      logu = array_ops.constant(logu_)

      grad = lambda flogu: gradients_impl.gradients(flogu, logu)[0]
      log_avg_u, log_sooavg_u = cd.csiszar_vimco_helper(logu)

      [
          grad_log_avg_u,
          grad_log_sooavg_u,
      ] = sess.run([grad(log_avg_u), grad(log_sooavg_u)])

      # We skip checking against finite-difference approximation since it
      # doesn't support batches.

      # Verify claim in docstring.
      self.assertAllClose(
          np.ones_like(grad_log_avg_u.sum(axis=0)),
          grad_log_avg_u.sum(axis=0))
      self.assertAllClose(
          np.ones_like(grad_log_sooavg_u.mean(axis=0)),
          grad_log_sooavg_u.mean(axis=0))

  def test_vimco_helper_gradient_using_finite_difference_2(self):
    """Tests that gradient calculation correctly handles overflow."""

    delta = 1e-3
    logu_ = np.float32([0., 1000, -1, 1])
    with self.test_session() as sess:
      logu = array_ops.constant(logu_)

      [
          np_grad_log_avg_u,
          np_grad_log_sooavg_u,
      ] = self._csiszar_vimco_helper_grad(logu_, delta)

      grad = lambda flogu: gradients_impl.gradients(flogu, logu)[0]
      log_avg_u, log_sooavg_u = cd.csiszar_vimco_helper(logu)

      [
          grad_log_avg_u,
          grad_log_sooavg_u,
      ] = sess.run([grad(log_avg_u), grad(log_sooavg_u)])

      self.assertAllClose(np_grad_log_avg_u, grad_log_avg_u,
                          rtol=delta, atol=0.)
      self.assertAllClose(np_grad_log_sooavg_u, grad_log_sooavg_u,
                          rtol=delta, atol=0.)
      # Verify claim in docstring.
      self.assertAllClose(
          np.ones_like(grad_log_avg_u.sum(axis=0)),
          grad_log_avg_u.sum(axis=0))
      self.assertAllClose(
          np.ones_like(grad_log_sooavg_u.mean(axis=0)),
          grad_log_sooavg_u.mean(axis=0))

  def test_vimco_helper_gradient_using_finite_difference_3(self):
    """Tests that gradient calculation correctly handles underlow."""

    delta = 1e-3
    logu_ = np.float32([0., -1000, -1, 1])
    with self.test_session() as sess:
      logu = array_ops.constant(logu_)

      [
          np_grad_log_avg_u,
          np_grad_log_sooavg_u,
      ] = self._csiszar_vimco_helper_grad(logu_, delta)

      grad = lambda flogu: gradients_impl.gradients(flogu, logu)[0]
      log_avg_u, log_sooavg_u = cd.csiszar_vimco_helper(logu)

      [
          grad_log_avg_u,
          grad_log_sooavg_u,
      ] = sess.run([grad(log_avg_u), grad(log_sooavg_u)])

      self.assertAllClose(np_grad_log_avg_u, grad_log_avg_u,
                          rtol=delta, atol=0.)
      self.assertAllClose(np_grad_log_sooavg_u, grad_log_sooavg_u,
                          rtol=delta, atol=0.)
      # Verify claim in docstring.
      self.assertAllClose(
          np.ones_like(grad_log_avg_u.sum(axis=0)),
          grad_log_avg_u.sum(axis=0))
      self.assertAllClose(
          np.ones_like(grad_log_sooavg_u.mean(axis=0)),
          grad_log_sooavg_u.mean(axis=0))

  def test_vimco_and_gradient(self):

    with self.test_session() as sess:
      dims = 5  # Dimension
      num_draws = int(20)
      num_batch_draws = int(3)
      seed = 1

      f = lambda logu: cd.kl_reverse(logu, self_normalized=False)
      np_f = lambda logu: -logu

      p = mvn_full_lib.MultivariateNormalFullCovariance(
          covariance_matrix=tridiag(dims, diag_value=1, offdiag_value=0.5))

      # Variance is very high when approximating Forward KL, so we make
      # scale_diag larger than in test_kl_reverse_multidim. This ensures q
      # "covers" p and thus Var_q[p/q] is smaller.
      s = array_ops.constant(1.)
      q = mvn_diag_lib.MultivariateNormalDiag(
          scale_diag=array_ops.tile([s], [dims]))

      vimco = cd.csiszar_vimco(
          f=f,
          p_log_prob=p.log_prob,
          q=q,
          num_draws=num_draws,
          num_batch_draws=num_batch_draws,
          seed=seed)

      x = q.sample(sample_shape=[num_draws, num_batch_draws],
                   seed=seed)
      x = array_ops.stop_gradient(x)
      logu = p.log_prob(x) - q.log_prob(x)
      f_log_sum_u = f(cd.csiszar_vimco_helper(logu)[0])

      grad_sum = lambda fs: gradients_impl.gradients(fs, s)[0]

      def jacobian(x):
        # Warning: this function is slow and may not even finish if prod(shape)
        # is larger than, say, 100.
        shape = x.shape.as_list()
        assert all(s is not None for s in shape)
        x = array_ops.reshape(x, shape=[-1])
        r = [grad_sum(x[i]) for i in range(np.prod(shape))]
        return array_ops.reshape(array_ops.stack(r), shape=shape)

      [
          logu_,
          jacobian_logqx_,
          vimco_,
          grad_vimco_,
          f_log_sum_u_,
          grad_mean_f_log_sum_u_,
      ] = sess.run([
          logu,
          jacobian(q.log_prob(x)),
          vimco,
          grad_sum(vimco),
          f_log_sum_u,
          grad_sum(f_log_sum_u) / num_batch_draws,
      ])

      np_log_avg_u, np_log_sooavg_u = self._csiszar_vimco_helper(logu_)

      # Test VIMCO loss is correct.
      self.assertAllClose(np_f(np_log_avg_u).mean(axis=0), vimco_,
                          rtol=1e-5, atol=0.)

      # Test gradient of VIMCO loss is correct.
      #
      # To make this computation we'll inject two gradients from TF:
      # - grad[mean(f(log(sum(p(x)/q(x)))))]
      # - jacobian[log(q(x))].
      #
      # We now justify why using these (and only these) TF values for
      # ground-truth does not undermine the completeness of this test.
      #
      # Regarding `grad_mean_f_log_sum_u_`, note that we validate the
      # correctness of the zero-th order derivative (for each batch member).
      # Since `cd.csiszar_vimco_helper` itself does not manipulate any gradient
      # information, we can safely rely on TF.
      self.assertAllClose(np_f(np_log_avg_u), f_log_sum_u_, rtol=1e-4, atol=0.)
      #
      # Regarding `jacobian_logqx_`, note that testing the gradient of
      # `q.log_prob` is outside the scope of this unit-test thus we may safely
      # use TF to find it.

      # The `mean` is across batches and the `sum` is across iid samples.
      np_grad_vimco = (
          grad_mean_f_log_sum_u_
          + np.mean(
              np.sum(
                  jacobian_logqx_ * (np_f(np_log_avg_u)
                                     - np_f(np_log_sooavg_u)),
                  axis=0),
              axis=0))

      self.assertAllClose(np_grad_vimco, grad_vimco_,
                          rtol=1e-5, atol=0.)


if __name__ == "__main__":
  test.main()