Commit 77ef8995 authored by Reimar H Leike's avatar Reimar H Leike
Browse files

Merge branch 'NIFTy_5' into mpi_experiments

maintenance of this branch
parents fcd5dad8 b55598c9
......@@ -559,7 +559,7 @@ def absmax(arr):
if arr._data.size == 0:
tmp = np.array(0, dtype=arr._data.dtype)
else:
tmp = np.asarray(np.linalg.norm(arr._data, ord=np.inf))
tmp = np.asarray(np.linalg.norm(arr._data.reshape(-1), ord=np.inf))
res = np.empty_like(tmp)
_comm.Allreduce(tmp, res, MPI.MAX)
return res[()]
......@@ -568,7 +568,10 @@ def absmax(arr):
def norm(arr, ord=2):
if ord == np.inf:
return absmax(arr)
tmp = np.asarray(np.linalg.norm(np.atleast_1d(arr._data), ord=ord) ** ord)
tmp = np.asarray(np.linalg.norm(arr._data.reshape(-1), ord=ord) ** ord)
res = np.empty_like(tmp)
_comm.Allreduce(tmp, res, MPI.SUM)
if len(arr._data.shape) == 0:
res = tmp
else:
_comm.Allreduce(tmp, res, MPI.SUM)
return res[()] ** (1./ord)
......@@ -144,8 +144,8 @@ def ensure_default_distributed(arr):
def absmax(arr):
return np.linalg.norm(arr, ord=np.inf)
return np.linalg.norm(arr.rehape(-1), ord=np.inf)
def norm(arr, ord=2):
return np.linalg.norm(np.atleast_1d(arr), ord=ord)
return np.linalg.norm(arr.reshape(-1), ord=ord)
......@@ -460,7 +460,7 @@ class Field(object):
swgt = self.scalar_weight(spaces)
if swgt is not None:
res = self.sum(spaces)
res *= swgt
res = res*swgt
return res
tmp = self.weight(1, spaces=spaces)
return tmp.sum(spaces)
......
......@@ -23,7 +23,8 @@ from ..operators.energy_operators import Hamiltonian, InverseGammaLikelihood
from ..operators.scaling_operator import ScalingOperator
def make_adjust_variances(a, xi, position, samples=[], scaling=None, ic_samp=None):
def make_adjust_variances(a, xi, position, samples=[], scaling=None,
ic_samp=None):
""" Creates a Hamiltonian for constant likelihood optimizations.
Constructs a Hamiltonian to solve constant likelihood optimizations of the
......
......@@ -22,7 +22,6 @@ import numpy as np
from ..compat import *
from ..domains.power_space import PowerSpace
from ..domains.unstructured_domain import UnstructuredDomain
from ..field import Field
from ..multi_domain import MultiDomain
from ..operators.operator import Operator
......
......@@ -3,8 +3,6 @@ from __future__ import absolute_import, division, print_function
from ..compat import *
from ..linearization import Linearization
from ..minimization.energy import Energy
from ..operators.block_diagonal_operator import BlockDiagonalOperator
from ..operators.scaling_operator import ScalingOperator
class EnergyAdapter(Energy):
......
......@@ -3,8 +3,6 @@ from __future__ import absolute_import, division, print_function
from ..compat import *
from .energy import Energy
from ..linearization import Linearization
from ..operators.scaling_operator import ScalingOperator
from ..operators.block_diagonal_operator import BlockDiagonalOperator
from .. import utilities
from ..field import Field
from ..multi_field import MultiField
......@@ -102,12 +100,17 @@ class KL_Energy_MPI(Energy):
class KL_Energy(Energy):
def __init__(self, position, h, nsamp, constants=[], _samples=None):
def __init__(self, position, h, nsamp, constants=[],
constants_samples=None, _samples=None):
super(KL_Energy, self).__init__(position)
self._h = h
self._constants = constants
if constants_samples is None:
constants_samples = constants
self._constants_samples = constants_samples
if _samples is None:
met = h(Linearization.make_var(position, True)).metric
met = h(Linearization.make_partial_var(
position, constants_samples, True)).metric
_samples = tuple(met.draw_sample(from_inverse=True)
for _ in range(nsamp))
self._samples = _samples
......@@ -127,7 +130,8 @@ class KL_Energy(Energy):
self._metric = None
def at(self, position):
return KL_Energy(position, self._h, 0, self._constants, self._samples)
return KL_Energy(position, self._h, 0, self._constants,
self._constants_samples, self._samples)
@property
def value(self):
......
......@@ -66,11 +66,6 @@ class MultiField(object):
def to_dict(self):
return {key: val for key, val in zip(self._domain.keys(), self._val)}
def update(self, other):
foo = self.to_dict()
foo.update(other.to_dict())
return MultiField.from_dict(foo)
def __getitem__(self, key):
return self._val[self._domain.idx[key]]
......@@ -222,6 +217,33 @@ class MultiField(object):
res[key] = res[key]+val if key in res else val
return MultiField.from_dict(res)
@staticmethod
def union(fields, domain=None):
"""Returns the union of its input fields
Parameters
----------
fields: iterable of MultiFields
The set of input fields. Their domains need not be identical.
domain: MultiDomain or None
If supplied, this will be the domain of the resulting field.
Providing this domain will accelerate the function.
Returns
-------
MultiField
The union of the input fields
Notes
-----
If the same key occurs more than once in the input fields, the value
associated with the last occurrence will be put into the output.
No summation is performed!
"""
res = {}
for field in fields:
res.update(field.to_dict())
return MultiField.from_dict(res, domain)
def flexible_addsub(self, other, neg):
if self._domain is other._domain:
return self-other if neg else self+other
......
......@@ -45,7 +45,7 @@ class VdotOperator(LinearOperator):
class SumReductionOperator(LinearOperator):
def __init__(self, domain):
self._domain = domain
self._domain = DomainTuple.make(domain)
self._target = DomainTuple.scalar_domain()
self._capability = self.TIMES | self.ADJOINT_TIMES
......@@ -58,7 +58,7 @@ class SumReductionOperator(LinearOperator):
class ConjugationOperator(EndomorphicOperator):
def __init__(self, domain):
self._domain = domain
self._domain = DomainTuple.make(domain)
self._capability = self._all_ops
def apply(self, x, mode):
......@@ -68,7 +68,7 @@ class ConjugationOperator(EndomorphicOperator):
class Realizer(EndomorphicOperator):
def __init__(self, domain):
self._domain = domain
self._domain = DomainTuple.make(domain)
self._capability = self.TIMES | self.ADJOINT_TIMES
def apply(self, x, mode):
......
......@@ -69,7 +69,6 @@ class Energy_Tests(unittest.TestCase):
energy = ift.InverseGammaLikelihood(ift.exp, d)
ift.extra.check_value_gradient_consistency(energy, model, tol=1e-7)
@expand(product(
[ift.GLSpace(15),
ift.RGSpace(64, distances=.789),
......
......@@ -110,10 +110,12 @@ class Test_Functionality(unittest.TestCase):
assert_allclose(sc1.mean.local_data, fp1.local_data, rtol=0.2)
assert_allclose(sc2.mean.local_data, fp2.local_data, rtol=0.2)
def test_norm(self):
s = ift.RGSpace((10,))
f = ift.Field.from_random("normal", domain=s, dtype=np.complex128)
gd = f.to_global_data()
@expand(product([ift.RGSpace((8,), harmonic=True), (),
ift.RGSpace((8, 8), harmonic=True, distances=0.123),
ift.RGSpace((2, 3, 7))]))
def test_norm(self, space):
f = ift.Field.from_random("normal", domain=space, dtype=np.complex128)
gd = f.to_global_data().reshape(-1)
assert_allclose(f.norm(), np.linalg.norm(gd))
assert_allclose(f.norm(1), np.linalg.norm(gd, ord=1))
assert_allclose(f.norm(2), np.linalg.norm(gd, ord=2))
......
......@@ -56,19 +56,3 @@ class Test_Functionality(unittest.TestCase):
f1 = op2(ift.full(dom, 1))
for val in f1.values():
assert_equal((val == 40).all(), True)
def test_update(self):
dom = ift.RGSpace(10)
f1 = ift.from_random('normal', domain=dom)
f2 = ift.from_random('normal', domain=dom)
f_new = ift.MultiField.from_dict({'dom1': f1, 'dom2': f2})
f3 = ift.from_random('normal', domain=dom)
f4 = ift.from_random('normal', domain=dom)
f5 = ift.from_random('normal', domain=dom)
f_old = ift.MultiField.from_dict({'dom1': f3, 'dom2': f4, 'dom3': f5})
updated = f_old.update(f_new).to_dict()
updated_true = f_old.to_dict()
updated_true.update(f_new.to_dict())
for key, val in updated.items():
assert_equal((val == updated_true[key]).all(), True)
......@@ -65,6 +65,12 @@ class Consistency_Tests(unittest.TestCase):
dtype=dtype))
ift.extra.consistency_check(op, dtype, dtype)
@expand(product(_h_spaces + _p_spaces + _pow_spaces,
[np.float64, np.complex128]))
def testSumReductionOperator(self, sp, dtype):
op = ift.SumReductionOperator(sp)
ift.extra.consistency_check(op, dtype, dtype)
@expand(product([(ift.RGSpace(10, harmonic=True), 4, 0),
(ift.RGSpace((24, 31), distances=(0.4, 2.34),
harmonic=True), 3, 0),
......
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