/
LinearRegression.kt
67 lines (52 loc) 路 2.2 KB
/
LinearRegression.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ai.hypergraph.kotlingrad.samples
import ai.hypergraph.kotlingrad.api.*
import ai.hypergraph.kotlingrad.shapes.*
fun main() {
val theta by DReal.Var(D2)
val input by DReal.Var(D3, D2)
val bias by DReal.Var()
val label by DReal.Var(D3)
val loss = ((input * theta).map { it + bias } - label).magnitude()
var weightsNow = DReal.Vec(D2) { rand.nextDouble() * 10 }
var biasNow = rand.nextDouble() * 10
println("Initial weights are: $weightsNow")
val hiddenWeights = DReal.Vec(D2) { rand.nextDouble() * 10 }
val hiddenBias = rand.nextDouble() * 10
println("Target coefficients: $hiddenWeights")
var totalLoss = 0.0
val epochSize = 500
var totalTime = 0L
val alpha = 0.001
val lossHistory = mutableListOf<Pair<Int, Double>>()
var weightMap: Array<Pair<Fun<DReal>, Any>>
val totalEpochs = 30
loss.saveToFile("lossFun.dot")
for (epochs in 1..(epochSize * totalEpochs)) {
totalTime += System.nanoTime()
val noise = DReal.Vec(D3) { rand.nextDouble() - 0.5 }
val batch = DReal.Mat(D3, D2) { _, _ -> rand.nextDouble() }
val targets = (batch * hiddenWeights).map { it + hiddenBias } + noise
val batchInputs = arrayOf(input to batch, label to targets())
val batchLoss = loss(*batchInputs)
weightMap = arrayOf(theta to weightsNow, bias to biasNow)
val averageLoss = batchLoss(*weightMap).toDouble() / batch.rows.size
val weightGrads = batchLoss.d(theta)
val biasGrads = batchLoss.d(bias)
weightsNow = (weightsNow - alpha * weightGrads)(*weightMap)()
biasNow = (biasNow - alpha * biasGrads)(*weightMap).toDouble()
totalTime -= System.nanoTime()
if (epochs % epochSize == 0) {
println("Average loss at ${epochs / epochSize} / $totalEpochs epochs: ${totalLoss / epochSize}")
println("Average time: " + -totalTime.toDouble() / epochSize + "ns")
lossHistory += epochs / epochSize to totalLoss / epochSize
totalTime = 0L
totalLoss = 0.0
}
totalLoss += averageLoss
}
println("Final weights: $weightsNow, bias: $biasNow")
println("Target coefficients: $hiddenWeights, $hiddenBias")
mapOf("Epochs" to lossHistory.map { it.first },
"Average Loss" to lossHistory.map { it.second }
).plot2D("Training Loss")
}