bartoshr

Perceptron

Oct 14th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.87 KB | None | 0 0
  1. package logic
  2.  
  3. import java.util.Random
  4.  
  5.  
  6.  
  7. class Perceptron {
  8.  
  9.     lateinit var weights : DoubleArray
  10.     var threshold : Double = 0.0
  11.  
  12.     fun train(inputs: Array<DoubleArray>, outputs: IntArray, threshold: Double, lrate: Double, epoch: Int) {
  13.  
  14.         val r = Random()
  15.         weights = DoubleArray(inputs[0].size,
  16.                 { _ -> r.nextDouble() })
  17.  
  18.         this.threshold = threshold
  19.         val n = inputs[0].size
  20.         val p = outputs.size
  21.  
  22.         for (i in 0 until epoch) {
  23.             var totalError = 0
  24.             for (j in 0 until p) {
  25.                 val output = calculate(inputs[j])
  26.                 val error = outputs[j] - output
  27.  
  28.                 totalError += error
  29.  
  30.                 for (k in 0 until n) {
  31.                     val delta = lrate * inputs[j][k] * error.toDouble()
  32.                     weights[k] += delta
  33.                 }
  34.             }
  35.             println(weights.toList());
  36.             if (totalError == 0)
  37.                 break
  38.         }
  39.     }
  40.  
  41.     fun spla(inputs: Array<DoubleArray>, outputs: IntArray, threshold: Double, times: Int) {
  42.         val r = Random()
  43.         weights = DoubleArray(inputs[0].size + 1,
  44.                 { _ -> r.nextDouble() })
  45.         this.threshold = threshold
  46.  
  47.         for (i in 0 until times) {
  48.  
  49.             val ind = r.nextInt(inputs.size)
  50.             if (calculate(inputs[ind]) != outputs[ind]) {
  51.                 for (j in 0 until inputs[0].size) {
  52.                     weights[j+1] += inputs[ind][j] * outputs[ind].toDouble()
  53.                 }
  54.                 weights[0] += outputs[ind].toDouble()
  55.             }
  56.  
  57.             val correctInputs = inputs.filterIndexed { index, input -> (calculate(input) == outputs[index]) }
  58.             val correct = correctInputs.count()
  59.             if (correct == inputs.size) {
  60.                 println("Break happend")
  61.                 break
  62.             }
  63.             println(weights.toList());
  64.  
  65.         }
  66.     }
  67.  
  68.  
  69.  
  70.  
  71. //    fun calculate(input: DoubleArray): Int {
  72. //        val sum = input.indices.sumByDouble { weights[it] * input[it] }
  73. //        return if (sum > threshold) 1 else 0
  74. //    }
  75.  
  76.     fun calculate(input: DoubleArray): Int {
  77.         val inputFixed = doubleArrayOf(1.0)+input
  78.         val sum = inputFixed.indices.sumByDouble { weights[it] * inputFixed[it] }
  79.         return if (sum > 0) 1 else -1
  80.     }
  81.  
  82. }
  83.  
  84. fun main(args: Array<String>) {
  85.     val p = Perceptron()
  86.     val inputs = arrayOf(doubleArrayOf(0.0, 0.0), doubleArrayOf(0.0, 1.0), doubleArrayOf(1.0, 0.0), doubleArrayOf(1.0, 1.0))
  87.     val outputs = intArrayOf(-1, -1, 1, -1)
  88.  
  89. //    p.train(inputs, outputs, 0.2, 0.2, 40_000)
  90.     p.spla(inputs, outputs, 0.2,40000)
  91.     println(p.calculate(doubleArrayOf(0.0, 0.0)))
  92.     println(p.calculate(doubleArrayOf(0.0, 1.0)))
  93.     println(p.calculate(doubleArrayOf(1.0, 0.0)))
  94.     println(p.calculate(doubleArrayOf(1.0, 1.0)))
  95. }
Advertisement
Add Comment
Please, Sign In to add comment