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 20
from __future__ import absolute_import, division, print_function
from ..compat import *
Philipp Arras's avatar
Fixups  
Philipp Arras committed
21
from ..nonlinearities import Exponential, PositiveTanh, Tanh
Martin Reinecke's avatar
Martin Reinecke committed
22
from ..sugar import makeOp
Philipp Arras's avatar
Philipp Arras committed
23
from .model import Model
24 25


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

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

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


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


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


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