metric_gaussian_kl.py 6.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2019 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
17
import numpy as np
18
19

from .. import utilities
Philipp Arras's avatar
Philipp Arras committed
20
21
from ..linearization import Linearization
from ..operators.energy_operators import StandardHamiltonian
22
23
from ..probing import approximation2endo
from ..sugar import makeOp
Philipp Arras's avatar
Philipp Arras committed
24
from .energy import Energy
25
26
27


class MetricGaussianKL(Energy):
Martin Reinecke's avatar
Martin Reinecke committed
28
29
30
    """Provides the sampled Kullback-Leibler divergence between a distribution
    and a Metric Gaussian.

Philipp Arras's avatar
Docs    
Philipp Arras committed
31
32
33
34
    A Metric Gaussian is used to approximate another probability distribution.
    It is a Gaussian distribution that uses the Fisher information metric of
    the other distribution at the location of its mean to approximate the
    variance. In order to infer the mean, a stochastic estimate of the
Martin Reinecke's avatar
Martin Reinecke committed
35
    Kullback-Leibler divergence is minimized. This estimate is obtained by
Philipp Arras's avatar
Docs    
Philipp Arras committed
36
37
38
39
40
    sampling the Metric Gaussian at the current mean. During minimization
    these samples are kept constant; only the mean is updated. Due to the
    typically nonlinear structure of the true distribution these samples have
    to be updated eventually by intantiating `MetricGaussianKL` again. For the
    true probability distribution the standard parametrization is assumed.
41
42
43
44

    Parameters
    ----------
    mean : Field
Philipp Arras's avatar
Docs    
Philipp Arras committed
45
        Mean of the Gaussian probability distribution.
Jakob Knollmueller's avatar
Jakob Knollmueller committed
46
    hamiltonian : StandardHamiltonian
Philipp Arras's avatar
Docs    
Philipp Arras committed
47
        Hamiltonian of the approximated probability distribution.
48
    n_samples : integer
Philipp Arras's avatar
Docs    
Philipp Arras committed
49
        Number of samples used to stochastically estimate the KL.
50
    constants : list
Philipp Arras's avatar
Docs    
Philipp Arras committed
51
52
        List of parameter keys that are kept constant during optimization.
        Default is no constants.
53
    point_estimates : list
Philipp Arras's avatar
Docs    
Philipp Arras committed
54
55
56
        List of parameter keys for which no samples are drawn, but that are
        (possibly) optimized for, corresponding to point estimates of these.
        Default is to draw samples for the complete domain.
57
58
    mirror_samples : boolean
        Whether the negative of the drawn samples are also used,
Philipp Arras's avatar
Docs    
Philipp Arras committed
59
        as they are equally legitimate samples. If true, the number of used
60
        samples doubles. Mirroring samples stabilizes the KL estimate as
Philipp Arras's avatar
Docs    
Philipp Arras committed
61
        extreme sample variation is counterbalanced. Default is False.
62
63
64
    napprox : int
        Number of samples for computing preconditioner for sampling. No
        preconditioning is done by default.
Philipp Arras's avatar
Philipp Arras committed
65
66
    _samples : None
        Only a parameter for internal uses. Typically not to be set by users.
67

Philipp Arras's avatar
Docs    
Philipp Arras committed
68
69
70
71
72
73
74
75
    Note
    ----
    The two lists `constants` and `point_estimates` are independent from each
    other. It is possible to sample along domains which are kept constant
    during minimization and vice versa.

    See also
    --------
76
77
    `Metric Gaussian Variational Inference`, Jakob Knollmüller,
    Torsten A. Enßlin, `<https://arxiv.org/abs/1901.11033>`_
78
79
    """

Martin Reinecke's avatar
typo    
Martin Reinecke committed
80
    def __init__(self, mean, hamiltonian, n_samples, constants=[],
Philipp Arras's avatar
Philipp Arras committed
81
                 point_estimates=[], mirror_samples=False,
Philipp Arras's avatar
Philipp Arras committed
82
                 napprox=0, _samples=None, lh_sampling_dtype=np.float64):
83
        super(MetricGaussianKL, self).__init__(mean)
Philipp Arras's avatar
Philipp Arras committed
84
85
86

        if not isinstance(hamiltonian, StandardHamiltonian):
            raise TypeError
87
        if hamiltonian.domain is not mean.domain:
Philipp Arras's avatar
Philipp Arras committed
88
89
90
91
92
93
            raise ValueError
        if not isinstance(n_samples, int):
            raise TypeError
        self._constants = list(constants)
        self._point_estimates = list(point_estimates)
        if not isinstance(mirror_samples, bool):
94
            raise TypeError
Philipp Arras's avatar
Philipp Arras committed
95

96
        self._hamiltonian = hamiltonian
Philipp Arras's avatar
Philipp Arras committed
97

98
99
100
        if _samples is None:
            met = hamiltonian(Linearization.make_partial_var(
                mean, point_estimates, True)).metric
101
102
            if napprox > 1:
                met._approximation = makeOp(approximation2endo(met, napprox))
103
            _samples = tuple(met.draw_sample(from_inverse=True,
Philipp Arras's avatar
Philipp Arras committed
104
                                             dtype=lh_sampling_dtype)
Martin Reinecke's avatar
typo    
Martin Reinecke committed
105
                             for _ in range(n_samples))
106
107
108
109
            if mirror_samples:
                _samples += tuple(-s for s in _samples)
        self._samples = _samples

110
        # FIXME Use simplify for constant input instead
111
112
113
114
115
        self._lin = Linearization.make_partial_var(mean, constants)
        v, g = None, None
        for s in self._samples:
            tmp = self._hamiltonian(self._lin+s)
            if v is None:
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
116
                v = tmp.val.val[()]
117
118
                g = tmp.gradient
            else:
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
119
                v += tmp.val.val[()]
120
121
122
123
                g = g + tmp.gradient
        self._val = v / len(self._samples)
        self._grad = g * (1./len(self._samples))
        self._metric = None
124
        self._napprox = napprox
125
126
127

    def at(self, position):
        return MetricGaussianKL(position, self._hamiltonian, 0,
Philipp Arras's avatar
Philipp Arras committed
128
                                self._constants, self._point_estimates,
129
                                napprox=self._napprox, _samples=self._samples)
130
131
132
133
134
135
136
137
138
139
140
141

    @property
    def value(self):
        return self._val

    @property
    def gradient(self):
        return self._grad

    def _get_metric(self):
        if self._metric is None:
            lin = self._lin.with_want_metric()
Martin Reinecke's avatar
Martin Reinecke committed
142
143
            mymap = map(lambda v: self._hamiltonian(lin+v).metric,
                        self._samples)
144
145
146
147
148
149
            self._unscaled_metric = utilities.my_sum(mymap)
            self._metric = self._unscaled_metric.scale(1./len(self._samples))

    def unscaled_metric(self):
        self._get_metric()
        return self._unscaled_metric, 1/len(self._samples)
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

    def apply_metric(self, x):
        self._get_metric()
        return self._metric(x)

    @property
    def metric(self):
        self._get_metric()
        return self._metric

    @property
    def samples(self):
        return self._samples

    def __repr__(self):
        return 'KL ({} samples):\n'.format(len(
Philipp Arras's avatar
Philipp Arras committed
166
            self._samples)) + utilities.indent(self._hamiltonian.__repr__())