Commit b8dbbbfa authored by Martin Reinecke's avatar Martin Reinecke
Browse files

Merge branch 'unit-tests-for-models' into 'NIFTy_5'

Unit tests for models

See merge request ift/nifty-dev!67
parents 50ffbdcc bc538ace
......@@ -34,9 +34,9 @@ def _get_acceptable_model(M):
dir = from_random("normal", M.position.domain)
dirder = M.jacobian(dir)
if dirder.norm() == 0:
dir = dir * val * 1e-5
dir = dir * val.norm() * 1e-5
else:
dir = dir * val * (1e-5/dirder.norm())
dir = dir * val.norm() * (1e-5/dirder.norm())
# Find a step length that leads to a "reasonable" Model
for i in range(50):
try:
......
......@@ -23,6 +23,7 @@ import numpy as np
from ..compat import *
from ..field import Field
from .multi_domain import MultiDomain
from .. import utilities
class MultiField(object):
......@@ -147,6 +148,27 @@ class MultiField(object):
"""
return np.sqrt(np.abs(self.vdot(x=self)))
def sum(self):
""" Computes the sum all field values.
Returns
-------
norm : float
The sum of the field values.
"""
return utilities.my_sum(map(lambda v: v.sum(), self._val))
@property
def size(self):
""" Computes the overall degrees of freedom.
Returns
-------
size : int
The sum of the size of the individual fields
"""
return utilities.my_sum(map(lambda d: d.size, self._domain.domains()))
def squared_norm(self):
""" Computes the square of the L2-norm of the field values.
......
......@@ -72,12 +72,14 @@ class Model_Tests(unittest.TestCase):
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testMul(self, type1, type2, space, seed):
def testBinary(self, type1, type2, space, seed):
model1 = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
model2 = self.make_model(
type2, space_key='s2', space=space, seed=seed+1)['s2']
ift.extra.check_value_gradient_consistency(model1*model2)
ift.extra.check_value_gradient_consistency(model1+model2)
ift.extra.check_value_gradient_consistency(model1*3.)
@expand(product(
['Variable', 'Constant'],
......@@ -92,3 +94,32 @@ class Model_Tests(unittest.TestCase):
lin_op = self.make_linear_operator('ScalingOperator', space=space)
model2 = self.make_model('LinearModel', model=model1, lin_op=lin_op)
ift.extra.check_value_gradient_consistency(model1*model2)
@expand(product(
['Variable', 'Constant'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testLocalModel(self, type, space, seed):
model = self.make_model(
type, space_key='s', space=space, seed=seed)['s']
ift.extra.check_value_gradient_consistency(
ift.PointwiseExponential(model))
ift.extra.check_value_gradient_consistency(ift.PointwiseTanh(model))
ift.extra.check_value_gradient_consistency(
ift.PointwisePositiveTanh(model))
@expand(product(
['Variable', 'Constant'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testMultiModel(self, type, space, seed):
model = self.make_model(
type, space_key='s', space=space, seed=seed)['s']
mmodel = ift.MultiModel(model, 'g')
ift.extra.check_value_gradient_consistency(mmodel)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment