Skip to content

Commit

Permalink
build based on 1656f7d
Browse files Browse the repository at this point in the history
  • Loading branch information
Documenter.jl committed Oct 2, 2024
1 parent c3b28df commit 9d289f7
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion dev/.documenter-siteinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"documenter":{"julia_version":"1.10.5","generation_timestamp":"2024-10-01T13:26:50","documenter_version":"1.7.0"}}
{"documenter":{"julia_version":"1.10.5","generation_timestamp":"2024-10-02T14:49:36","documenter_version":"1.7.0"}}
2 changes: 1 addition & 1 deletion dev/alternatives/index.html

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions dev/api/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dev/debugging/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dev/examples/autodiff/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@
Enzyme.Duplicated(obs_seq, ∇obs_enzyme),
Enzyme.Duplicated(control_seq, ∇control_enzyme),
Enzyme.Const(seq_ends),
)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">((nothing, nothing, nothing, nothing),)</code></pre><p>Once again we can check the results.</p><pre><code class="language-julia hljs">∇parameters_enzyme ≈ ∇parameters_forwarddiff</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">true</code></pre><pre><code class="language-julia hljs">∇obs_enzyme ≈ ∇obs_forwarddiff</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">true</code></pre><pre><code class="language-julia hljs">∇control_enzyme ≈ ∇control_forwarddiff</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">true</code></pre><p>For increased efficiency, we could provide temporary storage to Enzyme.jl in order to avoid allocations. This requires going one level deeper and leveraging the in-place <a href="../../api/#HiddenMarkovModels.forward!"><code>HiddenMarkovModels.forward!</code></a> function.</p><h2 id="Gradient-methods"><a class="docs-heading-anchor" href="#Gradient-methods">Gradient methods</a><a id="Gradient-methods-1"></a><a class="docs-heading-anchor-permalink" href="#Gradient-methods" title="Permalink"></a></h2><p>Once we have gradients of the loglikelihood, it is a natural idea to perform gradient descent in order to fit the parameters of a custom HMM. However, there are two caveats we must keep in mind.</p><p>First, computing a gradient essentially requires running the forward-backward algorithm, which means it is expensive. Given the output of forward-backward, if there is a way to perform a more accurate parameter update (like going straight to the maximum likelihood value), it is probably worth it. That is what we show in the other tutorials with the reimplementation of the <code>fit!</code> method.</p><p>Second, HMM parameters live in a constrained space, which calls for a projected gradient descent. Most notably, the transition matrix must be stochastic, and the orthogonal projection onto this set (the Birkhoff polytope) is not easy to obtain.</p><p>Still, first order optimization can be relevant when we lack explicit formulas for maximum likelihood.</p><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../controlled/">« Control dependency</a><a class="docs-footer-nextpage" href="../../api/">API reference »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.7.0 on <span class="colophon-date" title="Tuesday 1 October 2024 13:26">Tuesday 1 October 2024</span>. Using Julia version 1.10.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">((nothing, nothing, nothing, nothing),)</code></pre><p>Once again we can check the results.</p><pre><code class="language-julia hljs">∇parameters_enzyme ≈ ∇parameters_forwarddiff</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">true</code></pre><pre><code class="language-julia hljs">∇obs_enzyme ≈ ∇obs_forwarddiff</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">true</code></pre><pre><code class="language-julia hljs">∇control_enzyme ≈ ∇control_forwarddiff</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">true</code></pre><p>For increased efficiency, we could provide temporary storage to Enzyme.jl in order to avoid allocations. This requires going one level deeper and leveraging the in-place <a href="../../api/#HiddenMarkovModels.forward!"><code>HiddenMarkovModels.forward!</code></a> function.</p><h2 id="Gradient-methods"><a class="docs-heading-anchor" href="#Gradient-methods">Gradient methods</a><a id="Gradient-methods-1"></a><a class="docs-heading-anchor-permalink" href="#Gradient-methods" title="Permalink"></a></h2><p>Once we have gradients of the loglikelihood, it is a natural idea to perform gradient descent in order to fit the parameters of a custom HMM. However, there are two caveats we must keep in mind.</p><p>First, computing a gradient essentially requires running the forward-backward algorithm, which means it is expensive. Given the output of forward-backward, if there is a way to perform a more accurate parameter update (like going straight to the maximum likelihood value), it is probably worth it. That is what we show in the other tutorials with the reimplementation of the <code>fit!</code> method.</p><p>Second, HMM parameters live in a constrained space, which calls for a projected gradient descent. Most notably, the transition matrix must be stochastic, and the orthogonal projection onto this set (the Birkhoff polytope) is not easy to obtain.</p><p>Still, first order optimization can be relevant when we lack explicit formulas for maximum likelihood.</p><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../controlled/">« Control dependency</a><a class="docs-footer-nextpage" href="../../api/">API reference »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.7.0 on <span class="colophon-date" title="Wednesday 2 October 2024 14:49">Wednesday 2 October 2024</span>. Using Julia version 1.10.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
2 changes: 1 addition & 1 deletion dev/examples/basics/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@
[-0.502073, -0.800874] [-0.5, -0.8]
[0.504019, 0.801399] [0.5, 0.8]</code></pre><pre><code class="language-julia hljs">hcat(initialization(hmm_est_concat), initialization(hmm))</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">2×2 Matrix{Float64}:
0.605645 0.6
0.394355 0.4</code></pre><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../../">« Home</a><a class="docs-footer-nextpage" href="../types/">Types »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.7.0 on <span class="colophon-date" title="Tuesday 1 October 2024 13:26">Tuesday 1 October 2024</span>. Using Julia version 1.10.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
0.394355 0.4</code></pre><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../../">« Home</a><a class="docs-footer-nextpage" href="../types/">Types »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.7.0 on <span class="colophon-date" title="Wednesday 2 October 2024 14:49">Wednesday 2 October 2024</span>. Using Julia version 1.10.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
20 changes: 10 additions & 10 deletions dev/examples/controlled/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

obs_seq = reduce(vcat, obs_seqs)
control_seq = reduce(vcat, control_seqs)
seq_ends = cumsum(length.(obs_seqs));</code></pre><h2 id="Inference"><a class="docs-heading-anchor" href="#Inference">Inference</a><a id="Inference-1"></a><a class="docs-heading-anchor-permalink" href="#Inference" title="Permalink"></a></h2><p>Not much changes from the case with simple time dependency.</p><pre><code class="language-julia hljs">best_state_seq, _ = viterbi(hmm, obs_seq, control_seq; seq_ends)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">([2, 2, 2, 2, 1, 1, 1, 2, 2, 2 … 2, 2, 2, 1, 1, 1, 1, 1, 1, 1], [-282.9758167143853, -276.00671751411386, -371.24181781000067, -307.61823249622614, -367.83170808085697, -202.3308642947108, -329.55893436574945, -262.873633152325, -193.4782294384391, -324.22142538789774 … -237.18347666459772, -196.9444524847919, -376.0529887946711, -321.37826172211396, -345.20244256849907, -324.6386344081525, -367.2170181263724, -298.71453741912063, -229.71806938636718, -230.7122097211064])</code></pre><h2 id="Learning"><a class="docs-heading-anchor" href="#Learning">Learning</a><a id="Learning-1"></a><a class="docs-heading-anchor-permalink" href="#Learning" title="Permalink"></a></h2><p>Once more, we override the <code>fit!</code> function. The state-related parameters are estimated in the standard way. Meanwhile, the observation coefficients are given by the formula for <a href="https://en.wikipedia.org/wiki/Weighted_least_squares">weighted least squares</a>.</p><pre><code class="language-julia hljs">function StatsAPI.fit!(
seq_ends = cumsum(length.(obs_seqs));</code></pre><h2 id="Inference"><a class="docs-heading-anchor" href="#Inference">Inference</a><a id="Inference-1"></a><a class="docs-heading-anchor-permalink" href="#Inference" title="Permalink"></a></h2><p>Not much changes from the case with simple time dependency.</p><pre><code class="language-julia hljs">best_state_seq, _ = viterbi(hmm, obs_seq, control_seq; seq_ends)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">([2, 2, 2, 2, 2, 2, 2, 2, 1, 1 … 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [-243.47578071963778, -243.7258443363213, -293.61410741699393, -372.320204771554, -286.23192478901007, -264.81234427932316, -309.2776330038967, -188.87062693872687, -208.62871051873805, -307.15126274384835 … -201.06121562723416, -229.30361431996292, -236.47049487303286, -241.43104230988718, -266.85578390759633, -252.13022802729324, -276.3975224310904, -229.73411678091708, -330.29748775240625, -232.3306522657181])</code></pre><h2 id="Learning"><a class="docs-heading-anchor" href="#Learning">Learning</a><a id="Learning-1"></a><a class="docs-heading-anchor-permalink" href="#Learning" title="Permalink"></a></h2><p>Once more, we override the <code>fit!</code> function. The state-related parameters are estimated in the standard way. Meanwhile, the observation coefficients are given by the formula for <a href="https://en.wikipedia.org/wiki/Weighted_least_squares">weighted least squares</a>.</p><pre><code class="language-julia hljs">function StatsAPI.fit!(
hmm::ControlledGaussianHMM{T},
fb_storage::HMMs.ForwardBackwardStorage,
obs_seq::AbstractVector,
Expand Down Expand Up @@ -60,17 +60,17 @@
trans_guess = [0.6 0.4; 0.3 0.7]
dist_coeffs_guess = [-1.1 * ones(d), 1.1 * ones(d)]
hmm_guess = ControlledGaussianHMM(init_guess, trans_guess, dist_coeffs_guess);</code></pre><pre><code class="language-julia hljs">hmm_est, loglikelihood_evolution = baum_welch(hmm_guess, obs_seq, control_seq; seq_ends)
first(loglikelihood_evolution), last(loglikelihood_evolution)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">(-264727.24942514714, -261198.93867245756)</code></pre><p>How did we perform?</p><pre><code class="language-julia hljs">cat(hmm_est.trans, hmm.trans; dims=3)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">2×2×2 Array{Float64, 3}:
first(loglikelihood_evolution), last(loglikelihood_evolution)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">(-261835.27875217877, -258166.76081115034)</code></pre><p>How did we perform?</p><pre><code class="language-julia hljs">cat(hmm_est.trans, hmm.trans; dims=3)</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">2×2×2 Array{Float64, 3}:
[:, :, 1] =
0.70373 0.29627
0.201148 0.798852
0.700141 0.299859
0.199214 0.800786

