Advertisement
bartoshr

Zmiany

Jan 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.33 KB | None | 0 0
  1. package next
  2.  
  3. import org.apache.commons.math3.linear.Array2DRowRealMatrix
  4. import org.apache.commons.math3.linear.RealMatrix
  5. import java.util.*
  6.  
  7. class Network(private val size: Int) {
  8.  
  9.     private var stableCount = 10000
  10.     var neurons: Array<Neuron> = Array(size, { _ -> Neuron(size) })
  11.     private val random = Random()
  12.  
  13.     fun learn(examples: Array<DoubleArray>) {
  14.         var example: RealMatrix
  15.         var result: RealMatrix = Array2DRowRealMatrix(size, size)
  16.  
  17.         for (e in examples) {
  18.             example = Array2DRowRealMatrix(e)
  19.             result = result.add(example.multiply(example.transpose()))
  20.         }
  21.         println("Learning done")
  22.  
  23.         for ( (i,row) in result.data.withIndex()) {
  24.             neurons[i].weights = row
  25.         }
  26.     }
  27.  
  28.  
  29.     fun recognizeImage(image: DoubleArray): Array<Boolean> {
  30.  
  31.         var correctCounter = 0
  32.  
  33.         for (i in 0 until size) {
  34.             neurons[i].spin = image[i]
  35.         }
  36.  
  37.         while (correctCounter < stableCount) {
  38.             val randomNeuron = random.nextInt(size - 1)
  39.             val spins = neurons.map({ i -> i.spin })
  40.             correctCounter++
  41.             if (neurons[randomNeuron].updateSpin(spins)) {
  42.                 correctCounter = 0
  43.             }
  44.         }
  45.  
  46.         return neurons.map { i -> i.spin == 1.0 }.toTypedArray()
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement