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
Neel Shah
NIFTy
Commits
44d62ab7
Commit
44d62ab7
authored
Jun 11, 2021
by
Philipp Arras
Browse files
Just store the samples
parent
0427c08e
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/minimization/energy_adapter.py
View file @
44d62ab7
...
@@ -107,7 +107,7 @@ class StochasticEnergyAdapter(Energy):
...
@@ -107,7 +107,7 @@ class StochasticEnergyAdapter(Energy):
but rather via the factory function :attr:`make`.
but rather via the factory function :attr:`make`.
"""
"""
def
__init__
(
self
,
position
,
op
,
keys
,
local_ops
,
n_samples
,
comm
,
nanisinf
,
def
__init__
(
self
,
position
,
op
,
keys
,
local_ops
,
n_samples
,
comm
,
nanisinf
,
sampling_context
,
_callingfrommake
=
False
):
noise
,
_callingfrommake
=
False
):
if
not
_callingfrommake
:
if
not
_callingfrommake
:
raise
NotImplementedError
raise
NotImplementedError
super
(
StochasticEnergyAdapter
,
self
).
__init__
(
position
)
super
(
StochasticEnergyAdapter
,
self
).
__init__
(
position
)
...
@@ -127,10 +127,10 @@ class StochasticEnergyAdapter(Energy):
...
@@ -127,10 +127,10 @@ class StochasticEnergyAdapter(Energy):
if
np
.
isnan
(
self
.
_val
)
and
self
.
_nanisinf
:
if
np
.
isnan
(
self
.
_val
)
and
self
.
_nanisinf
:
self
.
_val
=
np
.
inf
self
.
_val
=
np
.
inf
self
.
_grad
=
allreduce_sum
(
g
,
self
.
_comm
)
/
self
.
_n_samples
self
.
_grad
=
allreduce_sum
(
g
,
self
.
_comm
)
/
self
.
_n_samples
self
.
_noise
=
noise
self
.
_op
=
op
self
.
_op
=
op
self
.
_keys
=
keys
self
.
_keys
=
keys
self
.
_context
=
sampling_context
@
property
@
property
def
value
(
self
):
def
value
(
self
):
...
@@ -143,7 +143,7 @@ class StochasticEnergyAdapter(Energy):
...
@@ -143,7 +143,7 @@ class StochasticEnergyAdapter(Energy):
def
at
(
self
,
position
):
def
at
(
self
,
position
):
return
StochasticEnergyAdapter
(
position
,
self
.
_op
,
self
.
_keys
,
return
StochasticEnergyAdapter
(
position
,
self
.
_op
,
self
.
_keys
,
self
.
_local_ops
,
self
.
_n_samples
,
self
.
_comm
,
self
.
_nanisinf
,
self
.
_local_ops
,
self
.
_n_samples
,
self
.
_comm
,
self
.
_nanisinf
,
self
.
_
context
,
_callingfrommake
=
True
)
self
.
_
noise
,
_callingfrommake
=
True
)
def
apply_metric
(
self
,
x
):
def
apply_metric
(
self
,
x
):
lin
=
Linearization
.
make_var
(
self
.
position
,
want_metric
=
True
)
lin
=
Linearization
.
make_var
(
self
.
position
,
want_metric
=
True
)
...
@@ -162,8 +162,8 @@ class StochasticEnergyAdapter(Energy):
...
@@ -162,8 +162,8 @@ class StochasticEnergyAdapter(Energy):
return
StochasticEnergyAdapter
.
make
(
position
,
self
.
_op
,
self
.
_keys
,
return
StochasticEnergyAdapter
.
make
(
position
,
self
.
_op
,
self
.
_keys
,
self
.
_n_samples
,
self
.
_comm
)
self
.
_n_samples
,
self
.
_comm
)
@
class
method
@
static
method
def
make
(
cls
,
position
,
op
,
sampling_keys
,
n_samples
,
mirror_samples
,
def
make
(
position
,
op
,
sampling_keys
,
n_samples
,
mirror_samples
,
comm
=
None
,
nanisinf
=
False
):
comm
=
None
,
nanisinf
=
False
):
"""Factory function for StochasticEnergyAdapter.
"""Factory function for StochasticEnergyAdapter.
...
@@ -202,29 +202,23 @@ class StochasticEnergyAdapter(Energy):
...
@@ -202,29 +202,23 @@ class StochasticEnergyAdapter(Energy):
raise
ValueError
raise
ValueError
samdom
[
k
]
=
op
.
domain
[
k
]
samdom
[
k
]
=
op
.
domain
[
k
]
samdom
=
MultiDomain
.
make
(
samdom
)
samdom
=
MultiDomain
.
make
(
samdom
)
seed
=
int
(
random
.
current_rng
().
integers
(
0
,
1000000
))
context
=
op
,
position
,
seed
,
comm
,
n_samples
,
mirror_samples
,
samdom
noise
=
cls
.
_draw_noise
(
*
context
)
local_ops
=
[
op
.
simplify_for_constant_input
(
nn
)[
1
]
for
nn
in
noise
]
n_samples
=
2
*
n_samples
if
mirror_samples
else
n_samples
return
StochasticEnergyAdapter
(
position
,
op
,
sampling_keys
,
local_ops
,
n_samples
,
comm
,
nanisinf
,
context
,
_callingfrommake
=
True
)
@
staticmethod
def
_draw_noise
(
op
,
position
,
seed
,
comm
,
n_samples
,
mirror_samples
,
sample_domain
):
from
.kl_energies
import
_get_lo_hi
with
random
.
Context
(
seed
):
sseq
=
random
.
spawn_sseq
(
n_samples
)
noise
=
[]
noise
=
[]
sseq
=
random
.
spawn_sseq
(
n_samples
)
from
.kl_energies
import
_get_lo_hi
for
i
in
range
(
*
_get_lo_hi
(
comm
,
n_samples
)):
for
i
in
range
(
*
_get_lo_hi
(
comm
,
n_samples
)):
with
random
.
Context
(
sseq
[
i
]):
with
random
.
Context
(
sseq
[
i
]):
rnd
=
from_random
(
sam
ple_domain
)
rnd
=
from_random
(
sam
dom
)
noise
.
append
(
rnd
)
noise
.
append
(
rnd
)
if
mirror_samples
:
if
mirror_samples
:
noise
.
append
(
-
rnd
)
noise
.
append
(
-
rnd
)
local_ops
=
[]
return
noise
for
nn
in
noise
:
_
,
tmp
=
op
.
simplify_for_constant_input
(
nn
)
myassert
(
tmp
.
domain
==
position
.
domain
)
local_ops
.
append
(
tmp
)
n_samples
=
2
*
n_samples
if
mirror_samples
else
n_samples
return
StochasticEnergyAdapter
(
position
,
op
,
sampling_keys
,
local_ops
,
n_samples
,
comm
,
nanisinf
,
noise
,
_callingfrommake
=
True
)
def
samples
(
self
):
def
samples
(
self
):
"""Standard-normal samples that have been inserted into `op`"""
return
self
.
_noise
return
self
.
_draw_noise
(
*
self
.
_context
)
test/test_kl.py
View file @
44d62ab7
...
@@ -133,7 +133,7 @@ def test_ParametricVI(mirror_samples, fc):
...
@@ -133,7 +133,7 @@ def test_ParametricVI(mirror_samples, fc):
assert_allclose
(
true_val
.
val
,
kl
.
value
,
rtol
=
0.1
)
assert_allclose
(
true_val
.
val
,
kl
.
value
,
rtol
=
0.1
)
samples
=
model
.
KL
.
samples
()
samples
=
model
.
KL
.
samples
()
model
.
minimize
(
ift
.
ADVIOptimizer
(
ift
.
GradientNormController
(
iteration_limit
=
3
))
)
ift
.
random
.
current_rng
().
integers
(
0
,
100
)
samples1
=
model
.
KL
.
samples
()
samples1
=
model
.
KL
.
samples
()
for
aa
,
bb
in
zip
(
samples
,
samples1
):
for
aa
,
bb
in
zip
(
samples
,
samples1
):
ift
.
extra
.
assert_allclose
(
aa
,
bb
)
ift
.
extra
.
assert_allclose
(
aa
,
bb
)
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