energy_adapter.py 6.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
14
# Copyright(C) 2013-2020 Max-Planck-Society
15
16
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
Martin Reinecke's avatar
Martin Reinecke committed
17

18
19
import numpy as np

Philipp Frank's avatar
Philipp Frank committed
20
from .. import random
Martin Reinecke's avatar
Martin Reinecke committed
21
from ..linearization import Linearization
Philipp Arras's avatar
Philipp Arras committed
22
from ..minimization.energy import Energy
Philipp Frank's avatar
Philipp Frank committed
23
24
25
from ..utilities import myassert, allreduce_sum
from ..multi_domain import MultiDomain
from ..sugar import from_random
Martin Reinecke's avatar
Martin Reinecke committed
26
27

class EnergyAdapter(Energy):
Martin Reinecke's avatar
Martin Reinecke committed
28
29
30
31
32
    """Helper class which provides the traditional Nifty Energy interface to
    Nifty operators with a scalar target domain.

    Parameters
    -----------
Philipp Arras's avatar
Philipp Arras committed
33
34
    position: Field or MultiField
        The position where the minimization process is started.
Philipp Arras's avatar
Philipp Arras committed
35
36
37
    op: EnergyOperator
        The expression computing the energy from the input data.
    constants: list of strings
Martin Reinecke's avatar
Martin Reinecke committed
38
39
40
        The component names of the operator's input domain which are assumed
        to be constant during the minimization process.
        If the operator's input domain is not a MultiField, this must be empty.
Philipp Arras's avatar
Philipp Arras committed
41
42
43
        Default: [].
    want_metric: bool
        If True, the class will provide a `metric` property. This should only
Martin Reinecke's avatar
Martin Reinecke committed
44
        be enabled if it is required, because it will most likely consume
Philipp Arras's avatar
Philipp Arras committed
45
        additional resources. Default: False.
46
47
48
49
50
    nanisinf : bool
        If true, nan energies which can happen due to overflows in the forward
        model are interpreted as inf. Thereby, the code does not crash on
        these occaisions but rather the minimizer is told that the position it
        has tried is not sensible.
Martin Reinecke's avatar
Martin Reinecke committed
51
52
    """

53
    def __init__(self, position, op, constants=[], want_metric=False,
54
55
                 nanisinf=False):
        if len(constants) > 0:
56
            cstpos = position.extract_by_keys(constants)
57
            _, op = op.simplify_for_constant_input(cstpos)
58
59
            varkeys = set(op.domain.keys()) - set(constants)
            position = position.extract_by_keys(varkeys)
60
61
        super(EnergyAdapter, self).__init__(position)
        self._op = op
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
62
        self._want_metric = want_metric
63
        lin = Linearization.make_var(position, want_metric)
Martin Reinecke's avatar
Martin Reinecke committed
64
        tmp = self._op(lin)
Martin Reinecke's avatar
stage2    
Martin Reinecke committed
65
        self._val = tmp.val.val[()]
Martin Reinecke's avatar
Martin Reinecke committed
66
67
        self._grad = tmp.gradient
        self._metric = tmp._metric
68
69
70
        self._nanisinf = bool(nanisinf)
        if self._nanisinf and np.isnan(self._val):
            self._val = np.inf
Martin Reinecke's avatar
Martin Reinecke committed
71
72

    def at(self, position):
73
74
        return EnergyAdapter(position, self._op, want_metric=self._want_metric,
                             nanisinf=self._nanisinf)
Martin Reinecke's avatar
Martin Reinecke committed
75
76
77

    @property
    def value(self):
Martin Reinecke's avatar
Martin Reinecke committed
78
        return self._val
Martin Reinecke's avatar
Martin Reinecke committed
79
80
81

    @property
    def gradient(self):
Martin Reinecke's avatar
Martin Reinecke committed
82
        return self._grad
Martin Reinecke's avatar
Martin Reinecke committed
83

Martin Reinecke's avatar
Martin Reinecke committed
84
85
86
87
    @property
    def metric(self):
        return self._metric

Martin Reinecke's avatar
Martin Reinecke committed
88
89
    def apply_metric(self, x):
        return self._metric(x)
Philipp Frank's avatar
Philipp Frank committed
90
91
92


class StochasticEnergyAdapter(Energy):
Philipp Frank's avatar
Philipp Frank committed
93
94
95
96
97
98
99
100
    """A variant of `EnergyAdapter` that provides the energy interface for an
    operator with a scalar target where parts of the imput are averaged
    instead of optmized. Specifically, for the input corresponding to `keys`
    a set of standart normal distributed samples are drawn and each gets
    partially inserted into `bigop`. The results are averaged and represent a
    stochastic average of an energy with the remaining subdomain being the DOFs
    that are considered to be optimization parameters.
    """
Philipp Frank's avatar
Philipp Frank committed
101
    def __init__(self, position, bigop, keys, local_ops, n_samples, comm, nanisinf,
Philipp Frank's avatar
Philipp Frank committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
                 _callingfrommake=False):
        if not _callingfrommake:
            raise NotImplementedError
        super(StochasticEnergyAdapter, self).__init__(position)
        for op in local_ops:
            myassert(position.domain == op.domain)
        self._comm = comm
        self._local_ops = local_ops
        self._n_samples = n_samples
        lin = Linearization.make_var(position)
        v, g = [], []
        for op in self._local_ops:
            tmp = op(lin)
            v.append(tmp.val.val)
            g.append(tmp.gradient)
        self._val = allreduce_sum(v, self._comm)[()]/self._n_samples
        if np.isnan(self._val) and self._nanisinf:
            self._val = np.inf
        self._grad = allreduce_sum(g, self._comm)/self._n_samples

Philipp Frank's avatar
Philipp Frank committed
122
        self._op = bigop
Philipp Frank's avatar
Philipp Frank committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
        self._keys = keys

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

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

    def at(self, position):
        return StochasticEnergyAdapter(position, self._local_ops,
                        self._n_samples, self._comm, self._nanisinf)

    def apply_metric(self, x):
        lin = Linearization.make_var(self.position, want_metric=True)
        res = []
        for op in self._local_ops:
            res.append(op(lin).metric(x))
        return allreduce_sum(res, self._comm)/self._n_samples

    @property
    def metric(self):
Philipp Frank's avatar
Philipp Frank committed
146
        from .kl_energies import _SelfAdjointOperatorWrapper
Philipp Frank's avatar
Philipp Frank committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        return _SelfAdjointOperatorWrapper(self.position.domain,
                                           self.apply_metric)

    def resample_at(self, position):
        return StochasticEnergyAdapter.make(position, self._op, self._keys,
                                             self._n_samples, self._comm)

    @staticmethod
    def make(position, op, keys, n_samples, mirror_samples, nanisinf = False, comm=None):
        """Energy adapter where parts of the model are sampled.
        """
        samdom = {}
        for k in keys:
            if k in position.domain.keys():
                raise ValueError
            if k not in op.domain.keys():
                raise ValueError
            else:
                samdom[k] = op.domain[k]
        samdom = MultiDomain.make(samdom)
        local_ops = []
        sseq = random.spawn_sseq(n_samples)
Philipp Frank's avatar
Philipp Frank committed
169
        from .kl_energies import _get_lo_hi
Philipp Frank's avatar
Philipp Frank committed
170
171
172
173
174
175
176
177
178
179
180
        for i in range(*_get_lo_hi(comm, n_samples)):
            with random.Context(sseq[i]):
                rnd = from_random(samdom)
                _, tmp = op.simplify_for_constant_input(rnd)
                myassert(tmp.domain == position.domain)
                local_ops.append(tmp)
                if mirror_samples:
                    local_ops.append(op.simplify_for_constant_input(-rnd)[1])
        n_samples = 2*n_samples if mirror_samples else n_samples
        return StochasticEnergyAdapter(position, op, keys, local_ops, n_samples,
                                        comm, nanisinf, _callingfrommake=True)