-
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
3 changed files
with
66 additions
and
2 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
31 changes: 31 additions & 0 deletions
31
src/main/scala/algorithm/classification/ExtremeLearning.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,31 @@ | ||
// Wei Chen - Extreme Learning | ||
// 2020-03-08 | ||
|
||
package com.scalaml.algorithm | ||
import com.scalaml.general.MatrixFunc._ | ||
|
||
class ExtremeLearning(val neuronNumber: Int, val featureNumber: Int, val outputNumber: Int) { | ||
var wIn = matrixrandom(featureNumber, neuronNumber, -1, 1) | ||
var wOut = matrixrandom(neuronNumber, outputNumber, -1, 1) | ||
|
||
def clear() = { | ||
wIn = matrixrandom(featureNumber, neuronNumber, -1, 1) | ||
wOut = matrixrandom(neuronNumber, outputNumber, -1, 1) | ||
} | ||
clear() | ||
|
||
private def reluLayer(x: Array[Array[Double]]): Array[Array[Double]] = { | ||
matrixdot(x, wIn).map(arr => arr.map(v => math.max(0, v))) | ||
} | ||
|
||
def train(x: Array[Array[Double]], y: Array[Array[Double]]) { | ||
val outX = reluLayer(x) | ||
val tranX = outX.transpose | ||
wOut = matrixdot(inverse(matrixdot(tranX, outX)), matrixdot(tranX, y)) | ||
} | ||
|
||
def predict(x: Array[Array[Double]]): Array[Array[Double]] = { | ||
val outX = reluLayer(x) | ||
matrixdot(outX, wOut) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/scala/algorithm/classification/ExtremeLearningTest.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,33 @@ | ||
// Wei Chen - Extreme Learning Test | ||
// 2016-11-06 | ||
|
||
import com.scalaml.TestData._ | ||
import com.scalaml.general.MatrixFunc._ | ||
import com.scalaml.algorithm.ExtremeLearning | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class ExtremeLearningSuite extends AnyFunSuite { | ||
|
||
val neuronNumber = 100 | ||
val featureNumber = UNLABELED_LARGE_HIGH_DIM_DATA.head.size | ||
val outputNumber = TARGET_LARGE_HIGH_DIM_DATA.head.size | ||
|
||
val el = new ExtremeLearning(neuronNumber, featureNumber, outputNumber) | ||
test("ExtremeLearning Test : Initialization") { | ||
assert(el.wIn.size == featureNumber) | ||
assert(el.wIn.head.size == neuronNumber) | ||
assert(el.wOut.size == neuronNumber) | ||
assert(el.wOut.head.size == outputNumber) | ||
} | ||
|
||
test("ExtremeLearning Test : Train") { | ||
el.train(UNLABELED_LARGE_HIGH_DIM_DATA, TARGET_LARGE_HIGH_DIM_DATA) | ||
val result = el.predict(UNLABELED_LARGE_HIGH_DIM_DATA) | ||
assert(matrixsimilar(result, TARGET_LARGE_HIGH_DIM_DATA, 0.5)) | ||
} | ||
|
||
test("ExtremeLearning Test : Predict - OVERFITS LIKE CRAZY W/O ENOUGH DATA") { | ||
val result = el.predict(UNLABELED_SMALL_HIGH_DIM_DATA) | ||
assert(matrixsimilar(result, TARGET_SMALL_HIGH_DIM_DATA, neuronNumber)) | ||
} | ||
} |