Skip to content

Commit 0a762b8

Browse files
committed
ExtremeLearning
1 parent 1b467e6 commit 0a762b8

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ A very light weight Scala machine learning library that provide some basic ML al
3838

3939
- [x] Random Forest [[Code]](src/main/scala/algorithm/classification/RandomForest.scala) [[Usage]](src/test/scala/algorithm/classification/RandomForestTest.scala)
4040

41+
- [x] Extreme Learning Machine [[Code]](src/main/scala/algorithm/classification/ExtremeLearning.scala) [[Usage]](src/test/scala/algorithm/classification/ExtremeLearningTest.scala)
42+
4143
### Boost :
4244

4345
- [x] Naive Boost [[Code]](src/main/scala/algorithm/classification/NaiveBoost.scala) [[Usage]](src/test/scala/algorithm/classification/NaiveBoostTest.scala)
@@ -123,8 +125,6 @@ A very light weight Scala machine learning library that provide some basic ML al
123125

124126
## TODO
125127

126-
- [ ] Extreme Learning Machine - Classification
127-
128128
- [ ] Polarize Experience Replay - Deep Reinforcement Learning
129129

130130
- [ ] Rainbow - Deep Reinforcement Learning
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Wei Chen - Extreme Learning
2+
// 2020-03-08
3+
4+
package com.scalaml.algorithm
5+
import com.scalaml.general.MatrixFunc._
6+
7+
class ExtremeLearning(val neuronNumber: Int, val featureNumber: Int, val outputNumber: Int) {
8+
var wIn = matrixrandom(featureNumber, neuronNumber, -1, 1)
9+
var wOut = matrixrandom(neuronNumber, outputNumber, -1, 1)
10+
11+
def clear() = {
12+
wIn = matrixrandom(featureNumber, neuronNumber, -1, 1)
13+
wOut = matrixrandom(neuronNumber, outputNumber, -1, 1)
14+
}
15+
clear()
16+
17+
private def reluLayer(x: Array[Array[Double]]): Array[Array[Double]] = {
18+
matrixdot(x, wIn).map(arr => arr.map(v => math.max(0, v)))
19+
}
20+
21+
def train(x: Array[Array[Double]], y: Array[Array[Double]]) {
22+
val outX = reluLayer(x)
23+
val tranX = outX.transpose
24+
wOut = matrixdot(inverse(matrixdot(tranX, outX)), matrixdot(tranX, y))
25+
}
26+
27+
def predict(x: Array[Array[Double]]): Array[Array[Double]] = {
28+
val outX = reluLayer(x)
29+
matrixdot(outX, wOut)
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Wei Chen - Extreme Learning Test
2+
// 2016-11-06
3+
4+
import com.scalaml.TestData._
5+
import com.scalaml.general.MatrixFunc._
6+
import com.scalaml.algorithm.ExtremeLearning
7+
import org.scalatest.funsuite.AnyFunSuite
8+
9+
class ExtremeLearningSuite extends AnyFunSuite {
10+
11+
val neuronNumber = 100
12+
val featureNumber = UNLABELED_LARGE_HIGH_DIM_DATA.head.size
13+
val outputNumber = TARGET_LARGE_HIGH_DIM_DATA.head.size
14+
15+
val el = new ExtremeLearning(neuronNumber, featureNumber, outputNumber)
16+
test("ExtremeLearning Test : Initialization") {
17+
assert(el.wIn.size == featureNumber)
18+
assert(el.wIn.head.size == neuronNumber)
19+
assert(el.wOut.size == neuronNumber)
20+
assert(el.wOut.head.size == outputNumber)
21+
}
22+
23+
test("ExtremeLearning Test : Train") {
24+
el.train(UNLABELED_LARGE_HIGH_DIM_DATA, TARGET_LARGE_HIGH_DIM_DATA)
25+
val result = el.predict(UNLABELED_LARGE_HIGH_DIM_DATA)
26+
assert(matrixsimilar(result, TARGET_LARGE_HIGH_DIM_DATA, 0.5))
27+
}
28+
29+
test("ExtremeLearning Test : Predict - OVERFITS LIKE CRAZY W/O ENOUGH DATA") {
30+
val result = el.predict(UNLABELED_SMALL_HIGH_DIM_DATA)
31+
assert(matrixsimilar(result, TARGET_SMALL_HIGH_DIM_DATA, neuronNumber))
32+
}
33+
}

0 commit comments

Comments
 (0)