Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Bayesian Statistical Learning 2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
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
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
Alina Bazarova
Bayesian Statistical Learning 2
Commits
49cfb457
Commit
49cfb457
authored
3 months ago
by
Steve Schmerler
Browse files
Options
Downloads
Patches
Plain Diff
utils: make extract_model_params handle more tensors
parent
279916d3
No related branches found
No related tags found
1 merge request
!2
Update GP slides and notebooks
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
BLcourse2.3/utils.py
+19
-3
19 additions, 3 deletions
BLcourse2.3/utils.py
with
19 additions
and
3 deletions
BLcourse2.3/utils.py
+
19
−
3
View file @
49cfb457
from
matplotlib
import
pyplot
as
plt
from
matplotlib
import
pyplot
as
plt
import
torch
def
extract_model_params
(
model
,
raw
=
False
)
->
dict
:
def
extract_model_params
(
model
,
raw
=
False
,
try_item
=
True
)
->
dict
:
"""
Helper to convert model.named_parameters() to dict.
"""
Helper to convert model.named_parameters() to dict.
With raw=True, use
With raw=True, use
...
@@ -11,9 +12,24 @@ def extract_model_params(model, raw=False) -> dict:
...
@@ -11,9 +12,24 @@ def extract_model_params(model, raw=False) -> dict:
See https://docs.gpytorch.ai/en/stable/examples/00_Basic_Usage/Hyperparameters.html#Raw-vs-Actual-Parameters
See https://docs.gpytorch.ai/en/stable/examples/00_Basic_Usage/Hyperparameters.html#Raw-vs-Actual-Parameters
"""
"""
if
try_item
:
def
get_val
(
p
):
if
isinstance
(
p
,
torch
.
Tensor
):
if
p
.
ndim
==
0
:
return
p
.
item
()
else
:
p_sq
=
p
.
squeeze
()
if
(
p_sq
.
ndim
==
1
and
len
(
p_sq
)
==
1
)
or
p_sq
.
ndim
==
0
:
return
p_sq
.
item
()
else
:
return
p
else
:
return
p
else
:
get_val
=
lambda
p
:
p
if
raw
:
if
raw
:
return
dict
(
return
dict
(
(
p_name
,
p
_val
.
item
(
))
(
p_name
,
get
_val
(
p_val
))
for
p_name
,
p_val
in
model
.
named_parameters
()
for
p_name
,
p_val
in
model
.
named_parameters
()
)
)
else
:
else
:
...
@@ -24,7 +40,7 @@ def extract_model_params(model, raw=False) -> dict:
...
@@ -24,7 +40,7 @@ def extract_model_params(model, raw=False) -> dict:
# Yes, eval() hack. Sorry.
# Yes, eval() hack. Sorry.
p_name
=
p_name
.
replace
(
"
.raw_
"
,
"
.
"
)
p_name
=
p_name
.
replace
(
"
.raw_
"
,
"
.
"
)
p_val
=
eval
(
f
"
model.
{
p_name
}
"
)
p_val
=
eval
(
f
"
model.
{
p_name
}
"
)
out
[
p_name
]
=
p
_val
.
item
(
)
out
[
p_name
]
=
get
_val
(
p_val
)
return
out
return
out
...
...
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
sign in
to comment