local_nonlinearity.py 2.09 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 18
# 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-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

Martin Reinecke's avatar
Martin Reinecke committed
19
from __future__ import absolute_import, division, print_function
20

Martin Reinecke's avatar
Martin Reinecke committed
21
from ..compat import *
Philipp Arras's avatar
Fixups  
Philipp Arras committed
22
from ..nonlinearities import Exponential, PositiveTanh, Tanh
Martin Reinecke's avatar
Martin Reinecke committed
23
from ..sugar import makeOp
Philipp Arras's avatar
Philipp Arras committed
24
from .model import Model
25 26


Philipp Arras's avatar
Philipp Arras committed
27
class LocalModel(Model):
Philipp Arras's avatar
Philipp Arras committed
28
    def __init__(self, inp, nonlinearity):
29 30
        """
        Computes nonlinearity(inp)
31
            - LocalModel.value = nonlinearity(value) (pointwise)
32
            - LocalModel.jacobian = Outer Product of Jacobians
33 34 35 36 37 38 39

        Parameters
        ----------
        inp : Model
            The model for which the  nonlinarity will be applied.
        nonlinearity: Function
            The nonlinearity to be applied to model.
40
        """
Philipp Arras's avatar
Philipp Arras committed
41 42
        super(LocalModel, self).__init__(inp.position)
        self._inp = inp
43 44
        self._nonlinearity = nonlinearity
        self._value = nonlinearity(self._inp.value)
45
        d_inner = self._inp.jacobian
Philipp Arras's avatar
Philipp Arras committed
46
        d_outer = makeOp(self._nonlinearity.derivative(self._inp.value))
47
        self._jacobian = d_outer * d_inner
48 49

    def at(self, position):
Philipp Arras's avatar
Philipp Arras committed
50
        return self.__class__(self._inp.at(position), self._nonlinearity)
Philipp Arras's avatar
Philipp Arras committed
51 52 53 54 55 56 57 58 59 60 61 62


def PointwiseExponential(inp):
    return LocalModel(inp, Exponential())


def PointwiseTanh(inp):
    return LocalModel(inp, Tanh())


def PointwisePositiveTanh(inp):
    return LocalModel(inp, PositiveTanh())