variational_models.py 6.31 KB
Newer Older
Philipp Arras's avatar
Philipp Arras committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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-2021 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

18
import numpy as np
Philipp Arras's avatar
Philipp Arras committed
19

20
21
from ..domain_tuple import DomainTuple
from ..domains.unstructured_domain import UnstructuredDomain
Philipp Arras's avatar
Philipp Arras committed
22
23
24
25
26
27
28
29
30
31
32
33
from ..field import Field
from ..linearization import Linearization
from ..multi_domain import MultiDomain
from ..multi_field import MultiField
from ..operators.einsum import MultiLinearEinsum
from ..operators.energy_operators import EnergyOperator
from ..operators.linear_operator import LinearOperator
from ..operators.multifield2vector import Multifield2Vector
from ..operators.sandwich_operator import SandwichOperator
from ..operators.simple_linear_operators import FieldAdapter, PartialExtractor
from ..sugar import domain_union, from_random, full, makeField

34
35
36

class MeanfieldModel():
    def __init__(self, domain):
Philipp Arras's avatar
Philipp Arras committed
37
38
        self.domain = MultiDomain.make(domain)
        self.Flat = Multifield2Vector(self.domain)
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

        self.std = FieldAdapter(self.Flat.target,'var').absolute()
        self.latent = FieldAdapter(self.Flat.target,'latent')
        self.mean = FieldAdapter(self.Flat.target,'mean')
        self.generator = self.Flat.adjoint(self.mean + self.std * self.latent)
        self.entropy = GaussianEntropy(self.std.target) @ self.std

    def get_initial_pos(self, initial_mean=None,initial_sig = 1):
        initial_pos = from_random(self.generator.domain).to_dict()
        initial_pos['latent'] = full(self.generator.domain['latent'], 0.)
        initial_pos['var'] = full(self.generator.domain['var'], initial_sig)

        if initial_mean is None:
            initial_mean = 0.1*from_random(self.generator.target)

        initial_pos['mean'] = self.Flat(initial_mean)
        return MultiField.from_dict(initial_pos)

Philipp Arras's avatar
Philipp Arras committed
57

58
59
class FullCovarianceModel():
    def __init__(self, domain):
Philipp Arras's avatar
Philipp Arras committed
60
61
        self.domain = MultiDomain.make(domain)
        self.Flat = Multifield2Vector(self.domain)
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
        one_space = UnstructuredDomain(1)
        self.flat_domain = self.Flat.target[0]
        N_tri = self.flat_domain.shape[0]*(self.flat_domain.shape[0]+1)//2
        triangular_space = DomainTuple.make(UnstructuredDomain(N_tri))
        tri = FieldAdapter(triangular_space, 'cov')
        mat_space = DomainTuple.make((self.flat_domain,self.flat_domain))
        lat_mat_space = DomainTuple.make((one_space,self.flat_domain))
        lat = FieldAdapter(lat_mat_space,'latent')
        LT = LowerTriangularProjector(triangular_space,mat_space)
        mean = FieldAdapter(self.flat_domain,'mean')
        cov = LT @ tri
        co = FieldAdapter(cov.target, 'co')

        matmul_setup_dom = domain_union((co.domain,lat.domain))
        co_part = PartialExtractor(matmul_setup_dom, co.domain)
        lat_part = PartialExtractor(matmul_setup_dom, lat.domain)
        matmul_setup = lat_part.adjoint @ lat.adjoint @ lat + co_part.adjoint @ co.adjoint @ cov
        MatMult = MultiLinearEinsum(matmul_setup.target,'ij,ki->jk', key_order=('co','latent'))

        Resp = Respacer(MatMult.target, mean.target)
        self.generator = self.Flat.adjoint @ (mean + Resp @ MatMult @ matmul_setup)
        
        Diag = DiagonalSelector(cov.target, self.Flat.target)
        diag_cov = Diag(cov).absolute()
        self.entropy = GaussianEntropy(diag_cov.target) @ diag_cov

Philipp Arras's avatar
Philipp Arras committed
88
    def get_initial_pos(self, initial_mean=None, initial_sig=1):
89
90
        initial_pos = from_random(self.generator.domain).to_dict()
        initial_pos['latent'] = full(self.generator.domain['latent'], 0.)
Philipp Arras's avatar
Philipp Arras committed
91
        diag_tri = np.diag(np.full(self.flat_domain.shape[0], initial_sig))[np.tril_indices(self.flat_domain.shape[0])]
92
93
94
95
96
97
98
99
100
101
102
103
104
        initial_pos['cov'] = makeField(self.generator.domain['cov'], diag_tri)
        if initial_mean is None:
            initial_mean = 0.1*from_random(self.generator.target)
        initial_pos['mean'] = self.Flat(initial_mean)
        return MultiField.from_dict(initial_pos)


class GaussianEntropy(EnergyOperator):
    def __init__(self, domain):
        self._domain = domain

    def apply(self, x):
        self._check_input(x)
Philipp Arras's avatar
Philipp Arras committed
105
        res = -0.5*(2*np.pi*np.e*x**2).log().sum()
106
107
108
109
        if not isinstance(x, Linearization):
            return Field.scalar(res)
        if not x.want_metric:
            return res
Philipp Arras's avatar
Philipp Arras committed
110
111
        # FIXME not sure about metric
        return res.add_metric(SandwichOperator.make(res.jac))
112
113
114
115


class LowerTriangularProjector(LinearOperator):
    def __init__(self, domain, target):
Philipp Arras's avatar
Philipp Arras committed
116
117
118
        self._domain = DomainTuple.make(domain)
        self._target = DomainTuple.make(target)
        self._indices = np.tril_indices(target.shape[0])
119
120
121
        self._capability = self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
Philipp Arras's avatar
Philipp Arras committed
122
123
        self._check_input(x, mode)
        x = x.val
124
        if mode == self.TIMES:
Philipp Arras's avatar
Philipp Arras committed
125
126
127
128
129
130
            res = np.zeros(self._target.shape)
            res[self._indices] = x
        else:
            res = x[self._indices].reshape(self._domain.shape)
        return makeField(self._tgt(mode), res)

131
132
133

class DiagonalSelector(LinearOperator):
    def __init__(self, domain, target):
Philipp Arras's avatar
Philipp Arras committed
134
135
        self._domain = DomainTuple.make(domain)
        self._target = DomainTuple.make(target)
136
137
138
        self._capability = self.TIMES | self.ADJOINT_TIMES

    def apply(self, x, mode):
Philipp Arras's avatar
Philipp Arras committed
139
140
141
142
143
        self._check_input(x, mode)
        x = np.diag(x.val)
        if mode == self.ADJOINT_TIMES:
            x = x.reshape(self._domain.shape)
        return makeField(self._tgt(mode), x)
144
145
146
147


class Respacer(LinearOperator):
    def __init__(self, domain, target):
Philipp Arras's avatar
Philipp Arras committed
148
149
        self._domain = DomainTuple.make(domain)
        self._target = DomainTuple.make(target)
150
151
        self._capability = self.TIMES | self.ADJOINT_TIMES

Philipp Arras's avatar
Philipp Arras committed
152
153
154
    def apply(self, x, mode):
        self._check_input(x, mode)
        return makeField(self._tgt(mode), x.val.reshape(self._tgt(mode).shape))