[:, :, 2] =
0.7 0.3
0.2 0.8</code></pre><pre><code class="language-julia hljs">hcat(hmm_est.dist_coeffs[1], hmm.dist_coeffs[1])</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">3×2 Matrix{Float64}:
-0.998469 -1.0
-1.00476 -1.0
-1.00124 -1.0</code></pre><pre><code class="language-julia hljs">hcat(hmm_est.dist_coeffs[2], hmm.dist_coeffs[2])</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">3×2 Matrix{Float64}:
1.00392 1.0
0.993359 1.0
1.00449 1.0</code></pre><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../temporal/">« Time dependency</a><a class="docs-footer-nextpage" href="../autodiff/">Autodiff »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.7.0 on <span class="colophon-date" title="Tuesday 1 October 2024 13:26">Tuesday 1 October 2024</span>. Using Julia version 1.10.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
-0.998111 -1.0
-0.99225 -1.0
-1.00328 -1.0</code></pre><pre><code class="language-julia hljs">hcat(hmm_est.dist_coeffs[2], hmm.dist_coeffs[2])</code></pre><pre class="documenter-example-output"><code class="nohighlight hljs ansi">3×2 Matrix{Float64}:
0.999531 1.0
0.995733 1.0
0.997331 1.0</code></pre><hr/><p><em>This page was generated using <a href="https://github.com/fredrikekre/Literate.jl">Literate.jl</a>.</em></p></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../temporal/">« Time dependency</a><a class="docs-footer-nextpage" href="../autodiff/">Autodiff »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.7.0 on <span class="colophon-date" title="Wednesday 2 October 2024 14:49">Wednesday 2 October 2024</span>. Using Julia version 1.10.5.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html>
Loading

0 comments on commit 9d289f7

Please sign in to comment.