Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. double w0, w1, w2, w3, w4, w5; // wagi
  9. double b1, b2, b3; // biasy
  10.  
  11. double random() // losuje 0.01 - 1.00
  12. {
  13.   return ((rand()%100)+1)/100.0;
  14. }
  15.  
  16. double sigmoid(double x) // funkcja aktywuj¹ca
  17. {
  18.     return 1/(1+exp(-x));
  19. }
  20.  
  21. double derivsigmoid(double x)
  22. {
  23.     return x*(1-x);
  24. }
  25.  
  26. double mse_loss(double* y, double* ypred, int n) // n - liczba probek, ypred - wartosci obliczone, y - wartosci docelowe
  27. {
  28.     double result = 0;
  29.     for(int i=0; i<n; i++)
  30.         result+=pow((y[i]-ypred[i]), 2);
  31.     return result/(double)n;
  32. }
  33.  
  34. double feedforward(double* x) // wynik neuronu
  35. {
  36.     double h0 = sigmoid(w0*x[0] + w1*x[1] + b1);
  37.     double h1 = sigmoid(w2*x[0] + w3*x[1] + b2);
  38.     return sigmoid(w4*h0 + w5*h1 + b3);
  39. }
  40.  
  41. void train(double* x, double* y, double learn_rate, int epochs, int n, int m) // m - size, n - co ile probek
  42. {
  43.     for(int i=0; i<epochs; i++)
  44.     {
  45.  
  46.         for(int j=0; j<m; j++)
  47.         {
  48.             double h0 = sigmoid(w0*x[0] + w1*x[1] + b1);
  49.             double h1 = sigmoid(w2*x[0] + w3*x[1] + b2);
  50.             double o0 = sigmoid(w4*h0 + w5*h1 + b3);
  51.  
  52.             double deriv_mse = -2 * (y[j] - o0);
  53.  
  54.             double dW5 = h1 * derivsigmoid(o0);
  55.             double dW4 = h0 * derivsigmoid(o0);
  56.             double dB3 = derivsigmoid(o0);
  57.             double dH0 = w4 * derivsigmoid(o0);
  58.             double dH1 = w5 * derivsigmoid(o0);
  59.  
  60.             double dW0 = x[0] * derivsigmoid(h0);
  61.             double dW1 = x[1] * derivsigmoid(h0);
  62.             double dB1 = derivsigmoid(h0);
  63.  
  64.             double dW2 = x[0] = derivsigmoid(h1);
  65.             double dW3 = x[1] = derivsigmoid(h1);
  66.             double dB2 = derivsigmoid(h1);
  67.  
  68.         // aktualizacja wag
  69.             w0 -= (deriv_mse*learn_rate*dH0*dW1);
  70.             w1 -= (deriv_mse*learn_rate*dH0*dW2);
  71.             b1 -= (deriv_mse*learn_rate*dH0*dB1);
  72.  
  73.             w2 -= (deriv_mse*learn_rate*dH1*dW3);
  74.             w3 -= (deriv_mse*learn_rate*dH1*dW4);
  75.             b2 -= (deriv_mse*learn_rate*dH1*dB2);
  76.  
  77.             w4 -= (deriv_mse*learn_rate*dW4);
  78.             w5 -= (deriv_mse*learn_rate*dW5);
  79.             b3 -= (deriv_mse*learn_rate*dB3);
  80.  
  81.         }
  82.         if(i%n==0) // co ile n wypisac dane
  83.         {
  84.             double ypred[4];
  85.             for(int j=0; j<4; j++)
  86.             {
  87.                 ypred[j] = feedforward(x[j]);
  88.             }
  89.             double loss = mse_loss(y, ypred, 4);
  90.            
  91.            
  92.             cout << "epochs: " << i << endl;
  93.             for(int j=0; j<4; j++)
  94.                 cout << ypred[j] << endl;
  95.             cout << loss << endl;
  96.            
  97.             //print mse, ypred
  98.         }
  99.     }
  100.  
  101. }
  102.  
  103. int main()
  104. {
  105.     srand(time(NULL));
  106.     w0 = random();
  107.     w1 = random();
  108.     w2 = random();
  109.     w3 = random();
  110.     w4 = random();
  111.     w5 = random();
  112.     b1 = random();
  113.     b2 = random();
  114.     b3 = random();
  115.  
  116.     cout << w1 << endl;
  117.  
  118.     return 0;
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement