-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Turing.jl an optional dep (#36)
* abstract away the extraction of stuff frm from the transitions * make Turing.jl a weakdep or using Requires if not available * update calls to params_and_values and extars * fixed conditional loading and typo in params_and_values * fixed requires usage * breaking change * added some simple tests with Turing * dont run workflow on nightly for all the OSes * fixed actions maybe * only run GH actions upon push to main rather than any branch * nvm * maybe fixed actions * run nightly on ubuntu * nah * okay maybe * updated docs and workflow
- Loading branch information
Showing
9 changed files
with
161 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "TuringCallbacks" | ||
uuid = "ea0860ee-d0ef-45ef-82e6-cc37d6be2f9c" | ||
authors = ["Tor Erlend Fjelde <[email protected]> and contributors"] | ||
version = "0.1.9" | ||
version = "0.2.0" | ||
|
||
[deps] | ||
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" | ||
|
@@ -11,20 +11,24 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | |
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" | ||
OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e" | ||
Reexport = "189a3867-3050-52da-a836-e630ba90ab69" | ||
Requires = "ae029012-a4dd-5104-9daa-d747884805df" | ||
TensorBoardLogger = "899adc3e-224a-11e9-021f-63837185c80f" | ||
|
||
[weakdeps] | ||
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" | ||
|
||
[extensions] | ||
TuringCallbacksTuringExt = "Turing" | ||
|
||
[compat] | ||
DataStructures = "0.18" | ||
DocStringExtensions = "0.8, 0.9" | ||
OnlineStats = "1.5" | ||
Reexport = "0.2, 1.0" | ||
Requires = "1" | ||
TensorBoardLogger = "0.1" | ||
Turing = "0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22" | ||
julia = "1" | ||
|
||
[extras] | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["Test"] | ||
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module TuringCallbacksTuringExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using Turing: Turing | ||
using TuringCallbacks: TuringCallbacks | ||
else | ||
# Requires compatible. | ||
using ..Turing: Turing | ||
using ..TuringCallbacks: TuringCallbacks | ||
end | ||
|
||
const TuringTransition = Union{Turing.Inference.Transition,Turing.Inference.HMCTransition} | ||
|
||
function TuringCallbacks.params_and_values(transition::TuringTransition; kwargs...) | ||
return Iterators.map(zip(Turing.Inference._params_to_array([transition])...)) do (ksym, val) | ||
return string(ksym), val | ||
end | ||
end | ||
|
||
function TuringCallbacks.extras(transition::TuringTransition; kwargs...) | ||
return Iterators.map(zip(Turing.Inference.get_transition_extras([transition])...)) do (ksym, val) | ||
return string(ksym), val | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[deps] | ||
TensorBoardLogger = "899adc3e-224a-11e9-021f-63837185c80f" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" | ||
ValueHistories = "98cad3c8-aec3-5f06-8e41-884608649ab7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
using TuringCallbacks | ||
using Test | ||
using Turing | ||
using TuringCallbacks | ||
using TensorBoardLogger, ValueHistories | ||
|
||
@testset "TuringCallbacks.jl" begin | ||
# Write your tests here. | ||
# TODO: Improve. | ||
@model function demo(x) | ||
s ~ InverseGamma(2, 3) | ||
m ~ Normal(0, √s) | ||
for i in eachindex(x) | ||
x[i] ~ Normal(m, √s) | ||
end | ||
end | ||
|
||
xs = randn(100) .+ 1 | ||
model = demo(xs) | ||
|
||
# Number of MCMC samples/steps | ||
num_samples = 1_000 | ||
num_adapts = 500 | ||
|
||
# Sampling algorithm to use | ||
alg = NUTS(num_adapts, 0.65) | ||
|
||
# Create the callback | ||
callback = TensorBoardCallback(mktempdir()) | ||
|
||
# Sample | ||
chain = sample(model, alg, num_samples; callback=callback) | ||
|
||
# Extract the values. | ||
hist = convert(MVHistory, callback.logger) | ||
|
||
# Compare the recorded values to the chain. | ||
m_mean = last(last(hist["m/stat/Mean"])) | ||
s_mean = last(last(hist["s/stat/Mean"])) | ||
|
||
@test m_mean ≈ mean(chain[:m]) | ||
@test s_mean ≈ mean(chain[:s]) | ||
end |
96af50f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
96af50f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/78374
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: