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

enable more tests

parent 1e157696
......@@ -38,8 +38,8 @@ from .operators.power_distributor import PowerDistributor
__all__ = ['PS_field', 'power_analyze', 'create_power_operator',
'create_harmonic_smoothing_operator', 'from_random',
'full', 'from_global_data', 'from_local_data',
'makeDomain', 'sqrt', 'exp', 'log', 'tanh', 'conjugate',
'get_signal_variance', 'makeOp', 'domain_union']
'makeDomain', 'sqrt', 'exp', 'log', 'tanh', 'positive_tanh',
'conjugate', 'get_signal_variance', 'makeOp', 'domain_union']
def PS_field(pspace, func):
......@@ -257,7 +257,7 @@ def domain_union(domains):
_current_module = sys.modules[__name__]
for f in ["sqrt", "exp", "log", "tanh", "conjugate"]:
for f in ["sqrt", "exp", "log", "tanh", "positive_tanh", "conjugate"]:
def func(f):
def func2(x):
from .linearization import Linearization
......
......@@ -25,100 +25,81 @@ import numpy as np
class Energy_Tests(unittest.TestCase):
def make_model(self, type, **kwargs):
if type == 'Constant':
np.random.seed(kwargs['seed'])
S = ift.ScalingOperator(1., kwargs['space'])
s = S.draw_sample()
return ift.Constant(
ift.MultiField.from_dict({kwargs['space_key']: s}),
ift.MultiField.from_dict({kwargs['space_key']: s}))
elif type == 'Variable':
np.random.seed(kwargs['seed'])
S = ift.ScalingOperator(1., kwargs['space'])
s = S.draw_sample()
return ift.Variable(
ift.MultiField.from_dict({kwargs['space_key']: s}))
elif type == 'LinearModel':
return ift.LinearModel(
inp=kwargs['model'], lin_op=kwargs['lin_op'])
else:
raise ValueError('unknown type passed')
def make_model(self, **kwargs):
np.random.seed(kwargs['seed'])
S = ift.ScalingOperator(1., kwargs['space'])
s = S.draw_sample()
return ift.MultiField.from_dict({kwargs['space_key']: s})
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testGaussian(self, type1, space, seed):
def testGaussian(self, space, seed):
model = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
energy = ift.GaussianEnergy(model)
ift.extra.check_value_gradient_consistency(energy)
space_key='s1', space=space, seed=seed)['s1']
energy = ift.GaussianEnergy()
ift.extra.check_value_gradient_consistency(energy, model)
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testQuadratic(self, type1, space, seed):
np.random.seed(seed)
S = ift.ScalingOperator(1., space)
s = [S.draw_sample() for _ in range(3)]
energy = ift.QuadraticEnergy(s[0], ift.makeOp(s[1]), s[2])
ift.extra.check_value_gradient_consistency(energy)
# @expand(product(
# [ift.GLSpace(15),
# ift.RGSpace(64, distances=.789),
# ift.RGSpace([32, 32], distances=.789)],
# [4, 78, 23]
# ))
# def testQuadratic(self, type1, space, seed):
# np.random.seed(seed)
# S = ift.ScalingOperator(1., space)
# s = [S.draw_sample() for _ in range(3)]
# energy = ift.QuadraticEnergy(s[0], ift.makeOp(s[1]), s[2])
# ift.extra.check_value_gradient_consistency(energy)
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testPoissonian(self, type1, space, seed):
def testPoissonian(self, space, seed):
model = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
model = ift.PointwiseExponential(model)
space_key='s1', space=space, seed=seed)['s1']
model = ift.exp(model)
d = np.random.poisson(120, size=space.shape)
d = ift.Field.from_global_data(space, d)
energy = ift.PoissonianEnergy(model, d)
ift.extra.check_value_gradient_consistency(energy)
energy = ift.PoissonianEnergy(ift.exp, d)
ift.extra.check_value_gradient_consistency(energy, model, tol=1e-7)
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testHamiltonian_and_KL(self, type1, space, seed):
def testHamiltonian_and_KL(self, space, seed):
model = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
model = ift.PointwiseExponential(model)
lh = ift.GaussianEnergy(model)
space_key='s1', space=space, seed=seed)['s1']
model = model.exp()
lh = ift.GaussianEnergy()
hamiltonian = ift.Hamiltonian(lh)
ift.extra.check_value_gradient_consistency(hamiltonian)
ift.extra.check_value_gradient_consistency(hamiltonian, model)
S = ift.ScalingOperator(1., space)
samps = [S.draw_sample() for i in range(3)]
kl = ift.SampledKullbachLeiblerDivergence(hamiltonian, samps)
ift.extra.check_value_gradient_consistency(kl)
ift.extra.check_value_gradient_consistency(kl, model)
@expand(product(
['Variable'],
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
ift.RGSpace([32, 32], distances=.789)],
[4, 78, 23]
))
def testBernoulli(self, type1, space, seed):
def testBernoulli(self, space, seed):
model = self.make_model(
type1, space_key='s1', space=space, seed=seed)['s1']
model = ift.PointwisePositiveTanh(model)
space_key='s1', space=space, seed=seed)['s1']
model = model.positive_tanh()
d = np.random.binomial(1, 0.1, size=space.shape)
d = ift.Field.from_global_data(space, d)
energy = ift.BernoulliEnergy(model, d)
ift.extra.check_value_gradient_consistency(energy)
energy = ift.BernoulliEnergy(ift.positive_tanh, d)
ift.extra.check_value_gradient_consistency(energy, model, tol=1e-7)
......@@ -53,6 +53,7 @@ class Energy_Tests(unittest.TestCase):
n = N.draw_sample()
s = ht(ift.makeOp(A)(xi0_var))
R = ift.ScalingOperator(10., space)
def d_model(inp):
if nonlinearity == "":
return R(ht(ift.makeOp(A)(inp)))
......
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