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
9a7ac57b
Commit
9a7ac57b
authored
Mar 23, 2020
by
Martin Reinecke
Browse files
make more functions private
parent
34ed0cd4
Pipeline
#71238
passed with stages
in 17 minutes and 7 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
nifty6/minimization/metric_gaussian_kl.py
View file @
9a7ac57b
...
...
@@ -36,7 +36,7 @@ def _shareRange(nwork, nshares, myshare):
return
lo
,
hi
def
np_allreduce_sum
(
comm
,
arr
):
def
_
np_allreduce_sum
(
comm
,
arr
):
if
comm
is
None
:
return
arr
from
mpi4py
import
MPI
...
...
@@ -46,18 +46,18 @@ def np_allreduce_sum(comm, arr):
return
res
def
allreduce_sum_field
(
comm
,
fld
):
def
_
allreduce_sum_field
(
comm
,
fld
):
if
comm
is
None
:
return
fld
if
isinstance
(
fld
,
Field
):
return
Field
(
fld
.
domain
,
np_allreduce_sum
(
fld
.
val
))
return
Field
(
fld
.
domain
,
_
np_allreduce_sum
(
fld
.
val
))
res
=
tuple
(
Field
(
f
.
domain
,
np_allreduce_sum
(
comm
,
f
.
val
))
Field
(
f
.
domain
,
_
np_allreduce_sum
(
comm
,
f
.
val
))
for
f
in
fld
.
values
())
return
MultiField
(
fld
.
domain
,
res
)
class
KLMetric
(
EndomorphicOperator
):
class
_
KLMetric
(
EndomorphicOperator
):
def
__init__
(
self
,
KL
):
self
.
_KL
=
KL
self
.
_capability
=
self
.
TIMES
|
self
.
ADJOINT_TIMES
...
...
@@ -68,7 +68,7 @@ class KLMetric(EndomorphicOperator):
return
self
.
_KL
.
apply_metric
(
x
)
def
draw_sample
(
self
,
from_inverse
=
False
,
dtype
=
np
.
float64
):
return
self
.
_KL
.
metric_sample
(
from_inverse
,
dtype
)
return
self
.
_KL
.
_
metric_sample
(
from_inverse
,
dtype
)
class
MetricGaussianKL
(
Energy
):
...
...
@@ -207,8 +207,8 @@ class MetricGaussianKL(Energy):
else
:
v
+=
tmp
.
val
.
val
g
=
g
+
tmp
.
gradient
self
.
_val
=
np_allreduce_sum
(
self
.
_comm
,
v
)[()]
/
self
.
_n_eff_samples
self
.
_grad
=
allreduce_sum_field
(
self
.
_comm
,
g
)
/
self
.
_n_eff_samples
self
.
_val
=
_
np_allreduce_sum
(
self
.
_comm
,
v
)[()]
/
self
.
_n_eff_samples
self
.
_grad
=
_
allreduce_sum_field
(
self
.
_comm
,
g
)
/
self
.
_n_eff_samples
self
.
_metric
=
None
self
.
_sampdt
=
lh_sampling_dtype
...
...
@@ -239,11 +239,11 @@ class MetricGaussianKL(Energy):
def
apply_metric
(
self
,
x
):
self
.
_get_metric
()
return
allreduce_sum_field
(
self
.
_comm
,
self
.
_metric
(
x
))
return
_
allreduce_sum_field
(
self
.
_comm
,
self
.
_metric
(
x
))
@
property
def
metric
(
self
):
return
KLMetric
(
self
)
return
_
KLMetric
(
self
)
@
property
def
samples
(
self
):
...
...
@@ -256,7 +256,7 @@ class MetricGaussianKL(Energy):
res
=
res
+
tuple
(
-
item
for
item
in
res
)
return
res
def
unscaled_metric_sample
(
self
,
from_inverse
=
False
,
dtype
=
np
.
float64
):
def
_
unscaled_metric_sample
(
self
,
from_inverse
=
False
,
dtype
=
np
.
float64
):
if
from_inverse
:
raise
NotImplementedError
()
lin
=
self
.
_lin
.
with_want_metric
()
...
...
@@ -268,7 +268,7 @@ class MetricGaussianKL(Energy):
if
self
.
_mirror_samples
:
samp
=
samp
+
self
.
_hamiltonian
(
lin
-
v
).
metric
.
draw_sample
(
from_inverse
=
False
,
dtype
=
dtype
)
random
.
pop_sseq
()
return
allreduce_sum_field
(
self
.
_comm
,
samp
)
return
_
allreduce_sum_field
(
self
.
_comm
,
samp
)
def
metric_sample
(
self
,
from_inverse
=
False
,
dtype
=
np
.
float64
):
return
self
.
unscaled_metric_sample
(
from_inverse
,
dtype
)
/
self
.
_n_eff_samples
def
_
metric_sample
(
self
,
from_inverse
=
False
,
dtype
=
np
.
float64
):
return
self
.
_
unscaled_metric_sample
(
from_inverse
,
dtype
)
/
self
.
_n_eff_samples
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