Skip to content

Instantly share code, notes, and snippets.

@rafalbednarczuk
Created April 9, 2018 20:39
Show Gist options
  • Save rafalbednarczuk/1af6f5122ccc6b00bdb4da498d3399ca to your computer and use it in GitHub Desktop.
Save rafalbednarczuk/1af6f5122ccc6b00bdb4da498d3399ca to your computer and use it in GitHub Desktop.
fun main(args: Array<String>) {
val seed = 13455.toLong()
val recordReader = CSVRecordReader(0, ',')
recordReader.initialize(FileSplit(ClassPathResource("data.csv").file))
//reader,label index,number of possible labels
val labelIndex = 0
val numClasses = 6
val batchSize = 106
val iterator = RecordReaderDataSetIterator(recordReader, batchSize, labelIndex, numClasses)
val allData = iterator.next()
allData.shuffle(seed)
val testAndTrain = allData.splitTestAndTrain(0.8)
val trainingData = testAndTrain.train
val testData = testAndTrain.test
val normalizer = NormalizerStandardize()
normalizer.fit(trainingData)
normalizer.transform(trainingData)
normalizer.transform(testData)
val numInputs = 9
val outputNum = 6
val iterations = 1
val epoches = 100000
log.info("Building model...")
val conf = NeuralNetConfiguration.Builder()
.seed(seed)
.iterations(iterations)
.activation(Activation.TANH)
.weightInit(WeightInit.XAVIER)
.learningRate(10.0)
.updater(Updater.ADAGRAD)
.list()
.layer(0, DenseLayer.Builder().nIn(numInputs).nOut(2).build())
.layer(1, OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.activation(Activation.SOFTMAX).nIn(2).nOut(outputNum).build())
.backprop(true)
.pretrain(false)
.build()
val model = MultiLayerNetwork(conf)
model.init()
model.setListeners(ScoreIterationListener(100))
for (i in 1..epoches) {
model.fit(trainingData)
if (i % 100 == 0) {
val eval = Evaluation(6)
val output = model.output(testData.featureMatrix)
eval.eval(testData.labels, output)
log.info(eval.accuracy().toString())
}
}
val eval = Evaluation(6)
val output = model.output(testData.featureMatrix)
eval.eval(testData.labels, output)
log.info(eval.accuracy().toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment