Skip to content
/ mini-ml Public

Computation graphs, automatic differentiation and machine learning for Kotlin

License

Notifications You must be signed in to change notification settings

fwcd/mini-ml

Repository files navigation

MiniML

A lightweight computation graph library for machine learning written in pure Kotlin.

MiniML optimizes mathematical expressions by backpropagating gradients through a dynamically created expression graph.

Example

Training a linear model:

val x = placeholder(zeros())
val w = variable(randoms(-10.0, 10.0))
val b = variable(randoms(-10.0, 10.0))
val expected = x * 3.0
val output = (w * x) + b
val cost = (expected - output).square()
val learningRate = 0.0001
val writer = File("cost.dat")
    .also { it.createNewFile() }
    .printWriter()

for (i in 0 until 2000) {
    x.value = randoms(-10.0, 10.0)
    val currentCost = cost.forward()
    cost.clearGradients()
    cost.backward()
    w.apply(-w.gradient!! * learningRate)
    b.apply(-b.gradient!! * learningRate)
    writer.println(currentCost)
}

writer.close()

Cost function over time

In addition to operator overloading, MiniML also supports modern Kotlin features such as destructuring declarations:

val (rowA, rowB) = matrixOf(
    rowOf(2.0, 5.3, 7.8),
    rowOf(0.0, 0.9, 1.1)
)
val (cellA, cellB, cellC) = rowA

About

Computation graphs, automatic differentiation and machine learning for Kotlin

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages