Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
supr
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mnsc
supr
Commits
1b0d6980
Commit
1b0d6980
authored
3 years ago
by
mnsc
Browse files
Options
Downloads
Patches
Plain Diff
added sampling to regression demo
parent
e3ac4ca9
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
demos/regression.py
+25
-1
25 additions, 1 deletion
demos/regression.py
with
25 additions
and
1 deletion
demos/regression.py
+
25
−
1
View file @
1b0d6980
...
...
@@ -82,7 +82,7 @@ for epoch in range(epochs):
with
torch
.
no_grad
():
# Define prior conditional p(y|x)
Ndx
=
1
sig_prior
=
1
sig_prior
=
.
5
p_y
=
norm
(
0
,
sqrt
(
sig_prior
)).
pdf
(
y_grid
)
# Compute normal approximation
...
...
@@ -102,6 +102,17 @@ for epoch in range(epochs):
idx
=
(
p_predictive
[
k
]
*
np
.
gradient
(
y_grid
))
<
p_sorted
[
i
]
hpr
[
k
,
idx
]
=
False
# Generate posterior conditional samples
y_sample
=
[]
for
x_g
,
p_x_g
in
zip
(
X_grid
,
p_x
):
model
(
x_g
[
None
],
marginalize
=
marginalize_y
)
# Sample from either SPN conditional or background/prior
if
torch
.
rand
(
1
)
<
N
*
p_x_g
/
(
N
*
p_x_g
+
Ndx
):
y_sample
.
append
(
model
.
sample
()[
1
])
else
:
y_sample
.
append
(
torch
.
randn
(
1
)
*
sig_prior
)
y_sample
=
torch
.
tensor
(
y_sample
)
# Plot posterior
plt
.
figure
(
1
).
clf
()
plt
.
title
(
'
Posterior distribution
'
)
...
...
@@ -131,3 +142,16 @@ for epoch in range(epochs):
plt
.
xlim
([
x_min
,
x_max
])
plt
.
ylim
([
y_min
,
y_max
])
drawnow
()
# Plot samples from posterior conditional
plt
.
figure
(
3
).
clf
()
plt
.
title
(
'
Posterior conditional samples
'
)
plt
.
fill_between
(
x_grid
,
m_pred
+
1.96
*
std_pred
,
m_pred
-
1.96
*
std_pred
,
color
=
'
tab:orange
'
,
alpha
=
0.1
)
plt
.
plot
(
x_grid
,
y_sample
,
'
.
'
,
color
=
'
tab:blue
'
,
alpha
=
.
5
,
markersize
=
15
,
markeredgewidth
=
0
)
plt
.
axis
(
'
square
'
)
plt
.
xlim
([
x_min
,
x_max
])
plt
.
ylim
([
y_min
,
y_max
])
drawnow
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment