Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.file.Files
- import java.nio.file.Paths
- import java.util.concurrent.ThreadLocalRandom
- import kotlin.math.*
- import kotlin.streams.toList
- fun main() {
- val hiddenLayer = mutableListOf(Perceptron(), Perceptron(), Perceptron())
- val outputLayer = mutableListOf(Perceptron(), Perceptron(), Perceptron())
- val neuralNetwork = NeuralNetwork("C:\\Users\\eddie\\Documents\\teachgolden\\src\\com\\script\\iris.csv", hiddenLayer, outputLayer)
- neuralNetwork.start()
- }
- class Perceptron {
- private val randomizer = ThreadLocalRandom.current()
- var delta: Double = 0.0
- private val alpha = 0.1
- private var weights = mutableListOf(
- randomizer.nextDouble(0.0, 1.0),
- randomizer.nextDouble(0.0, 1.0),
- randomizer.nextDouble(0.0, 1.0),
- randomizer.nextDouble(0.0, 1.0)
- )
- private var theta = randomizer.nextDouble(0.0, 1.0)
- fun fire(sample: MutableList<Double>): Double {
- var accumulator = 0.0
- for (i in sample.indices) {
- accumulator += weights[i] * sample[i]
- }
- accumulator -= theta
- return sigmoid(accumulator)
- }
- private fun sigmoid(x: Double): Double {
- return 1 / (1 + exp(-x))
- }
- fun updateWeights(sample: MutableList<Double>) {
- for (i in sample.indices) {
- weights[i] += alpha * sample[i] * delta
- }
- theta -= alpha * delta
- }
- fun firstWeight(): Double = weights[0]
- }
- class NeuralNetwork(val path: String, val hiddenLayer: MutableList<Perceptron>, val outputLayer: MutableList<Perceptron>) {
- private var hiddenResult: MutableList<Double> = mutableListOf()
- private var outputResult: MutableList<Double> = mutableListOf()
- private var currentPerformance = 0.0
- val yd = mutableListOf<MutableList<Double>>()
- private var counter = 0
- private val samples = mutableListOf<MutableList<Double>>()
- init {
- val path = Paths.get(this.path)
- val reader = Files.newBufferedReader(path)
- var count = 0
- reader.lines().forEach {
- val x = mutableListOf<Double>()
- val words = it.split(",")
- val y = mutableListOf<Double>()
- for (i in words.indices) {
- if (i < 4) x.add(words[i].toDouble())
- else y.add(words[i].toDouble())
- }
- yd.add(y)
- samples.add(x)
- count++
- }
- }
- fun start() {
- for (i in 1..10) {
- while (counter < 150) {
- feedForward()
- backPropagate()
- }
- println("Epoch $i Results: MAD = ${currentPerformance / 150}")
- currentPerformance = 0.0
- counter = 0
- }
- }
- private fun backPropagate() {
- var summation = 0.0
- for (i in outputResult.indices) {
- if (yd[counter][i] != outputResult[i]) {
- outputLayer[i].delta = outputResult[i] * ( 1 - outputResult[i]) * (yd[counter][i] - outputResult[i])
- outputLayer[i].updateWeights(hiddenResult)
- summation+= outputLayer[i].delta * outputLayer[i].firstWeight()
- }
- }
- for (i in hiddenResult.indices) {
- hiddenLayer[i].delta = hiddenResult[i] * (1 - hiddenResult[i]) * summation
- hiddenLayer[i].updateWeights(samples[counter])
- }
- hiddenResult.clear()
- outputResult.clear()
- counter++
- }
- private fun feedForward() {
- hiddenResult.addAll(hiddenLayer.stream().map{it.fire(samples[counter]) }.toList().toMutableList())
- outputResult.addAll(outputLayer.stream().map{ it.fire(hiddenResult) }.toList().toMutableList())
- var loss = 0.0
- for (i in outputResult.indices) {
- loss += abs(yd[counter][i] - outputResult[i])
- }
- currentPerformance += loss
- // println("Epoch 1: Prediction: $outputResult, Loss: $loss")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement