SaveText.Ru

Без имени
  1.  
  2.  
  3.  
  4.  
  5.  
  6. var w1 = [[], []];
  7. var w2 = [];
  8.  
  9. function neuron(x, w) {
  10.   var Y = 0;
  11.   for (var i = 0; i < x.length; i++) {
  12.     Y += x[i] * w[i];
  13.   }
  14.   return (1 / (1 + Math.exp(-Y)));
  15. }
  16.  
  17.  
  18.  
  19.  
  20. for (var i = 0; i < 3; i++) {
  21.   w1[0][i] = Math.random()
  22.   w1[1][i] = Math.random()
  23. }
  24. for (var i = 0; i < 2; i++) {
  25.   w2[i] = Math.random()
  26.  
  27. }
  28.  
  29. var trening_in = [[0, 0, 0],[0, 0, 1],[0, 1, 0],[0, 1, 1],[1, 0, 0],[1, 0, 1],[1, 1, 0],[1, 1, 1]]
  30. var trening_out = [0,1,0,0,1,1,0,1]
  31.  
  32.  
  33.  
  34. var learning_rate = 0.5
  35. var epochs = 500000
  36.  
  37. for (var e = 0; e < epochs; e++) {
  38.   for (var i = 0; i < trening_in.length; i++) {
  39.  
  40. var nr1 = neuron(trening_in[i],w1[0])//;console.log(nr1)
  41. var nr2 = neuron(trening_in[i],w1[1])//;console.log(nr2)
  42. var nr3 = neuron([nr1,nr2],w2)//;console.log(nr3)
  43. //console.log(i)
  44.  
  45. var weights_delta_nr3 = weights_delta(nr3,trening_out[i])//;console.log(weights_delta_nr3)
  46. w2[0] = weight(w2[0],nr1,weights_delta_nr3,learning_rate)//;console.log(w2[0])
  47. w2[1] = weight(w2[1],nr2,weights_delta_nr3,learning_rate)//;console.log(w2[1])
  48.  
  49. var error_nr1 = error(w2[0],weights_delta_nr3)//;console.log(error_nr1)
  50. var weights_delta_nr1 = weights_delta(nr1,error_nr1)//;console.log(weights_delta_nr1)
  51. w1[0][0] = weight(w1[0][0],trening_in[i][0],weights_delta_nr1,learning_rate)//;console.log(w1[0][0])
  52. w1[0][1] = weight(w1[0][1],trening_in[i][1],weights_delta_nr1,learning_rate)//;console.log(w1[0][1])
  53. w1[0][2] = weight(w1[0][2],trening_in[i][2],weights_delta_nr1,learning_rate)//;console.log(w1[0][2])
  54.  
  55. var error_nr2 = error(w2[1],weights_delta_nr3)//;console.log(error_nr2)
  56. var weights_delta_nr2 = weights_delta(nr2,error_nr2)//;console.log(weights_delta_nr2)
  57. w1[1][0] = weight(w1[1][0],trening_in[i][0],weights_delta_nr1,learning_rate)//;console.log(w1[1][0])
  58. w1[1][1] = weight(w1[1][1],trening_in[i][1],weights_delta_nr1,learning_rate)//;console.log(w1[1][1])
  59. w1[1][2] = weight(w1[1][2],trening_in[i][2],weights_delta_nr1,learning_rate)//;console.log(w1[1][2])
  60.  
  61. var mse = MSE(trening_out[i],nr3)
  62.   }
  63. }
  64. console.log(w1[0])
  65. console.log(w1[1])
  66. console.log(w2)
  67. console.log(mse)
  68. console.log(nr3)
  69.  
  70.  
  71.  
  72.  
  73. function weights_delta(actual,expected){  
  74.  return (actual - expected) * (actual * (1 - actual))
  75. }
  76.  
  77. function weight(weight,output1,weights_delta,learning_rate){
  78.   return weight - output1 * weights_delta * learning_rate
  79. }
  80. function error(weight,weights_delta){
  81.   return weight * weights_delta
  82. }
  83.  
  84. function MSE(y,Y){
  85.   return Math.pow(y-Y,2)
  86. }

Share with your friends:

Print