Advertisement
szmelu

PSI2

Oct 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <windows.h>
  6. using namespace std;
  7. float* NN(float *a, float W, float b) {
  8. float * nowa = new float[100];
  9. for (int i = 0; i<100; i++) {
  10. nowa[i] = W*a[i] + b;
  11. }
  12. return nowa;
  13. }
  14.  
  15. float MSE(float *a, float *b) {
  16. float wynik = 0.0;
  17. for (int i = 0; i<100; i++) {
  18. wynik += (a[i] - b[i])*(a[i] - b[i]);
  19. }
  20. return wynik / 100.0;
  21. }
  22. void update(float *x, float *y, float &w, float &b)
  23. {
  24. float E = 0.01;
  25. //dW = (suma(Wx[i]+b-y[i])*x[i])/d
  26. float dw=0, db=0;
  27. for (int i = 0; i < 100; i++)
  28. {
  29. dw = dw + (x[i] * (x[i] * w + b - y[i]));
  30. db += x[i] * w + b - y[i];
  31.  
  32. }
  33. w = w - ((E*dw)/100.0);
  34. b = b - ((E*db)/100.0);
  35. }
  36.  
  37. int main()
  38. {
  39.  
  40. srand(time(NULL));
  41. //y=2x+0,5
  42. float x[100];
  43. float y[100];
  44. float y2[100];
  45. float y3[100];
  46. float N;
  47. for (int i = 0; i<100; i++) {
  48. x[i] = (float)rand() / RAND_MAX * 2;
  49. y[i] = 2 * x[i] + 0.5;
  50. N = (rand() % 21 - 10) / 100.0;
  51. cout << N << endl;
  52. //Sleep(3000);
  53. y2[i] = (-3) * x[i] + 4 + N;
  54. y3[i] = x[i] * x[i];
  55.  
  56. }
  57.  
  58. float w, b;
  59. w = (float)rand() / RAND_MAX * 6 - 3;
  60. b = (float)rand() / RAND_MAX * 6 - 3;
  61.  
  62. /*float * Z = NN(x, w, b);
  63. float mse = MSE(y, Z);
  64. cout << mse << endl;
  65. Z = NN(x, 2, 0.5);
  66. mse = MSE(y, Z);
  67. cout << mse;*/
  68. for (int i = 0; i < 10000; i++)
  69. {
  70. float * Z = NN(x, w, b);
  71. float mse = MSE(y3, Z);
  72. cout << "MSE: " << mse << endl;
  73. cout << "W: " << w << endl;
  74. cout << "b: " << b << endl;
  75. float memory = b;
  76. update(x, y3, w, b);
  77. if (b == memory)
  78. break;
  79.  
  80.  
  81. }
  82. system("pause");
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement