diff options
Diffstat (limited to 'examples/src/main/scala/LocalLR.scala')
-rw-r--r-- | examples/src/main/scala/LocalLR.scala | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/src/main/scala/LocalLR.scala b/examples/src/main/scala/LocalLR.scala new file mode 100644 index 0000000000..175907e551 --- /dev/null +++ b/examples/src/main/scala/LocalLR.scala @@ -0,0 +1,41 @@ +import java.util.Random +import Vector._ + +object LocalLR { + val N = 10000 // Number of data points + val D = 10 // Numer of dimensions + val R = 0.7 // Scaling factor + val ITERATIONS = 5 + val rand = new Random(42) + + case class DataPoint(x: Vector, y: Double) + + def generateData = { + def generatePoint(i: Int) = { + val y = if(i % 2 == 0) -1 else 1 + val x = Vector(D, _ => rand.nextGaussian + y * R) + DataPoint(x, y) + } + Array.fromFunction(generatePoint _)(N) + } + + def main(args: Array[String]) { + val data = generateData + + // Initialize w to a random value + var w = Vector(D, _ => 2 * rand.nextDouble - 1) + println("Initial w: " + w) + + for (i <- 1 to ITERATIONS) { + println("On iteration " + i) + var gradient = Vector.zeros(D) + for (p <- data) { + val scale = (1 / (1 + Math.exp(-p.y * (w dot p.x))) - 1) * p.y + gradient += scale * p.x + } + w -= gradient + } + + println("Final w: " + w) + } +} |