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
5572575f
Commit
5572575f
authored
Jul 28, 2018
by
Martin Reinecke
Browse files
adjust polynomial fitting demo
parent
d32f675d
Changes
1
Hide whitespace changes
Inline
Side-by-side
demos/polynomial_fit.py
View file @
5572575f
...
...
@@ -12,14 +12,14 @@ def polynomial(coefficients, sampling_points):
Parameters
----------
coefficients:
Mod
el
coefficients:
Fi
el
d
sampling_points: Numpy array
"""
if
not
(
isinstance
(
coefficients
,
ift
.
Mod
el
)
if
not
(
isinstance
(
coefficients
,
ift
.
Fi
el
d
)
and
isinstance
(
sampling_points
,
np
.
ndarray
)):
raise
TypeError
params
=
coefficients
.
value
.
to_global_data
()
params
=
coefficients
.
to_global_data
()
out
=
np
.
zeros_like
(
sampling_points
)
for
ii
in
range
(
len
(
params
)):
out
+=
params
[
ii
]
*
sampling_points
**
ii
...
...
@@ -88,8 +88,7 @@ y[5] -= 0
# Set up minimization problem
p_space
=
ift
.
UnstructuredDomain
(
N_params
)
params
=
ift
.
Variable
(
ift
.
MultiField
.
from_dict
(
{
'params'
:
ift
.
full
(
p_space
,
0.
)}))[
'params'
]
params
=
ift
.
full
(
p_space
,
0.
)
R
=
PolynomialResponse
(
p_space
,
x
)
ift
.
extra
.
consistency_check
(
R
)
...
...
@@ -98,7 +97,9 @@ d = ift.from_global_data(d_space, y)
N
=
ift
.
DiagonalOperator
(
ift
.
from_global_data
(
d_space
,
var
))
IC
=
ift
.
GradientNormController
(
tol_abs_gradnorm
=
1e-8
)
H
=
ift
.
Hamiltonian
(
ift
.
GaussianEnergy
(
R
(
params
),
d
,
N
),
IC
)
likelihood
=
lambda
inp
:
ift
.
GaussianEnergy
(
d
,
N
)(
R
(
inp
))
H
=
ift
.
Hamiltonian
(
likelihood
,
IC
)
H
=
ift
.
EnergyAdapter
(
params
,
H
)
H
=
H
.
make_invertible
(
IC
)
# Minimize
...
...
@@ -116,13 +117,13 @@ xs = np.linspace(xmin, xmax, 100)
sc
=
ift
.
StatCalculator
()
for
ii
in
range
(
len
(
samples
)):
sc
.
add
(
params
.
at
(
samples
[
ii
])
.
value
)
ys
=
polynomial
(
params
.
at
(
samples
[
ii
]
)
,
xs
)
sc
.
add
(
samples
[
ii
])
ys
=
polynomial
(
samples
[
ii
],
xs
)
if
ii
==
0
:
plt
.
plot
(
xs
,
ys
,
'k'
,
alpha
=
.
05
,
label
=
'Posterior samples'
)
continue
plt
.
plot
(
xs
,
ys
,
'k'
,
alpha
=
.
05
)
ys
=
polynomial
(
params
.
at
(
H
.
position
)
,
xs
)
ys
=
polynomial
(
H
.
position
,
xs
)
plt
.
plot
(
xs
,
ys
,
'r'
,
linewidth
=
2.
,
label
=
'Interpolation'
)
plt
.
legend
()
plt
.
savefig
(
'fit.png'
)
...
...
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