utils.exampleextractor.ClassificationSparseExampleExtractor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simplednn Show documentation
Show all versions of simplednn Show documentation
SimpleDNN is a machine learning lightweight open-source library written in Kotlin whose purpose is to
support the development of feed-forward and recurrent Artificial Neural Networks.
package utils.exampleextractor
import com.jsoniter.JsonIterator
import com.jsoniter.ValueType
import com.kotlinnlp.simplednn.dataset.SimpleExample
import com.kotlinnlp.simplednn.simplemath.ndarray.Shape
import com.kotlinnlp.simplednn.simplemath.ndarray.dense.DenseNDArrayFactory
import com.kotlinnlp.simplednn.simplemath.ndarray.sparsebinary.SparseBinaryNDArray
import utils.readSparseBinaryNDArray
/**
*
*/
class ClassificationSparseExampleExtractor(
val inputSize: Int,
val outputSize: Int
) : ExampleExtractor> {
/**
*
*/
override fun extract(iterator: JsonIterator): SimpleExample {
val outputGold = DenseNDArrayFactory.zeros(Shape(this.outputSize))
var goldIndex: Int
var features: SparseBinaryNDArray? = null
while (iterator.readArray()) {
if (iterator.whatIsNext() == ValueType.ARRAY) {
features = iterator.readSparseBinaryNDArray(size = inputSize)
} else if (iterator.whatIsNext() == ValueType.NUMBER) {
goldIndex = iterator.readInt()
outputGold[goldIndex] = 1.0
}
}
return SimpleExample(features!!, outputGold)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy