Raster-compiled log densities as foerster blocks (doc/contract.md).
A raster block is a deftm log density over typed arguments. The block
says how θ and the site's inputs become those arguments, and which argument
slots hold θ:
(raster-block {:block/id :gauss :block/latents [{:name :mu :shape [2] :support :real}] :block/target :complete-conditional} #'gauss-lp {:args (fn [^doubles theta {:keys [ys n s0 s]}] [(aget theta 0) (aget theta 1) ys n s0 s]) :theta [0 1]})
:theta lists, in θ order, the argument slots θ occupies: a scalar slot
contributes one coordinate, an array slot as many as its gradient has.
The block's :log-density calls the compiled function; its :value+grad
calls raster's reverse mode (raster.ad.reverse/value+grad) with respect
to the θ slots only, built once per block: the other arguments (the data)
stay constant. Raster owns compilation and caching; this namespace only
binds arguments.
Raster-compiled log densities as foerster blocks (doc/contract.md).
A raster block is a `deftm` log density over typed arguments. The block
says how θ and the site's inputs become those arguments, and which argument
slots hold θ:
(raster-block {:block/id :gauss
:block/latents [{:name :mu :shape [2] :support :real}]
:block/target :complete-conditional}
#'gauss-lp
{:args (fn [^doubles theta {:keys [ys n s0 s]}]
[(aget theta 0) (aget theta 1) ys n s0 s])
:theta [0 1]})
`:theta` lists, in θ order, the argument slots θ occupies: a scalar slot
contributes one coordinate, an array slot as many as its gradient has.
The block's `:log-density` calls the compiled function; its `:value+grad`
calls raster's reverse mode (`raster.ad.reverse/value+grad`) with respect
to the θ slots only, built once per block: the other arguments (the data)
stay constant. Raster owns compilation and caching; this namespace only
binds arguments.(defdensity name latents data & body)Define a raster block from its log density, written as the body of a
deftm: latents are the block's latents in θ order — a scalar b, a
constrained scalar [b support] (support :positive or [:interval a b]),
a vector [beta [k]] or a constrained vector [beta [k] support] (the
body sees it as an (Array double) of length k) — and data the other
arguments with their raster types. Defines name as the block and
name-lp as the compiled density; the gradient with respect to the
latents comes from raster's reverse mode. The site's inputs are a map
keyed by the data arguments' names:
(defdensity logreg [b0 [b [3]]] [xs :- (Array double), ys :- (Array double), cnt :- Long] (loop … acc))
(sample (block/block-dist logreg {:xs xs :ys ys :cnt n}) :id :beta :init [0.0 0.0 0.0 0.0])
Inside a loop, read a vector latent at the loop's index ((ra/aget beta i)); bind an element read at a fixed index before the loop ((let [b1 (ra/aget beta 0)] (loop …))) — raster's reverse mode differentiates a
loop's reads of an active array only at the loop index.
θ is the latents flattened in order. With a constrained latent the body
sees it in natural coordinates (σ, not log σ): the block declares
:block/coordinates :constrained and foerster transforms.
Define a raster block from its log density, written as the body of a
`deftm`: `latents` are the block's latents in θ order — a scalar `b`, a
constrained scalar `[b support]` (support `:positive` or [:interval a b]),
a vector `[beta [k]]` or a constrained vector `[beta [k] support]` (the
body sees it as an `(Array double)` of length k) — and `data` the other
arguments with their raster types. Defines `name` as the block and
`name-lp` as the compiled density; the gradient with respect to the
latents comes from raster's reverse mode. The site's inputs are a map
keyed by the data arguments' names:
(defdensity logreg [b0 [b [3]]]
[xs :- (Array double), ys :- (Array double), cnt :- Long]
(loop … acc))
(sample (block/block-dist logreg {:xs xs :ys ys :cnt n}) :id :beta :init [0.0 0.0 0.0 0.0])
Inside a loop, read a vector latent at the loop's index (`(ra/aget beta
i)`); bind an element read at a fixed index before the loop (`(let [b1
(ra/aget beta 0)] (loop …))`) — raster's reverse mode differentiates a
loop's reads of an active array only at the loop index.
θ is the latents flattened in order. With a constrained latent the body
sees it in natural coordinates (σ, not log σ): the block declares
`:block/coordinates :constrained` and foerster transforms.(raster-block description lp-var {:keys [args theta]})A foerster block whose log density is the raster function lp-var. See
the namespace for binding: {:args (fn [theta inputs]) :theta [slot …]}.
A foerster block whose log density is the raster function `lp-var`. See
the namespace for `binding`: {:args (fn [theta inputs]) :theta [slot …]}.cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |