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
Neel Shah
NIFTy
Commits
59a009cb
Commit
59a009cb
authored
Jun 01, 2021
by
Philipp Arras
Browse files
Tweak docs
parent
0d3e909a
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/library/variational_models.py
View file @
59a009cb
...
@@ -33,14 +33,15 @@ from ..sugar import domain_union, from_random, full, makeField
...
@@ -33,14 +33,15 @@ from ..sugar import domain_union, from_random, full, makeField
class
MeanfieldModel
():
class
MeanfieldModel
():
'''
"""Collect the operators required for Gaussian mean-field variational
Collects the operators required for Gaussian mean-field variational
inference.
inference.
Parameters
Parameters
----------
----------
domain: MultiDomain
domain: MultiDomain
The domain of the model parameters.
The domain of the model parameters.
'''
"""
def
__init__
(
self
,
domain
):
def
__init__
(
self
,
domain
):
self
.
domain
=
MultiDomain
.
make
(
domain
)
self
.
domain
=
MultiDomain
.
make
(
domain
)
self
.
Flat
=
Multifield2Vector
(
self
.
domain
)
self
.
Flat
=
Multifield2Vector
(
self
.
domain
)
...
@@ -52,17 +53,18 @@ class MeanfieldModel():
...
@@ -52,17 +53,18 @@ class MeanfieldModel():
self
.
entropy
=
GaussianEntropy
(
self
.
std
.
target
)
@
self
.
std
self
.
entropy
=
GaussianEntropy
(
self
.
std
.
target
)
@
self
.
std
def
get_initial_pos
(
self
,
initial_mean
=
None
,
initial_sig
=
1
):
def
get_initial_pos
(
self
,
initial_mean
=
None
,
initial_sig
=
1
):
'''
"""Provide an initial position for a given mean parameter vector and an
Provides an initial position for a given mean parameter vector and an
initial standard deviation.
initial standard deviation.
Parameters
Parameters
----------
----------
initial_mean: MultiField
initial_mean: MultiField
The initial mean of the variational approximation. If not None, a Gaussian sample with mean zero and standard deviation of 0.1 is used.
The initial mean of the variational approximation. If not None, a
Default: None
Gaussian sample with mean zero and standard deviation of 0.1 is
used. Default: None
initial_sig: positive float
initial_sig: positive float
The initial standard deviation shared by all parameters. Default: 1
The initial standard deviation shared by all parameters. Default: 1
'''
"""
initial_pos
=
from_random
(
self
.
generator
.
domain
).
to_dict
()
initial_pos
=
from_random
(
self
.
generator
.
domain
).
to_dict
()
initial_pos
[
'latent'
]
=
full
(
self
.
generator
.
domain
[
'latent'
],
0.
)
initial_pos
[
'latent'
]
=
full
(
self
.
generator
.
domain
[
'latent'
],
0.
)
...
@@ -76,14 +78,15 @@ class MeanfieldModel():
...
@@ -76,14 +78,15 @@ class MeanfieldModel():
class
FullCovarianceModel
():
class
FullCovarianceModel
():
'''
"""Collect the operators required for Gaussian full-covariance variational
Collects the operators required for Gaussian full-covariance variational
inference.
inference.
Parameters
Parameters
----------
----------
domain: MultiDomain
domain: MultiDomain
The domain of the model parameters.
The domain of the model parameters.
'''
"""
def
__init__
(
self
,
domain
):
def
__init__
(
self
,
domain
):
self
.
domain
=
MultiDomain
.
make
(
domain
)
self
.
domain
=
MultiDomain
.
make
(
domain
)
self
.
Flat
=
Multifield2Vector
(
self
.
domain
)
self
.
Flat
=
Multifield2Vector
(
self
.
domain
)
...
@@ -108,23 +111,24 @@ class FullCovarianceModel():
...
@@ -108,23 +111,24 @@ class FullCovarianceModel():
Resp
=
Respacer
(
MatMult
.
target
,
mean
.
target
)
Resp
=
Respacer
(
MatMult
.
target
,
mean
.
target
)
self
.
generator
=
self
.
Flat
.
adjoint
@
(
mean
+
Resp
@
MatMult
@
matmul_setup
)
self
.
generator
=
self
.
Flat
.
adjoint
@
(
mean
+
Resp
@
MatMult
@
matmul_setup
)
Diag
=
DiagonalSelector
(
cov
.
target
,
self
.
Flat
.
target
)
Diag
=
DiagonalSelector
(
cov
.
target
,
self
.
Flat
.
target
)
diag_cov
=
Diag
(
cov
).
absolute
()
diag_cov
=
Diag
(
cov
).
absolute
()
self
.
entropy
=
GaussianEntropy
(
diag_cov
.
target
)
@
diag_cov
self
.
entropy
=
GaussianEntropy
(
diag_cov
.
target
)
@
diag_cov
def
get_initial_pos
(
self
,
initial_mean
=
None
,
initial_sig
=
1
):
def
get_initial_pos
(
self
,
initial_mean
=
None
,
initial_sig
=
1
):
'''
"""Provide an initial position for a given mean parameter vector and a
Provides an initial position for a given mean parameter vector and a
diagonal covariance with an initial standard deviation.
diagonal covariance with an initial standard deviation.
Parameters
Parameters
----------
----------
initial_mean: MultiField
initial_mean: MultiField
The initial mean of the variational approximation. If not None, a Gaussian sample with mean zero and standard deviation of 0.1 is used.
The initial mean of the variational approximation. If not None, a
Default: None
Gaussian sample with mean zero and standard deviation of 0.1 is
used. Default: None
initial_sig: positive float
initial_sig: positive float
The initial standard deviation shared by all parameters. Default: 1
The initial standard deviation shared by all parameters. Default: 1
'''
"""
initial_pos
=
from_random
(
self
.
generator
.
domain
).
to_dict
()
initial_pos
=
from_random
(
self
.
generator
.
domain
).
to_dict
()
initial_pos
[
'latent'
]
=
full
(
self
.
generator
.
domain
[
'latent'
],
0.
)
initial_pos
[
'latent'
]
=
full
(
self
.
generator
.
domain
[
'latent'
],
0.
)
diag_tri
=
np
.
diag
(
np
.
full
(
self
.
flat_domain
.
shape
[
0
],
initial_sig
))[
np
.
tril_indices
(
self
.
flat_domain
.
shape
[
0
])]
diag_tri
=
np
.
diag
(
np
.
full
(
self
.
flat_domain
.
shape
[
0
],
initial_sig
))[
np
.
tril_indices
(
self
.
flat_domain
.
shape
[
0
])]
...
@@ -136,14 +140,15 @@ class FullCovarianceModel():
...
@@ -136,14 +140,15 @@ class FullCovarianceModel():
class
GaussianEntropy
(
EnergyOperator
):
class
GaussianEntropy
(
EnergyOperator
):
'''
"""Calculate the entropy of a Gaussian distribution given the diagonal of a
Calculates the entropy of a Gaussian distribution given the diagonal of a
triangular decomposition of the covariance.
triangular decomposition of the covariance.
Parameters
Parameters
----------
----------
domain: Domain
domain: Domain
The domain of the diagonal.
The domain of the diagonal.
'''
"""
def
__init__
(
self
,
domain
):
def
__init__
(
self
,
domain
):
self
.
_domain
=
domain
self
.
_domain
=
domain
...
@@ -159,16 +164,17 @@ class GaussianEntropy(EnergyOperator):
...
@@ -159,16 +164,17 @@ class GaussianEntropy(EnergyOperator):
class
LowerTriangularProjector
(
LinearOperator
):
class
LowerTriangularProjector
(
LinearOperator
):
'''
"""Project the DOFs of a triangular matrix into the matrix form.
Projects the DOFs of a triangular matrix into the matrix form.
Parameters
Parameters
----------
----------
domain: Domain
domain: Domain
A one-dimensional domain containing N(N+1)/2 DOFs of a triangular matrix.
A one-dimensional domain containing N(N+1)/2 DOFs of a triangular
matrix.
target: Domain
target: Domain
A two-dimensional domain with NxN entries.
A two-dimensional domain with NxN entries.
'''
"""
def
__init__
(
self
,
domain
,
target
):
def
__init__
(
self
,
domain
,
target
):
self
.
_domain
=
DomainTuple
.
make
(
domain
)
self
.
_domain
=
DomainTuple
.
make
(
domain
)
self
.
_target
=
DomainTuple
.
make
(
target
)
self
.
_target
=
DomainTuple
.
make
(
target
)
...
@@ -187,16 +193,17 @@ class LowerTriangularProjector(LinearOperator):
...
@@ -187,16 +193,17 @@ class LowerTriangularProjector(LinearOperator):
class
DiagonalSelector
(
LinearOperator
):
class
DiagonalSelector
(
LinearOperator
):
'''
"""Extract the diagonal of a two-dimensional field.
Extracts the diagonal of a two-dimensional field.
Parameters
Parameters
----------
----------
domain: Domain
domain: Domain
The two-dimensional domain of the input field
The two-dimensional domain of the input field
target: Domain
target: Domain
A one-dimensional domain in which the diagonal of the input field lives.
The one-dimensional domain on which the diagonal of the input field is
'''
defined.
"""
def
__init__
(
self
,
domain
,
target
):
def
__init__
(
self
,
domain
,
target
):
self
.
_domain
=
DomainTuple
.
make
(
domain
)
self
.
_domain
=
DomainTuple
.
make
(
domain
)
self
.
_target
=
DomainTuple
.
make
(
target
)
self
.
_target
=
DomainTuple
.
make
(
target
)
...
@@ -211,16 +218,16 @@ class DiagonalSelector(LinearOperator):
...
@@ -211,16 +218,16 @@ class DiagonalSelector(LinearOperator):
class
Respacer
(
LinearOperator
):
class
Respacer
(
LinearOperator
):
'''
"""Re-map a field from one domain to another one with the same amounts of
Re-maps a field from one domain to another one with the same amounts of
DOFs. Wrapps the numpy.reshape method.
DOFs. Wrapps the numpy.reshape method.
Parameters
Parameters
----------
----------
domain: Domain
domain: Domain
The domain of the input field.
The domain of the input field.
target: Domain
target: Domain
The domain of the output field.
The domain of the output field.
'''
"""
def
__init__
(
self
,
domain
,
target
):
def
__init__
(
self
,
domain
,
target
):
self
.
_domain
=
DomainTuple
.
make
(
domain
)
self
.
_domain
=
DomainTuple
.
make
(
domain
)
...
...
src/minimization/stochastic_minimizer.py
View file @
59a009cb
...
@@ -19,26 +19,25 @@ from .minimizer import Minimizer
...
@@ -19,26 +19,25 @@ from .minimizer import Minimizer
class
ADVIOptimizer
(
Minimizer
):
class
ADVIOptimizer
(
Minimizer
):
'''
"""Provide an implementation of an adaptive step-size sequence optimizer,
Provides an implementation of an adaptive step-size sequence optimizer,
following https://arxiv.org/abs/1603.00788.
following https://arxiv.org/abs/1603.00788.
Parameters
Parameters
----------
----------
steps: int
steps: int
The number of concecutive steps during one call of the optimizer.
The number of concecutive steps during one call of the optimizer.
eta: positive float
eta: positive float
The scale of the step-size sequence. It might have to be adapted to the application to increase performance. Default: 1.
The scale of the step-size sequence. It might have to be adapted to the
application to increase performance. Default: 1.
alpha: float between 0 and 1
alpha: float between 0 and 1
The fraction of how much the current gradient impacts the momentum.
The fraction of how much the current gradient impacts the momentum.
tau: positive float
tau: positive float
This quantity prevents division by zero.
This quantity prevents division by zero.
epsilon: positive float
epsilon: positive float
A small value guarantees Robbins and Monro conditions.
A small value guarantees Robbins and Monro conditions.
"""
'''
def
__init__
(
self
,
steps
,
eta
=
1
,
alpha
=
0.1
,
tau
=
1
,
epsilon
=
1e-16
):
def
__init__
(
self
,
steps
,
eta
=
1
,
alpha
=
0.1
,
tau
=
1
,
epsilon
=
1e-16
):
self
.
alpha
=
alpha
self
.
alpha
=
alpha
self
.
eta
=
eta
self
.
eta
=
eta
self
.
tau
=
tau
self
.
tau
=
tau
...
@@ -59,15 +58,6 @@ class ADVIOptimizer(Minimizer):
...
@@ -59,15 +58,6 @@ class ADVIOptimizer(Minimizer):
return
new_position
return
new_position
def
__call__
(
self
,
E
):
def
__call__
(
self
,
E
):
'''
Performs the optimization.
Parameters
----------
E: EnergyOperator
The target function.
'''
from
..minimization.parametric_gaussian_kl
import
ParametricGaussianKL
from
..minimization.parametric_gaussian_kl
import
ParametricGaussianKL
if
self
.
s
is
None
:
if
self
.
s
is
None
:
self
.
s
=
E
.
gradient
**
2
self
.
s
=
E
.
gradient
**
2
...
...
Write
Preview
Markdown
is supported
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