fork download
  1. -- your code goes here
  2. --//neiro
  3.  
  4. local Neirons = {}
  5. Neirons.__index=Neirons
  6. local count = 0
  7. function newNeiro(input,value,func,name,range,a)
  8. n={}
  9. n.range=range or 1
  10. n.a = a or 1
  11. n.input = input or {}
  12. n.value = value or 0
  13. n.func = func or sigma
  14. n.weight = {}
  15. math.randomseed (os.clock())
  16. for i=1,#n.input do
  17. n.weight[i]=math.random()*0.4+0.1
  18. end
  19. n.name = name or "neiro_"..count
  20. count = count+1
  21. return setmetatable(n,Neirons)
  22. end
  23.  
  24. function Neirons:math()
  25. local n = 0
  26. if(#self.input>0) then
  27. for i=1,#self.input do
  28. self.input[i]:math()--пересчитываем входной нейрон
  29. n = self.weight[i]*self.input[i].value+n
  30. end
  31. self.value = self.func(n,self.range,self.a)
  32. end
  33. end
  34. --обновляем веса синапсов нейронов
  35. function Neirons:newWeights(d)
  36. for i=1,#self.input do
  37. self.weight[i] = self.input[i].value*d + self.weight[i]
  38. end
  39. end
  40. --среднее значение входных параметров
  41. function porog(n,range,a)
  42. if n > range then
  43. return 1
  44. else
  45. return 0
  46. end
  47. end
  48. function sigma(n,range,a)
  49. local range = range or 1
  50. local a = a or 1
  51. return range*(2/(1+math.exp(-n*a-1)))
  52. end
  53. function line(n,range,a)
  54. return n
  55. end
  56.  
  57.  
  58. local fn,sn=newNeiro(),newNeiro()
  59. local add = newNeiro({fn,sn},0,line,nil,0.5,1)
  60.  
  61.  
  62. local a_and={
  63. {1.0,1.0,3.0},
  64. {2.0,2.0,6.0},
  65. {3.0,3.0,9.0},
  66. {1.0,5.0,11.0},
  67. }
  68.  
  69. for i=1,1000 do
  70. local gErr = 0
  71.  
  72. for p=1,#a_and do
  73. fn.value = a_and[p][1]
  74. sn.value = a_and[p][2]
  75. add:math()
  76.  
  77. local err = a_and[p][3] - add.value
  78. add:newWeights(err*0.1)
  79. gErr = math.abs(err)+gErr
  80. end
  81.  
  82. if(gErr<0.0005) then
  83. print(string.format("SLearned in %d iteration",i))
  84. break
  85. end
  86. end
  87.  
  88. for p=1,20 do
  89. fn.value = math.random(1,10)
  90. sn.value = math.random(1,10)
  91. add:math()
  92. print(string.format("%.3f + 2*%.3f = %.3f",fn.value,sn.value,add.value))
  93. end-- your code goes here
Success #stdin #stdout 0s 2832KB
stdin
Standard input is empty
stdout
SLearned in 14 iteration
8.000 + 2*10.000 = 28.000
2.000 + 2*4.000 = 10.000
8.000 + 2*3.000 = 14.000
6.000 + 2*5.000 = 16.000
7.000 + 2*4.000 = 15.000
6.000 + 2*10.000 = 26.000
10.000 + 2*7.000 = 24.000
8.000 + 2*2.000 = 12.000
7.000 + 2*1.000 = 9.000
3.000 + 2*2.000 = 7.000
9.000 + 2*2.000 = 13.000
5.000 + 2*2.000 = 9.000
2.000 + 2*10.000 = 22.000
3.000 + 2*6.000 = 15.000
9.000 + 2*7.000 = 23.000
3.000 + 2*7.000 = 17.000
6.000 + 2*5.000 = 16.000
10.000 + 2*3.000 = 16.000
8.000 + 2*6.000 = 20.000
8.000 + 2*5.000 = 18.000