-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/scala/algorithm/regression/StochasticGradientDecent.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Wei Chen - Stochastic Gradient Decent | ||
// 2020-03-08 | ||
|
||
package com.scalaml.algorithm | ||
import com.scalaml.general.MatrixFunc._ | ||
|
||
class StochasticGradientDecent() extends Regression { | ||
val algoname: String = "StochasticGradientDecent" | ||
val version: String = "0.1" | ||
|
||
var weights = Array[Double]() | ||
var limit = 1000 // for GD | ||
var batch = 10 // for GD | ||
var lr = 0.01 // for GD | ||
|
||
override def clear(): Boolean = { | ||
weights = Array[Double]() | ||
true | ||
} | ||
|
||
override def config(paras: Map[String, Any]): Boolean = try { | ||
limit = paras.getOrElse("LIMIT", paras.getOrElse("limit", 1000)).asInstanceOf[Int] | ||
batch = paras.getOrElse("BATCH", paras.getOrElse("batch", 10)).asInstanceOf[Int] | ||
lr = paras.getOrElse("learning_rate", paras.getOrElse("lr", 0.01)).asInstanceOf[Double] | ||
true | ||
} catch { case e: Exception => | ||
Console.err.println(e) | ||
false | ||
} | ||
|
||
// --- Start Multivariate Linear Regression Function --- | ||
override def train( | ||
data: Array[(Double, Array[Double])] // Data Array(yi, xi) | ||
): Boolean = try { // Return PData Class | ||
val dataSize = data.size | ||
val y = data.map(_._1) | ||
val x = data.map(_._2 :+ 1.0) | ||
val xSize = x.head.size | ||
|
||
for (i <- 0 until limit) { | ||
val cut1 = (i * batch) % xSize | ||
val cut2 = cut1 + batch | ||
weights = gradientDescent( | ||
x.slice(cut1, cut2), | ||
y.slice(cut1, cut2), | ||
lr, 1, weights | ||
) | ||
} | ||
true | ||
} catch { case e: Exception => | ||
Console.err.println(e) | ||
false | ||
} | ||
// --- Predict Multivariate Linear Regression --- | ||
override def predict( | ||
data: Array[Array[Double]] | ||
): Array[Double] = { | ||
return data.map { d => | ||
(d :+ 1.0).zip(weights).map { case (x, w) => w * x }.sum | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/test/scala/algorithm/regression/StochasticGradientDecentTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Wei Chen - Multivariate Linear Regression Test | ||
// 2016-06-04 | ||
|
||
import com.scalaml.TestData._ | ||
import com.scalaml.general.MatrixFunc._ | ||
import com.scalaml.algorithm.StochasticGradientDecent | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class StochasticGradientDecentSuite extends AnyFunSuite { | ||
|
||
val sgd = new StochasticGradientDecent() | ||
|
||
test("StochasticGradientDecent Test : Clear") { | ||
assert(sgd.clear()) | ||
} | ||
|
||
test("StochasticGradientDecent Test : Linear Data") { | ||
assert(sgd.clear()) | ||
assert(sgd.config(Map[String, Double]())) | ||
assert(sgd.train(LABELED_LINEAR_DATA.map(yx => yx._1.toDouble -> yx._2))) | ||
val result = sgd.predict(UNLABELED_LINEAR_DATA) | ||
val nResult = result.map(v => if (v > 0) 1.0 else -1.0) | ||
assert(arraysimilar(nResult, LABEL_LINEAR_DATA.map(_.toDouble), 0.9)) | ||
} | ||
|
||
test("StochasticGradientDecent Test : Nonlinear Data - WRONG") { | ||
assert(sgd.clear()) | ||
assert(sgd.config(Map[String, Double]())) | ||
assert(sgd.train(LABELED_NONLINEAR_DATA.map(yx => yx._1.toDouble -> yx._2))) | ||
val result = sgd.predict(UNLABELED_NONLINEAR_DATA) | ||
assert(!arraysimilar(result, LABEL_NONLINEAR_DATA.map(_.toDouble), 0.45)) | ||
} | ||
|
||
test("StochasticGradientDecent Test : Invalid Config & Data") { | ||
assert(sgd.clear()) | ||
assert(!sgd.config(Map("limit" -> "test"))) | ||
assert(!sgd.train(Array((1, Array(1, 2)), (1, Array())))) | ||
} | ||
} |