https://github.com/todesking/platebuilder
Typesafe plate notation builder
https://github.com/todesking/platebuilder
Last synced: 10 months ago
JSON representation
Typesafe plate notation builder
- Host: GitHub
- URL: https://github.com/todesking/platebuilder
- Owner: todesking
- License: mit
- Created: 2017-01-18T10:25:37.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-04T09:53:57.000Z (almost 9 years ago)
- Last Synced: 2025-03-02T09:45:08.853Z (over 1 year ago)
- Language: Scala
- Size: 223 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Platebuilder: Typesafe plate notation (graphical model) builder
Typesafe Scala DSL to build graphical model.

```scala
// example/src/main/scala/Demo.scala
object Demo {
object Unigram extends PlateBuilder {
import builder.dsl._
val V = size.V("Number of vocabularies")
val D = size.D("Number of documents")
val N = size.N("Number of words for each documents") * D
// hyperparameters
val beta = given.beta.realVec(V)
// variables
val phi = hidden.phi.realVec(V)
val w = observed.w.category(V) * (D, N)
phi ~ dirichlet(beta)
for (d <- D) {
for (n <- N(d)) {
w(d, n) ~ categorical(phi)
}
}
}
val MixtureOfUnigrams = Model.define("MixtureOfUnigrams") { implicit builder =>
import builder.dsl._
val K = size.K("Number of topics")
val V = size.V("Number of vocabularies")
val D = size.D("Number of documents")
val N = size.N("Number of words for each documents") * D
// hyperparameters
val alpha = given.alpha.realVec(K)
val beta = given.beta.realVec(V)
// variables
val phi = hidden.phi.realVec(V) * K
val theta = hidden.theta.realVec(K)
val z = hidden("z", "Hidden topic for each document").category(K) * D
val w = observed.w.category(V) * (D, N)
for (k <- K) {
phi(k) ~ dirichlet(beta)
}
theta ~ dirichlet(alpha)
for (d <- D) {
z(d) ~ categorical(theta)
for (n <- N(d)) {
w(d, n) ~ categorical(phi(z(d)))
}
}
}
val LDA = Model.define("LDA") { implicit builder =>
import builder.dsl._
val K = size.K("Number of topics")
val V = size.V("Number of vocabularies")
val D = size.D("Number of documents")
val N = size.N("Number of words for each documents") * D
// hyperparameters
val alpha = given.alpha.realVec(K)
val beta = given.beta.realVec(V)
// variables
val phi = hidden.phi.realVec(V) * K
val theta = hidden.theta.realVec(K) * D
val z = hidden("z", "Hidden topic for each word").category(K) * (D, N)
val w = observed.w.category(V) * (D, N)
for (k <- K) {
phi(k) ~ dirichlet(beta)
}
for (d <- D) {
theta(d) ~ dirichlet(alpha)
for (n <- N(d)) {
z(d, n) ~ categorical(theta(d))
w(d, n) ~ categorical(phi(z(d, n)))
}
}
}
val basic = Seq(Unigram.model, MixtureOfUnigrams, LDA)
def main(args: Array[String]): Unit = {
println(Model.toDot(basic))
}
}
```
## Demo
```shellsession
$ sbt --error 'set showSuccess := false' 'example/runMain Demo' > demo.dot
$ dot -Tpng demo.dot > demo.png
```