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
ift
NIFTy
Commits
d59ba775
Commit
d59ba775
authored
May 24, 2017
by
Matevz, Sraml (sraml)
Browse files
minimization tests
parent
dd80d59d
Pipeline
#12957
failed with stage
in 12 minutes and 49 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
test/test_minimization/test_conjugate_gradient.py
0 → 100644
View file @
d59ba775
import
unittest
from
numpy.testing
import
assert_approx_equal
,
assert_equal
from
nifty
import
*
from
itertools
import
product
from
test.common
import
expand
from
test.common
import
generate_spaces
class
ConjugateGradient_Tests
(
unittest
.
TestCase
):
spaces
=
generate_spaces
()
@
expand
(
product
(
spaces
,
[
10
,
100
,
1000
],
[
1E-3
,
1E-4
,
1E-5
],
[
2
,
3
,
4
]
))
def
test_property
(
self
,
space
,
iteration_limit
,
convergence_tolerance
,
convergence_level
):
x0
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
A
=
DiagonalOperator
(
space
,
diagonal
=
1.
)
b
=
Field
(
space
,
val
=
0.
)
minimizer
=
ConjugateGradient
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
)
(
position
,
convergence
)
=
minimizer
(
A
=
A
,
x0
=
x0
,
b
=
b
)
if
position
.
domain
[
0
]
!=
space
:
raise
TypeError
if
type
(
convergence
)
!=
float
:
raise
TypeError
@
expand
(
product
(
spaces
,
[
10
,
100
,
1000
],
[
1E-3
,
1E-4
,
1E-5
],
[
2
,
3
,
4
]
))
def
test_property
(
self
,
space
,
iteration_limit
,
convergence_tolerance
,
convergence_level
):
x0
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
test_x
=
Field
(
space
,
val
=
0.
)
A
=
DiagonalOperator
(
space
,
diagonal
=
1.
)
b
=
Field
(
space
,
val
=
0.
)
minimizer
=
ConjugateGradient
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
)
(
position
,
convergence
)
=
minimizer
(
A
=
A
,
x0
=
x0
,
b
=
b
)
assert_approx_equal
(
position
.
val
.
get_full_data
(),
test_x
.
val
.
get_full_data
(),
significant
=
3
)
assert_equal
(
convergence
,
convergence_level
+
1
)
test/test_minimization/test_relaxed_newton.py
0 → 100644
View file @
d59ba775
import
unittest
from
numpy.testing
import
assert_approx_equal
,
assert_equal
from
nifty
import
*
from
itertools
import
product
from
test.common
import
expand
from
test.common
import
generate_spaces
class
QuadraticPot
(
Energy
):
def
__init__
(
self
,
position
,
N
):
super
(
QuadraticPot
,
self
).
__init__
(
position
)
self
.
N
=
N
def
at
(
self
,
position
):
return
self
.
__class__
(
position
,
N
=
self
.
N
)
@
property
def
value
(
self
):
H
=
0.5
*
self
.
position
.
dot
(
self
.
N
.
inverse_times
(
self
.
position
))
return
H
.
real
@
property
def
gradient
(
self
):
g
=
self
.
N
.
inverse_times
(
self
.
position
)
return_g
=
g
.
copy_empty
(
dtype
=
np
.
float
)
return_g
.
val
=
g
.
val
.
real
return
return_g
@
property
def
curvature
(
self
):
return
self
.
N
class
RelaxedNewton_Tests
(
unittest
.
TestCase
):
spaces
=
generate_spaces
()
@
expand
(
product
(
spaces
,
[
10
,
100
,
1000
],
[
1E-3
,
1E-4
,
1E-5
],
[
2
,
3
,
4
]
))
def
test_property
(
self
,
space
,
iteration_limit
,
convergence_tolerance
,
convergence_level
):
x
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
N
=
DiagonalOperator
(
space
,
diagonal
=
1.
)
energy
=
QuadraticPot
(
position
=
x
,
N
=
N
)
minimizer
=
RelaxedNewton
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
)
(
energy
,
convergence
)
=
minimizer
(
energy
)
if
energy
.
position
.
domain
[
0
]
!=
space
:
raise
TypeError
if
type
(
convergence
)
!=
float
:
raise
TypeError
@
expand
(
product
(
spaces
,
[
10
,
100
,
1000
],
[
1E-3
,
1E-4
,
1E-5
],
[
2
,
3
,
4
]
))
def
test_property
(
self
,
space
,
iteration_limit
,
convergence_tolerance
,
convergence_level
):
x
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
test_x
=
Field
(
space
,
val
=
0.
)
N
=
DiagonalOperator
(
space
,
diagonal
=
1.
)
energy
=
QuadraticPot
(
position
=
x
,
N
=
N
)
minimizer
=
RelaxedNewton
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
)
(
energy
,
convergence
)
=
minimizer
(
energy
)
assert_approx_equal
(
energy
.
value
,
0
,
significant
=
3
)
assert_approx_equal
(
energy
.
position
.
val
.
get_full_data
(),
test_x
.
val
.
get_full_data
(),
significant
=
3
)
assert_equal
(
convergence
,
convergence_level
+
2
)
test/test_minimization/test_steepest_descent.py
0 → 100644
View file @
d59ba775
import
unittest
from
numpy.testing
import
assert_approx_equal
,
assert_equal
from
nifty
import
*
from
itertools
import
product
from
test.common
import
expand
from
test.common
import
generate_spaces
np
.
random
.
seed
(
42
)
class
QuadraticPot
(
Energy
):
def
__init__
(
self
,
position
,
N
):
super
(
QuadraticPot
,
self
).
__init__
(
position
)
self
.
N
=
N
def
at
(
self
,
position
):
return
self
.
__class__
(
position
,
N
=
self
.
N
)
@
property
def
value
(
self
):
H
=
0.5
*
self
.
position
.
dot
(
self
.
N
.
inverse_times
(
self
.
position
))
return
H
.
real
@
property
def
gradient
(
self
):
g
=
self
.
N
.
inverse_times
(
self
.
position
)
return_g
=
g
.
copy_empty
(
dtype
=
np
.
float
)
return_g
.
val
=
g
.
val
.
real
return
return_g
@
property
def
curvature
(
self
):
return
self
.
N
class
SteepestDescent_Tests
(
unittest
.
TestCase
):
spaces
=
generate_spaces
()
@
expand
(
product
(
spaces
,
[
10
,
100
,
1000
],
[
1E-3
,
1E-4
,
1E-5
],
[
2
,
3
,
4
]
))
def
test_property
(
self
,
space
,
iteration_limit
,
convergence_tolerance
,
convergence_level
):
x
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
N
=
DiagonalOperator
(
space
,
diagonal
=
1.
)
energy
=
QuadraticPot
(
position
=
x
,
N
=
N
)
minimizer
=
SteepestDescent
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
)
(
energy
,
convergence
)
=
minimizer
(
energy
)
if
energy
.
position
.
domain
[
0
]
!=
space
:
raise
TypeError
if
type
(
convergence
)
!=
float
:
raise
TypeError
@
expand
(
product
(
spaces
,
[
10
,
100
,
1000
],
[
1E-3
,
1E-4
,
1E-5
],
[
2
,
3
,
4
]
))
def
test_property
(
self
,
space
,
iteration_limit
,
convergence_tolerance
,
convergence_level
):
x
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
test_x
=
Field
(
space
,
val
=
0.
)
N
=
DiagonalOperator
(
space
,
diagonal
=
1.
)
energy
=
QuadraticPot
(
position
=
x
,
N
=
N
)
minimizer
=
SteepestDescent
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
)
(
energy
,
convergence
)
=
minimizer
(
energy
)
assert_approx_equal
(
energy
.
value
,
0
,
significant
=
3
)
assert_approx_equal
(
energy
.
position
.
val
.
get_full_data
(),
test_x
.
val
.
get_full_data
(),
significant
=
3
)
assert_equal
(
convergence
,
convergence_level
+
2
)
test/test_minimization/test_vl_bfgs.py
0 → 100644
View file @
d59ba775
import
unittest
from
numpy.testing
import
assert_approx_equal
,
assert_equal
from
nifty
import
*
from
itertools
import
product
from
test.common
import
expand
from
test.common
import
generate_spaces
class
QuadraticPot
(
Energy
):
def
__init__
(
self
,
position
,
N
):
super
(
QuadraticPot
,
self
).
__init__
(
position
)
self
.
N
=
N
def
at
(
self
,
position
):
return
self
.
__class__
(
position
,
N
=
self
.
N
)
@
property
def
value
(
self
):
H
=
0.5
*
self
.
position
.
dot
(
self
.
N
.
inverse_times
(
self
.
position
))
return
H
.
real
@
property
def
gradient
(
self
):
g
=
self
.
N
.
inverse_times
(
self
.
position
)
return_g
=
g
.
copy_empty
(
dtype
=
np
.
float
)
return_g
.
val
=
g
.
val
.
real
return
return_g
@
property
def
curvature
(
self
):
return
self
.
N
class
VL_BFGS_Tests
(
unittest
.
TestCase
):
spaces
=
generate_spaces
()
@
expand
(
product
(
spaces
,
[
10
,
100
,
1000
],
[
1E-3
,
1E-4
,
1E-5
],
[
2
,
3
,
4
]
))
def
test_property
(
self
,
space
,
iteration_limit
,
convergence_tolerance
,
convergence_level
):
x
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
N
=
DiagonalOperator
(
space
,
diagonal
=
1.
)
energy
=
QuadraticPot
(
position
=
x
,
N
=
N
)
minimizer
=
VL_BFGS
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
)
(
energy
,
convergence
)
=
minimizer
(
energy
)
if
energy
.
position
.
domain
[
0
]
!=
space
:
raise
TypeError
if
type
(
convergence
)
!=
float
:
raise
TypeError
@
expand
(
product
(
spaces
,
[
10
,
100
,
1000
],
[
1E-3
,
1E-4
,
1E-5
],
[
2
,
3
,
4
]
))
def
test_property
(
self
,
space
,
iteration_limit
,
convergence_tolerance
,
convergence_level
):
x
=
Field
.
from_random
(
'normal'
,
domain
=
space
)
test_x
=
Field
(
space
,
val
=
0.
)
N
=
DiagonalOperator
(
space
,
diagonal
=
1.
)
energy
=
QuadraticPot
(
position
=
x
,
N
=
N
)
minimizer
=
VL_BFGS
(
iteration_limit
=
iteration_limit
,
convergence_tolerance
=
convergence_tolerance
,
convergence_level
=
convergence_level
)
(
energy
,
convergence
)
=
minimizer
(
energy
)
assert_approx_equal
(
energy
.
value
,
0
,
significant
=
3
)
assert_approx_equal
(
energy
.
position
.
val
.
get_full_data
(),
test_x
.
val
.
get_full_data
(),
significant
=
3
)
assert_equal
(
convergence
,
convergence_level
)
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