mnist.helpers.MNISTSequenceExampleExtractor.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.
/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
* ------------------------------------------------------------------*/
package mnist.helpers
import com.jsoniter.JsonIterator
import com.jsoniter.ValueType
import com.kotlinnlp.simplednn.simplemath.ndarray.Shape
import com.kotlinnlp.simplednn.simplemath.ndarray.dense.DenseNDArrayFactory
import com.kotlinnlp.simplednn.dataset.SequenceExampleWithFinalOutput
import com.kotlinnlp.simplednn.simplemath.ndarray.dense.DenseNDArray
import utils.exampleextractor.ExampleExtractor
import utils.readDenseNDArray
/**
*
*/
class MNISTSequenceExampleExtractor(val outputSize: Int)
: ExampleExtractor> {
/**
*
*/
override fun extract(iterator: JsonIterator): SequenceExampleWithFinalOutput {
val featuresList = ArrayList()
val outputGold = DenseNDArrayFactory.zeros(Shape(10))
// read "digit"
iterator.readObject()
outputGold[iterator.readInt()] = 1.0
// skip "id"
iterator.readObject()
iterator.readAny()
// read "sequence_data"
iterator.readObject()
while (iterator.readArray()) {
if (iterator.whatIsNext() == ValueType.ARRAY) {
val features = iterator.readDenseNDArray()
val deltaX = features[0]
val deltaY = features[1]
featuresList.add(DenseNDArrayFactory.arrayOf(doubleArrayOf(deltaX, deltaY)))
}
}
return SequenceExampleWithFinalOutput(featuresList, outputGold)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy