Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Neel Shah
NIFTy
Commits
f5205c8f
Commit
f5205c8f
authored
Mar 11, 2020
by
Martin Reinecke
Committed by
Philipp Arras
Mar 11, 2020
Browse files
more fixes
parent
9523951a
Changes
11
Hide whitespace changes
Inline
Side-by-side
nifty6/minimization/conjugate_gradient.py
View file @
f5205c8f
...
...
@@ -70,14 +70,14 @@ class ConjugateGradient(Minimizer):
r
=
energy
.
gradient
d
=
r
if
preconditioner
is
None
else
preconditioner
(
r
)
previous_gamma
=
r
.
vdot
(
d
).
real
previous_gamma
=
r
.
s_
vdot
(
d
).
real
if
previous_gamma
==
0
:
return
energy
,
controller
.
CONVERGED
ii
=
0
while
True
:
q
=
energy
.
apply_metric
(
d
)
curv
=
d
.
vdot
(
q
).
real
curv
=
d
.
s_
vdot
(
q
).
real
if
curv
==
0.
:
logger
.
error
(
"Error: ConjugateGradient: curv==0."
)
return
energy
,
controller
.
ERROR
...
...
@@ -98,7 +98,7 @@ class ConjugateGradient(Minimizer):
s
=
r
if
preconditioner
is
None
else
preconditioner
(
r
)
gamma
=
r
.
vdot
(
s
).
real
gamma
=
r
.
s_
vdot
(
s
).
real
if
gamma
<
0
:
logger
.
error
(
"Positive definiteness of preconditioner violated!"
)
...
...
nifty6/minimization/descent_minimizers.py
View file @
f5205c8f
...
...
@@ -227,16 +227,16 @@ class L_BFGS(DescentMinimizer):
if
nhist
>
0
:
for
i
in
range
(
k
-
1
,
k
-
nhist
-
1
,
-
1
):
idx
=
i
%
maxhist
alpha
[
idx
]
=
s
[
idx
].
vdot
(
p
)
/
s
[
idx
].
vdot
(
y
[
idx
])
alpha
[
idx
]
=
s
[
idx
].
s_
vdot
(
p
)
/
s
[
idx
].
s_
vdot
(
y
[
idx
])
p
=
p
-
alpha
[
idx
]
*
y
[
idx
]
idx
=
(
k
-
1
)
%
maxhist
fact
=
s
[
idx
].
vdot
(
y
[
idx
])
/
y
[
idx
].
vdot
(
y
[
idx
])
fact
=
s
[
idx
].
s_
vdot
(
y
[
idx
])
/
y
[
idx
].
s_
vdot
(
y
[
idx
])
if
fact
<=
0.
:
logger
.
error
(
"L-BFGS curvature not positive definite!"
)
p
=
p
*
fact
for
i
in
range
(
k
-
nhist
,
k
):
idx
=
i
%
maxhist
beta
=
y
[
idx
].
vdot
(
p
)
/
s
[
idx
].
vdot
(
y
[
idx
])
beta
=
y
[
idx
].
s_
vdot
(
p
)
/
s
[
idx
].
s_
vdot
(
y
[
idx
])
p
=
p
+
(
alpha
[
idx
]
-
beta
)
*
s
[
idx
]
self
.
_lastx
=
x
self
.
_lastgrad
=
gradient
...
...
@@ -388,12 +388,12 @@ class _InformationStore(object):
k1
=
(
k
-
1
)
%
mmax
for
i
in
range
(
m
):
kmi
=
(
k
-
m
+
i
)
%
mmax
self
.
ss
[
kmi
,
k1
]
=
self
.
ss
[
k1
,
kmi
]
=
self
.
s
[
kmi
].
vdot
(
self
.
s
[
k1
])
self
.
yy
[
kmi
,
k1
]
=
self
.
yy
[
k1
,
kmi
]
=
self
.
y
[
kmi
].
vdot
(
self
.
y
[
k1
])
self
.
sy
[
kmi
,
k1
]
=
self
.
s
[
kmi
].
vdot
(
self
.
y
[
k1
])
self
.
ss
[
kmi
,
k1
]
=
self
.
ss
[
k1
,
kmi
]
=
self
.
s
[
kmi
].
s_
vdot
(
self
.
s
[
k1
])
self
.
yy
[
kmi
,
k1
]
=
self
.
yy
[
k1
,
kmi
]
=
self
.
y
[
kmi
].
s_
vdot
(
self
.
y
[
k1
])
self
.
sy
[
kmi
,
k1
]
=
self
.
s
[
kmi
].
s_
vdot
(
self
.
y
[
k1
])
for
j
in
range
(
m
-
1
):
kmj
=
(
k
-
m
+
j
)
%
mmax
self
.
sy
[
k1
,
kmj
]
=
self
.
s
[
k1
].
vdot
(
self
.
y
[
kmj
])
self
.
sy
[
k1
,
kmj
]
=
self
.
s
[
k1
].
s_
vdot
(
self
.
y
[
kmj
])
for
i
in
range
(
m
):
kmi
=
(
k
-
m
+
i
)
%
mmax
...
...
@@ -403,10 +403,10 @@ class _InformationStore(object):
result
[
i
,
m
+
j
]
=
result
[
m
+
j
,
i
]
=
self
.
sy
[
kmi
,
kmj
]
result
[
m
+
i
,
m
+
j
]
=
self
.
yy
[
kmi
,
kmj
]
sgrad_i
=
self
.
s
[
kmi
].
vdot
(
self
.
last_gradient
)
sgrad_i
=
self
.
s
[
kmi
].
s_
vdot
(
self
.
last_gradient
)
result
[
2
*
m
,
i
]
=
result
[
i
,
2
*
m
]
=
sgrad_i
ygrad_i
=
self
.
y
[
kmi
].
vdot
(
self
.
last_gradient
)
ygrad_i
=
self
.
y
[
kmi
].
s_
vdot
(
self
.
last_gradient
)
result
[
2
*
m
,
m
+
i
]
=
result
[
m
+
i
,
2
*
m
]
=
ygrad_i
result
[
2
*
m
,
2
*
m
]
=
self
.
last_gradient
.
norm
()
...
...
nifty6/minimization/line_search.py
View file @
f5205c8f
...
...
@@ -95,7 +95,7 @@ class LineEnergy(object):
"""
float : The directional derivative at the given `position`.
"""
res
=
self
.
_energy
.
gradient
.
vdot
(
self
.
_line_direction
)
res
=
self
.
_energy
.
gradient
.
s_
vdot
(
self
.
_line_direction
)
if
abs
(
res
.
imag
)
/
max
(
abs
(
res
.
real
),
1.
)
>
1e-12
:
from
..logger
import
logger
logger
.
warning
(
"directional derivative has non-negligible "
...
...
nifty6/minimization/nonlinear_cg.py
View file @
f5205c8f
...
...
@@ -75,17 +75,17 @@ class NonlinearCG(Minimizer):
if
self
.
_beta_heuristic
==
'Hestenes-Stiefel'
:
# Eq. (5.46) in Nocedal & Wright.
beta
=
max
(
0.0
,
(
grad_new
.
vdot
(
grad_new
-
grad_old
)
/
(
grad_new
-
grad_old
).
vdot
(
p
)).
real
)
beta
=
max
(
0.0
,
(
grad_new
.
s_
vdot
(
grad_new
-
grad_old
)
/
(
grad_new
-
grad_old
).
s_
vdot
(
p
)).
real
)
elif
self
.
_beta_heuristic
==
'Polak-Ribiere'
:
# Eq. (5.44) in Nocedal & Wright. (with (5.45) additionally)
beta
=
max
(
0.0
,
(
grad_new
.
vdot
(
grad_new
-
grad_old
)
/
(
grad_old
.
vdot
(
grad_old
))).
real
)
beta
=
max
(
0.0
,
(
grad_new
.
s_
vdot
(
grad_new
-
grad_old
)
/
(
grad_old
.
s_
vdot
(
grad_old
))).
real
)
elif
self
.
_beta_heuristic
==
'Fletcher-Reeves'
:
# Eq. (5.41a) in Nocedal & Wright.
beta
=
(
grad_new
.
vdot
(
grad_new
)
/
(
grad_old
.
vdot
(
grad_old
))).
real
beta
=
(
grad_new
.
s_
vdot
(
grad_new
)
/
(
grad_old
.
s_
vdot
(
grad_old
))).
real
else
:
# Eq. (5.49) in Nocedal & Wright.
beta
=
(
grad_new
.
vdot
(
grad_new
)
/
((
grad_new
-
grad_old
).
vdot
(
p
))).
real
beta
=
(
grad_new
.
s_
vdot
(
grad_new
)
/
((
grad_new
-
grad_old
).
s_
vdot
(
p
))).
real
p
=
beta
*
p
-
grad_new
nifty6/minimization/quadratic_energy.py
View file @
f5205c8f
...
...
@@ -34,9 +34,9 @@ class QuadraticEnergy(Energy):
else
:
Ax
=
self
.
_A
(
self
.
_position
)
self
.
_grad
=
Ax
if
b
is
None
else
Ax
-
b
self
.
_value
=
0.5
*
self
.
_position
.
vdot
(
Ax
)
self
.
_value
=
0.5
*
self
.
_position
.
s_
vdot
(
Ax
)
if
b
is
not
None
:
self
.
_value
-=
b
.
vdot
(
self
.
_position
)
self
.
_value
-=
b
.
s_
vdot
(
self
.
_position
)
def
at
(
self
,
position
):
return
QuadraticEnergy
(
position
,
self
.
_A
,
self
.
_b
)
...
...
nifty6/minimization/scipy_minimizer.py
View file @
f5205c8f
...
...
@@ -74,7 +74,7 @@ class _MinHelper(object):
def
_update
(
self
,
x
):
pos
=
_toField
(
x
,
self
.
_energy
.
position
)
if
(
pos
!=
self
.
_energy
.
position
).
any
():
if
(
pos
!=
self
.
_energy
.
position
).
s_
any
():
self
.
_energy
=
self
.
_energy
.
at
(
pos
)
def
fun
(
self
,
x
):
...
...
nifty6/multi_field.py
View file @
f5205c8f
...
...
@@ -169,7 +169,7 @@ class MultiField(object):
return
(
nrm
**
ord
).
sum
()
**
(
1.
/
ord
)
# return np.sqrt(np.abs(self.vdot(x=self)))
def
sum
(
self
):
def
s_
sum
(
self
):
"""Computes the sum all field values.
Returns
...
...
@@ -177,7 +177,7 @@ class MultiField(object):
norm : float
The sum of the field values.
"""
return
utilities
.
my_sum
(
map
(
lambda
v
:
v
.
sum
(),
self
.
_val
))
return
utilities
.
my_sum
(
map
(
lambda
v
:
v
.
s_
sum
(),
self
.
_val
))
@
property
def
size
(
self
):
...
...
@@ -207,15 +207,15 @@ class MultiField(object):
self
.
_domain
,
tuple
(
self
.
_val
[
i
].
clip
(
lmin
[
i
],
lmax
[
i
])
for
i
in
range
(
ncomp
)))
def
all
(
self
):
def
s_
all
(
self
):
for
v
in
self
.
_val
:
if
not
v
.
all
():
if
not
v
.
s_
all
():
return
False
return
True
def
any
(
self
):
def
s_
any
(
self
):
for
v
in
self
.
_val
:
if
v
.
any
():
if
v
.
s_
any
():
return
True
return
False
...
...
test/test_multi_field.py
View file @
f5205c8f
...
...
@@ -26,7 +26,7 @@ dom = ift.makeDomain({"d1": ift.RGSpace(10)})
def
test_vdot
():
f1
=
ift
.
from_random
(
"normal"
,
domain
=
dom
,
dtype
=
np
.
complex128
)
f2
=
ift
.
from_random
(
"normal"
,
domain
=
dom
,
dtype
=
np
.
complex128
)
assert_allclose
(
f1
.
vdot
(
f2
),
np
.
conj
(
f2
.
vdot
(
f1
)))
assert_allclose
(
f1
.
s_
vdot
(
f2
),
np
.
conj
(
f2
.
s_
vdot
(
f1
)))
def
test_func
():
...
...
@@ -38,7 +38,7 @@ def test_func():
def
test_multifield_field_consistency
():
f1
=
ift
.
full
(
dom
,
27
)
f2
=
ift
.
makeField
(
dom
[
'd1'
],
f1
[
'd1'
].
val
)
assert_equal
(
f1
.
sum
(),
f2
.
sum
())
assert_equal
(
f1
.
s_
sum
(),
f2
.
s_
sum
())
assert_equal
(
f1
.
size
,
f2
.
size
)
...
...
@@ -64,9 +64,9 @@ def test_blockdiagonal():
assert_equal
(
type
(
op2
),
ift
.
BlockDiagonalOperator
)
f1
=
op2
(
ift
.
full
(
dom
,
1
))
for
val
in
f1
.
values
():
assert_equal
((
val
==
400
).
all
(),
True
)
assert_equal
((
val
==
400
).
s_
all
(),
True
)
op2
=
op
+
op
assert_equal
(
type
(
op2
),
ift
.
BlockDiagonalOperator
)
f1
=
op2
(
ift
.
full
(
dom
,
1
))
for
val
in
f1
.
values
():
assert_equal
((
val
==
40
).
all
(),
True
)
assert_equal
((
val
==
40
).
s_
all
(),
True
)
test/test_operators/test_composed_operator.py
View file @
f5205c8f
...
...
@@ -43,8 +43,8 @@ def test_times_adjoint_times(space1, space2):
rand1
=
ift
.
Field
.
from_random
(
'normal'
,
domain
=
(
space1
,
space2
))
rand2
=
ift
.
Field
.
from_random
(
'normal'
,
domain
=
(
space1
,
space2
))
tt1
=
rand2
.
vdot
(
op
.
times
(
rand1
))
tt2
=
rand1
.
vdot
(
op
.
adjoint_times
(
rand2
))
tt1
=
rand2
.
s_
vdot
(
op
.
times
(
rand1
))
tt2
=
rand1
.
s_
vdot
(
op
.
adjoint_times
(
rand2
))
assert_allclose
(
tt1
,
tt2
)
...
...
test/test_operators/test_diagonal_operator.py
View file @
f5205c8f
...
...
@@ -42,8 +42,8 @@ def test_times_adjoint(space):
rand2
=
ift
.
Field
.
from_random
(
'normal'
,
domain
=
space
)
diag
=
ift
.
Field
.
from_random
(
'normal'
,
domain
=
space
)
D
=
ift
.
DiagonalOperator
(
diag
)
tt1
=
rand1
.
vdot
(
D
.
times
(
rand2
))
tt2
=
rand2
.
vdot
(
D
.
times
(
rand1
))
tt1
=
rand1
.
s_
vdot
(
D
.
times
(
rand2
))
tt2
=
rand2
.
s_
vdot
(
D
.
times
(
rand1
))
assert_allclose
(
tt1
,
tt2
)
...
...
test/test_operators/test_smoothing_operator.py
View file @
f5205c8f
...
...
@@ -47,8 +47,8 @@ def test_adjoint_times(space, sigma):
op
=
ift
.
HarmonicSmoothingOperator
(
space
,
sigma
=
sigma
)
rand1
=
ift
.
Field
.
from_random
(
'normal'
,
domain
=
space
)
rand2
=
ift
.
Field
.
from_random
(
'normal'
,
domain
=
space
)
tt1
=
rand1
.
vdot
(
op
.
times
(
rand2
))
tt2
=
rand2
.
vdot
(
op
.
adjoint_times
(
rand1
))
tt1
=
rand1
.
s_
vdot
(
op
.
times
(
rand2
))
tt2
=
rand2
.
s_
vdot
(
op
.
adjoint_times
(
rand1
))
assert_allclose
(
tt1
,
tt2
)
...
...
@@ -58,7 +58,7 @@ def test_times(space, sigma):
fld
[
0
]
=
1.
rand1
=
ift
.
Field
.
from_raw
(
space
,
fld
)
tt1
=
op
.
times
(
rand1
)
assert_allclose
(
1
,
tt1
.
sum
())
assert_allclose
(
1
,
tt1
.
s_
sum
())
@
pmp
(
'sz'
,
[
128
,
256
])
...
...
@@ -70,7 +70,7 @@ def test_smooth_regular1(sz, d, sigma, tp):
inp
=
ift
.
Field
.
from_random
(
domain
=
sp
,
random_type
=
'normal'
,
std
=
1
,
mean
=
4
,
dtype
=
tp
)
out
=
smo
(
inp
)
assert_allclose
(
inp
.
sum
(),
out
.
sum
(),
rtol
=
tol
,
atol
=
tol
)
assert_allclose
(
inp
.
s_
sum
(),
out
.
s_
sum
(),
rtol
=
tol
,
atol
=
tol
)
@
pmp
(
'sz1'
,
[
10
,
15
])
...
...
@@ -84,4 +84,4 @@ def test_smooth_regular2(sz1, sz2, d1, d2, sigma, tp):
inp
=
ift
.
Field
.
from_random
(
domain
=
sp
,
random_type
=
'normal'
,
std
=
1
,
mean
=
4
,
dtype
=
tp
)
out
=
smo
(
inp
)
assert_allclose
(
inp
.
sum
(),
out
.
sum
(),
rtol
=
tol
,
atol
=
tol
)
assert_allclose
(
inp
.
s_
sum
(),
out
.
s_
sum
(),
rtol
=
tol
,
atol
=
tol
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment