Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.99 KB | None | 0 0
  1. import java.nio.file.Files
  2. import java.nio.file.Paths
  3. import java.util.concurrent.ThreadLocalRandom
  4. import kotlin.math.*
  5. import kotlin.streams.toList
  6.  
  7. fun main() {
  8.     val hiddenLayer = mutableListOf(Perceptron(), Perceptron(), Perceptron())
  9.     val outputLayer = mutableListOf(Perceptron(), Perceptron(), Perceptron())
  10.  
  11.     val neuralNetwork = NeuralNetwork("C:\\Users\\eddie\\Documents\\teachgolden\\src\\com\\script\\iris.csv", hiddenLayer, outputLayer)
  12.     neuralNetwork.start()
  13.  
  14. }
  15.  
  16.  
  17. class Perceptron {
  18.     private val randomizer = ThreadLocalRandom.current()
  19.     var delta: Double = 0.0
  20.     private val alpha = 0.1
  21.  
  22.     private var weights = mutableListOf(
  23.         randomizer.nextDouble(0.0, 1.0),
  24.         randomizer.nextDouble(0.0, 1.0),
  25.         randomizer.nextDouble(0.0, 1.0),
  26.         randomizer.nextDouble(0.0, 1.0)
  27.     )
  28.     private var theta = randomizer.nextDouble(0.0, 1.0)
  29.  
  30.     fun fire(sample: MutableList<Double>): Double {
  31.         var accumulator = 0.0
  32.         for (i in sample.indices) {
  33.             accumulator += weights[i] * sample[i]
  34.         }
  35.         accumulator -= theta
  36.         return sigmoid(accumulator)
  37.     }
  38.  
  39.     private fun sigmoid(x: Double): Double {
  40.         return 1 / (1 + exp(-x))
  41.     }
  42.  
  43.     fun updateWeights(sample: MutableList<Double>) {
  44.         for (i in sample.indices) {
  45.             weights[i] += alpha * sample[i] * delta
  46.         }
  47.         theta -= alpha * delta
  48.     }
  49.  
  50.     fun firstWeight(): Double = weights[0]
  51.  
  52. }
  53.  
  54. class NeuralNetwork(val path: String, val hiddenLayer: MutableList<Perceptron>, val outputLayer: MutableList<Perceptron>) {
  55.  
  56.     private var hiddenResult: MutableList<Double> = mutableListOf()
  57.     private var outputResult: MutableList<Double> = mutableListOf()
  58.     private var currentPerformance = 0.0
  59.  
  60.  
  61.     val yd = mutableListOf<MutableList<Double>>()
  62.     private var counter = 0
  63.     private val samples = mutableListOf<MutableList<Double>>()
  64.     init {
  65.         val path = Paths.get(this.path)
  66.         val reader = Files.newBufferedReader(path)
  67.         var count = 0
  68.         reader.lines().forEach {
  69.             val x = mutableListOf<Double>()
  70.             val words = it.split(",")
  71.             val y = mutableListOf<Double>()
  72.             for (i in words.indices) {
  73.                 if (i < 4) x.add(words[i].toDouble())
  74.                 else y.add(words[i].toDouble())
  75.             }
  76.             yd.add(y)
  77.             samples.add(x)
  78.             count++
  79.         }
  80.     }
  81.  
  82.     fun start() {
  83.         for (i in 1..10) {
  84.             while (counter < 150) {
  85.                 feedForward()
  86.                 backPropagate()
  87.             }
  88.             println("Epoch $i Results: MAD = ${currentPerformance / 150}")
  89.             currentPerformance = 0.0
  90.             counter = 0
  91.         }
  92.     }
  93.  
  94.     private fun backPropagate() {
  95.         var summation = 0.0
  96.         for (i in outputResult.indices) {
  97.             if (yd[counter][i] != outputResult[i]) {
  98.                 outputLayer[i].delta = outputResult[i] * ( 1 - outputResult[i]) * (yd[counter][i] - outputResult[i])
  99.                 outputLayer[i].updateWeights(hiddenResult)
  100.                 summation+= outputLayer[i].delta * outputLayer[i].firstWeight()
  101.             }
  102.         }
  103.  
  104.  
  105.         for (i in hiddenResult.indices) {
  106.             hiddenLayer[i].delta = hiddenResult[i] * (1 - hiddenResult[i]) * summation
  107.             hiddenLayer[i].updateWeights(samples[counter])
  108.         }
  109.  
  110.  
  111.         hiddenResult.clear()
  112.         outputResult.clear()
  113.         counter++
  114.     }
  115.  
  116.     private fun feedForward() {
  117.         hiddenResult.addAll(hiddenLayer.stream().map{it.fire(samples[counter]) }.toList().toMutableList())
  118.         outputResult.addAll(outputLayer.stream().map{ it.fire(hiddenResult) }.toList().toMutableList())
  119.  
  120.         var loss = 0.0
  121.  
  122.         for (i in outputResult.indices) {
  123.             loss += abs(yd[counter][i] - outputResult[i])
  124.         }
  125.         currentPerformance += loss
  126.  
  127. //        println("Epoch 1: Prediction: $outputResult, Loss: $loss")
  128.     }
  129.  
  130.  
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement