Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ift
NIFTy
Commits
b9e3ff80
Commit
b9e3ff80
authored
Aug 19, 2017
by
Martin Reinecke
Browse files
changes
parent
1285fc8f
Pipeline
#16896
passed with stage
in 10 minutes and 33 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
test/test_minimization/test_minimizers.py
View file @
b9e3ff80
import
unittest
import
numpy
as
np
from
numpy.testing
import
assert_equal
,
assert_allclose
from
numpy.testing
import
assert_allclose
from
nifty
import
Field
,
DiagonalOperator
,
RGSpace
,
HPSpace
from
nifty
import
ConjugateGradient
,
QuadraticEnergy
import
nifty
as
ift
from
itertools
import
product
from
test.common
import
expand
spaces
=
[
RGSpace
([
1024
,
1024
],
distances
=
0.123
),
HPSpace
(
32
)]
spaces
=
[
ift
.
RGSpace
([
1024
],
distances
=
0.123
),
ift
.
HPSpace
(
32
)]
minimizers
=
[
ift
.
SteepestDescent
,
ift
.
RelaxedNewton
,
ift
.
VL_BFGS
,
ift
.
ConjugateGradient
]
class
Test_
ConjugateGradient
(
unittest
.
TestCase
):
class
Test_
Minimizers
(
unittest
.
TestCase
):
def
test_interface
(
self
):
iteration_limit
=
100
convergence_level
=
4
convergence_tolerance
=
1E-6
callback
=
lambda
z
:
z
minimizer
=
ConjugateGradient
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
,
callback
=
callback
)
assert_equal
(
minimizer
.
iteration_limit
,
iteration_limit
)
assert_equal
(
minimizer
.
convergence_level
,
convergence_level
)
assert_equal
(
minimizer
.
convergence_tolerance
,
convergence_tolerance
)
assert
(
minimizer
.
callback
is
callback
)
@
expand
([[
space
]
for
space
in
spaces
])
def
test_minimization
(
self
,
space
):
@
expand
(
product
(
minimizers
,
spaces
))
def
test_minimization
(
self
,
minimizer_class
,
space
):
np
.
random
.
seed
(
42
)
starting_point
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
*
10
covariance_diagonal
=
Field
.
from_random
(
'uniform'
,
domain
=
space
)
+
0.5
covariance
=
DiagonalOperator
(
space
,
diagonal
=
covariance_diagonal
)
required_result
=
Field
(
space
,
val
=
1.
)
starting_point
=
ift
.
Field
.
from_random
(
'normal'
,
domain
=
space
)
*
10
covariance_diagonal
=
ift
.
Field
.
from_random
(
'uniform'
,
domain
=
space
)
+
0.5
covariance
=
ift
.
DiagonalOperator
(
space
,
diagonal
=
covariance_diagonal
)
required_result
=
ift
.
Field
(
space
,
val
=
1.
)
minimizer
=
ConjugateGradient
()
energy
=
QuadraticEnergy
(
A
=
covariance
,
b
=
required_result
,
position
=
starting_point
)
IC
=
ift
.
DefaultIterationController
(
tol_gradnorm
=
1e-5
)
minimizer
=
minimizer_class
(
controller
=
IC
)
energy
=
ift
.
QuadraticEnergy
(
A
=
covariance
,
b
=
required_result
,
position
=
starting_point
)
(
energy
,
convergence
)
=
minimizer
(
energy
)
assert
convergence
==
IC
.
CONVERGED
assert_allclose
(
energy
.
position
.
val
.
get_full_data
(),
1.
/
covariance_diagonal
.
val
.
get_full_data
(),
r
tol
=
1e-3
)
1.
/
covariance_diagonal
.
val
.
get_full_data
(),
rtol
=
1e-3
,
a
tol
=
1e-3
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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