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
f46fca01
Commit
f46fca01
authored
Feb 09, 2017
by
Jait Dixit
Browse files
Merge branch 'master' into tests
parents
fd9d341d
185f454e
Pipeline
#10084
passed with stage
in 19 minutes and 36 seconds
Changes
15
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
demos/probing.py
0 → 100644
View file @
f46fca01
# -*- coding: utf-8 -*-
from
nifty
import
Field
,
RGSpace
,
DiagonalProberMixin
,
TraceProberMixin
,
\
Prober
,
DiagonalOperator
class
DiagonalProber
(
DiagonalProberMixin
,
Prober
):
pass
class
MultiProber
(
DiagonalProberMixin
,
TraceProberMixin
,
Prober
):
pass
x
=
RGSpace
((
8
,
8
))
f
=
Field
.
from_random
(
domain
=
x
,
random_type
=
'normal'
)
diagOp
=
DiagonalOperator
(
domain
=
x
,
diagonal
=
f
)
diagProber
=
DiagonalProber
(
domain
=
x
)
diagProber
(
diagOp
)
print
(
f
-
diagProber
.
diagonal
).
norm
()
multiProber
=
MultiProber
(
domain
=
x
)
multiProber
(
diagOp
)
print
(
f
-
multiProber
.
diagonal
).
norm
()
print
f
.
sum
()
-
multiProber
.
trace
demos/wiener_filter_hamiltonian.py
View file @
f46fca01
from
nifty
import
*
#
import plotly.offline as pl
#
import plotly.graph_objs as go
import
plotly.offline
as
pl
import
plotly.graph_objs
as
go
from
mpi4py
import
MPI
comm
=
MPI
.
COMM_WORLD
rank
=
comm
.
rank
np
.
random
.
seed
(
42
)
class
WienerFilterEnergy
(
Energy
):
def
__init__
(
self
,
position
,
D
,
j
):
...
...
@@ -34,6 +35,17 @@ class WienerFilterEnergy(Energy):
return_g
.
val
=
g
.
val
.
real
return
return_g
@
property
def
curvature
(
self
):
class
Dummy
(
object
):
def
__init__
(
self
,
x
):
self
.
x
=
x
def
inverse_times
(
self
,
*
args
,
**
kwargs
):
return
self
.
x
.
times
(
*
args
,
**
kwargs
)
my_dummy
=
Dummy
(
self
.
D
)
return
my_dummy
@
memo
def
D_inverse_x
(
self
):
return
D
.
inverse_times
(
self
.
position
)
...
...
@@ -82,14 +94,18 @@ if __name__ == "__main__":
x
=
energy
.
position
print
(
iteration
,
((
x
-
ss
).
norm
()
/
ss
.
norm
()).
real
)
minimizer
=
SteepestDescent
(
convergence_tolerance
=
0
,
iteration_limit
=
50
,
callback
=
distance_measure
)
# minimizer = SteepestDescent(convergence_tolerance=0,
# iteration_limit=50,
# callback=distance_measure)
minimizer
=
RelaxedNewton
(
convergence_tolerance
=
0
,
iteration_limit
=
2
,
callback
=
distance_measure
)
minimizer
=
VL_BFGS
(
convergence_tolerance
=
0
,
iteration_limit
=
50
,
callback
=
distance_measure
,
max_history_length
=
3
)
#
minimizer = VL_BFGS(convergence_tolerance=0,
#
iteration_limit=50,
#
callback=distance_measure,
#
max_history_length=3)
m0
=
Field
(
s_space
,
val
=
1
)
...
...
@@ -97,40 +113,35 @@ if __name__ == "__main__":
(
energy
,
convergence
)
=
minimizer
(
energy
)
m
=
energy
.
position
d_data
=
d
.
val
.
get_full_data
().
real
if
rank
==
0
:
pl
.
plot
([
go
.
Heatmap
(
z
=
d_data
)],
filename
=
'data.html'
)
ss_data
=
ss
.
val
.
get_full_data
().
real
if
rank
==
0
:
pl
.
plot
([
go
.
Heatmap
(
z
=
ss_data
)],
filename
=
'ss.html'
)
sh_data
=
sh
.
val
.
get_full_data
().
real
if
rank
==
0
:
pl
.
plot
([
go
.
Heatmap
(
z
=
sh_data
)],
filename
=
'sh.html'
)
j_data
=
j
.
val
.
get_full_data
().
real
if
rank
==
0
:
pl
.
plot
([
go
.
Heatmap
(
z
=
j_data
)],
filename
=
'j.html'
)
jabs_data
=
np
.
abs
(
j
.
val
.
get_full_data
())
jphase_data
=
np
.
angle
(
j
.
val
.
get_full_data
())
if
rank
==
0
:
pl
.
plot
([
go
.
Heatmap
(
z
=
jabs_data
)],
filename
=
'j_abs.html'
)
pl
.
plot
([
go
.
Heatmap
(
z
=
jphase_data
)],
filename
=
'j_phase.html'
)
m_data
=
m
.
val
.
get_full_data
().
real
if
rank
==
0
:
pl
.
plot
([
go
.
Heatmap
(
z
=
m_data
)],
filename
=
'map.html'
)
#
#
#
# grad = gradient(m)
#
# d_data = d.val.get_full_data().real
# if rank == 0:
# pl.plot([go.Heatmap(z=d_data)], filename='data.html')
#
#
# ss_data = ss.val.get_full_data().real
# if rank == 0:
# pl.plot([go.Heatmap(z=ss_data)], filename='ss.html')
#
# sh_data = sh.val.get_full_data().real
# if rank == 0:
# pl.plot([go.Heatmap(z=sh_data)], filename='sh.html')
#
# j_data = j.val.get_full_data().real
# if rank == 0:
# pl.plot([go.Heatmap(z=j_data)], filename='j.html')
#
# jabs_data = np.abs(j.val.get_full_data())
# jphase_data = np.angle(j.val.get_full_data())
# if rank == 0:
# pl.plot([go.Heatmap(z=jabs_data)], filename='j_abs.html')
# pl.plot([go.Heatmap(z=jphase_data)], filename='j_phase.html')
#
# m_data = m.val.get_full_data().real
# if rank == 0:
# pl.plot([go.Heatmap(z=m_data)], filename='map.html')
#
# grad_data = grad.val.get_full_data().real
# if rank == 0:
# pl.plot([go.Heatmap(z=grad_data)], filename='grad.html')
nifty/__init__.py
View file @
f46fca01
...
...
@@ -55,7 +55,7 @@ from spaces import *
from
operators
import
*
#
from probing import *
from
probing
import
*
from
sugar
import
*
...
...
nifty/energies/energy.py
View file @
f46fca01
# -*- coding: utf-8 -*-
from
keepers
import
Loggable
class
Energy
(
object
):
class
Energy
(
Loggable
,
object
):
def
__init__
(
self
,
position
):
self
.
_cache
=
{}
try
:
...
...
nifty/field.py
View file @
f46fca01
...
...
@@ -34,7 +34,10 @@ class Field(Loggable, Versionable, object):
distribution_strategy
=
distribution_strategy
,
val
=
val
)
self
.
set_val
(
new_val
=
val
,
copy
=
copy
)
if
val
is
None
:
self
.
_val
=
None
else
:
self
.
set_val
(
new_val
=
val
,
copy
=
copy
)
def
_parse_domain
(
self
,
domain
,
val
=
None
):
if
domain
is
None
:
...
...
@@ -406,6 +409,9 @@ class Field(Loggable, Versionable, object):
return
self
def
get_val
(
self
,
copy
=
False
):
if
self
.
_val
is
None
:
self
.
set_val
(
None
)
if
copy
:
return
self
.
_val
.
copy
()
else
:
...
...
@@ -413,11 +419,11 @@ class Field(Loggable, Versionable, object):
@
property
def
val
(
self
):
return
self
.
_val
return
self
.
get
_val
(
copy
=
False
)
@
val
.
setter
def
val
(
self
,
new_val
):
self
.
_val
=
self
.
cast
(
new_val
)
self
.
set
_val
(
new_val
=
new_val
,
copy
=
False
)
@
property
def
shape
(
self
):
...
...
nifty/field_types/field_array.py
View file @
f46fca01
...
...
@@ -2,12 +2,14 @@
import
pickle
import
numpy
as
np
from
field_type
import
FieldType
class
FieldArray
(
FieldType
):
def
__init__
(
self
,
dtype
,
shape
):
def
__init__
(
self
,
shape
,
dtype
=
np
.
float
):
try
:
new_shape
=
tuple
([
int
(
i
)
for
i
in
shape
])
except
TypeError
:
...
...
nifty/minimization/__init__.py
View file @
f46fca01
...
...
@@ -5,3 +5,4 @@ from conjugate_gradient import ConjugateGradient
from
quasi_newton_minimizer
import
QuasiNewtonMinimizer
from
steepest_descent
import
SteepestDescent
from
vl_bfgs
import
VL_BFGS
from
relaxed_newton
import
RelaxedNewton
nifty/minimization/relaxed_newton.py
View file @
f46fca01
...
...
@@ -21,8 +21,9 @@ class RelaxedNewton(QuasiNewtonMinimizer):
gradient
=
energy
.
gradient
curvature
=
energy
.
curvature
descend_direction
=
curvature
.
inverse_times
(
gradient
)
norm
=
descend_direction
.
norm
()
if
norm
!=
1
:
return
descend_direction
/
-
norm
else
:
return
descend_direction
*
-
1
return
descend_direction
*
-
1
#norm = descend_direction.norm()
# if norm != 1:
# return descend_direction / -norm
# else:
# return descend_direction * -1
nifty/operators/fft_operator/fft_operator.py
View file @
f46fca01
...
...
@@ -69,7 +69,7 @@ class FFTOperator(LinearOperator):
self
.
_backward_transformation
=
TransformationCache
.
create
(
backward_class
,
self
.
target
[
0
],
self
.
domain
[
0
],
module
=
module
)
def
_times
(
self
,
x
,
spaces
):
def
_times
(
self
,
x
,
spaces
,
dtype
=
None
):
spaces
=
utilities
.
cast_axis_to_tuple
(
spaces
,
len
(
x
.
domain
))
if
spaces
is
None
:
# this case means that x lives on only one space, which is
...
...
@@ -87,12 +87,12 @@ class FFTOperator(LinearOperator):
result_domain
=
list
(
x
.
domain
)
result_domain
[
spaces
[
0
]]
=
self
.
target
[
0
]
result_field
=
x
.
copy_empty
(
domain
=
result_domain
)
result_field
=
x
.
copy_empty
(
domain
=
result_domain
,
dtype
=
dtype
)
result_field
.
set_val
(
new_val
=
new_val
,
copy
=
False
)
return
result_field
def
_inverse_times
(
self
,
x
,
spaces
):
def
_inverse_times
(
self
,
x
,
spaces
,
dtype
=
None
):
spaces
=
utilities
.
cast_axis_to_tuple
(
spaces
,
len
(
x
.
domain
))
if
spaces
is
None
:
# this case means that x lives on only one space, which is
...
...
@@ -110,7 +110,7 @@ class FFTOperator(LinearOperator):
result_domain
=
list
(
x
.
domain
)
result_domain
[
spaces
[
0
]]
=
self
.
domain
[
0
]
result_field
=
x
.
copy_empty
(
domain
=
result_domain
)
result_field
=
x
.
copy_empty
(
domain
=
result_domain
,
dtype
=
dtype
)
result_field
.
set_val
(
new_val
=
new_val
,
copy
=
False
)
return
result_field
...
...
nifty/probing/mixin_classes/__init__.py
View file @
f46fca01
# -*- coding: utf-8 -*-
from
mixin_base
import
MixinBase
from
diagonal_prober_mixin
import
DiagonalProberMixin
from
trace_prober_mixin
import
TraceProberMixin
nifty/probing/mixin_classes/diagonal_prober_mixin.py
View file @
f46fca01
# -*- coding: utf-8 -*-
from
mixin_base
import
MixinBase
class
DiagonalProberMixin
(
MixinBase
):
def
__init__
(
self
):
class
DiagonalProberMixin
(
object
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
reset
()
super
(
DiagonalProberMixin
,
self
).
__init__
()
super
(
DiagonalProberMixin
,
self
).
__init__
(
*
args
,
**
kwargs
)
def
reset
(
self
):
self
.
__sum_of_probings
=
0
...
...
nifty/probing/mixin_classes/mixin_base.py
deleted
100644 → 0
View file @
fd9d341d
# -*- coding: utf-8 -*-
class
MixinBase
(
object
):
def
reset
(
self
,
*
args
,
**
kwargs
):
pass
def
finish_probe
(
self
,
*
args
,
**
kwargs
):
pass
nifty/probing/mixin_classes/trace_prober_mixin.py
View file @
f46fca01
# -*- coding: utf-8 -*-
from
mixin_base
import
MixinBase
class
TraceProberMixin
(
MixinBase
):
def
__init__
(
self
):
class
TraceProberMixin
(
object
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
reset
()
super
(
TraceProberMixin
,
self
).
__init__
()
super
(
TraceProberMixin
,
self
).
__init__
(
*
args
,
**
kwargs
)
def
reset
(
self
):
self
.
__sum_of_probings
=
0
...
...
nifty/probing/prober/prober.py
View file @
f46fca01
...
...
@@ -34,8 +34,6 @@ class Prober(object):
self
.
_random_type
=
self
.
_parse_random_type
(
random_type
)
self
.
compute_variance
=
bool
(
compute_variance
)
super
(
Prober
,
self
).
__init__
()
# ---Properties---
@
property
...
...
@@ -84,7 +82,7 @@ class Prober(object):
self
.
finish_probe
(
current_probe
,
pre_result
)
def
reset
(
self
):
super
(
Prober
,
self
).
reset
()
pass
def
get_probe
(
self
,
index
):
""" layer of abstraction for potential probe-caching """
...
...
@@ -107,7 +105,7 @@ class Prober(object):
return
callee
(
probe
,
**
kwargs
)
def
finish_probe
(
self
,
probe
,
pre_result
):
super
(
Prober
,
self
).
finish_probe
(
probe
,
pre_result
)
pass
def
__call__
(
self
,
callee
):
return
self
.
probing_run
(
callee
)
nifty/spaces/gl_space/gl_space.py
View file @
f46fca01
...
...
@@ -139,8 +139,8 @@ class GLSpace(Space):
if
axes
is
not
None
:
# reshape the weight array to match the input shape
new_shape
=
np
.
ones
(
len
(
x
.
shape
),
dtype
=
np
.
int
)
for
index
in
range
(
len
(
axes
)):
new_shape
[
index
]
=
len
(
weight
)
# we know len(axes) is always 1
new_shape
[
axes
[
0
]
]
=
len
(
weight
)
weight
=
weight
.
reshape
(
new_shape
)
if
inplace
:
...
...
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