Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <ctime>
  4. #include <cstdlib>
  5. const int rows = 4, cols = 3;
  6. double sigmoid(double x, double w)
  7. {
  8. if (x == 0) x -= w;
  9. //if (x > -1 && x<1) x -= w;
  10. return 1 / (1 + exp(-(x)));
  11. }
  12. double fRand()
  13. {
  14. double fMin = -1;
  15. double fMax = 1;
  16. double f = (double)rand() / RAND_MAX;
  17. return fMin + f * (fMax - fMin);
  18. }
  19. template <int Size1, int Size2>
  20. double newDot(int(&input)[Size1][Size2], double other[], int k)
  21. {
  22. double d = 0;
  23. for (int j = 0; j < Size2; j++)
  24. {
  25. d += input[k][j] * other[j];
  26. }
  27. return d;
  28. }
  29. void transpose(int T[cols][rows], int input_layers[rows][cols])
  30. {
  31. for (int i = 0; i < rows; i++)
  32. for (int j = 0; j < cols; j++)
  33. T[j][i] = input_layers[i][j];
  34. }
  35. void findRfromO(double errors[rows], double outputs[rows], double right[rows])
  36. {
  37. for (int i = 0; i < rows; i++)
  38. {
  39. right[i] = errors[i] * (outputs[i] * (1 - outputs[i]));
  40. }
  41. }
  42. int main()
  43. {
  44. double B = 8;
  45. srand(time(0));
  46. int training_inputs[rows][cols] =
  47. { {0,1,0},
  48. {0,1,1},
  49. {0,0,1},
  50. {1,0,1} };
  51. int training_outputs[rows] = { 0,0,0,1 };
  52. double synaptic_weights[cols];
  53. for (int i = 0; i < cols; i++)
  54. synaptic_weights[i] = fRand();
  55. std::cout << "Starting weights: " << std::endl;
  56. for (int i = 0; i < cols; i++)
  57. std::cout << synaptic_weights[i] << std::endl;
  58. //Метод обратного распространения
  59. int input_layers[rows][cols];
  60. for (int i = 0; i < rows; i++)
  61. for (int j = 0; j < cols; j++)
  62. input_layers[i][j] = training_inputs[i][j];
  63. double outputs[rows];
  64. double errors[rows];
  65. double adjustments[cols];
  66. double right[rows];
  67. int T_input_layers[cols][rows];
  68. for (int g = 0; g < 20000; g++)
  69. {
  70. for (int i = 0; i < rows; i++)
  71. outputs[i] = sigmoid(newDot(input_layers, synaptic_weights, i), B);
  72. for (int i = 0; i < rows; i++)
  73. errors[i] = training_outputs[i] - outputs[i];
  74. transpose(T_input_layers, input_layers);
  75. findRfromO(errors, outputs, right);
  76. for (int j = 0; j < cols; j++)
  77. adjustments[j] = newDot(T_input_layers, right, j);
  78. for (int j = 0; j < cols; j++)
  79. synaptic_weights[j] += adjustments[j];
  80. }
  81.  
  82. std::cout << "\nWeights after learning: " << std::endl;
  83. for (int j = 0; j < cols; j++)
  84. std::cout << synaptic_weights[j] << std::endl;
  85.  
  86. std::cout << "\nResult for starting inputs: " << std::endl;
  87. for (int i = 0; i < rows; i++)
  88. std::cout << outputs[i] << std::endl;
  89.  
  90.  
  91. //Тест
  92. const int new_rows = 2;
  93. int new_in[new_rows][cols];
  94. int x;
  95. for (int i = 0; i < new_rows; i++)
  96. {
  97. for (int j = 0; j < cols; j++)
  98. {
  99. std::cin >> x;
  100. new_in[i][j] = x;
  101. }
  102. }
  103. double new_out[new_rows];
  104. std::cout << "\n\nTest Results: " << std::endl;
  105. for (int i = 0; i < new_rows; i++)
  106. {
  107. new_out[i] = sigmoid(newDot(new_in, synaptic_weights, i), B);
  108. std::cout << std::endl << new_out[i];
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement