Advertisement
Ryuuk

Neural_network

Nov 1st, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. // author: Ryuuk
  2. #include<bits/stdc++.h>
  3.  
  4. #define sz(a) int((a).size())
  5. #define pb push_back
  6. #define ZERO (1e-10)
  7. #define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
  8. #define all(c) (c).begin(),(c).end()
  9. #define tr(c,i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
  10. #define present(c,x) ((c).find(x) != (c).end())
  11. #define cpresent(c,x) (find(all(c),x) != (c).end())
  12. #define LSOne(i) (i&(-i))
  13. #define REP(i,a,b) for(int(i)=(a);(i)<(b);i++)
  14. #define BUG(x) {cout<<#x<<" = "<<x<<endl;}
  15. #define CL(A,I) (memset(A,I,sizeof(A)))
  16.  
  17.  
  18. static const int INF = 0x3f3f3f3f;
  19. static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
  20. static const long double epsilon = 1e-15;
  21. static const long double pi = acos((long double) -1);
  22. using namespace std;
  23.  
  24. typedef vector<int> vi;
  25. typedef vector<vi> vvi;
  26. typedef pair<int,int> ii;
  27. typedef long long ll;
  28.  
  29. inline void init_io(){ios_base::sync_with_stdio(false);cin.tie(NULL);}
  30.  
  31.  
  32. int x[3]={1, 2, 3};
  33. double t[3]={0.1, 0.3, 0.7};
  34. double W[2][3]={{0.5, 0.3, 0.1},{0.3, 0.2, 0.1}};
  35. double Z[3][2]={{0.1, 0.2},{0.3, 0.4},{0.5, 0.6}};
  36. double b[2],a[3];
  37. double h[2], o[3], e[3];
  38. double rho_sortie[3], rho_cahe[2];
  39.  
  40. void GoBack()
  41. {
  42.     double temM[3][2];
  43.  
  44.     for(int i=0;i<3;i++)
  45.         rho_sortie[i]=o[i]*(1-o[i])*e[i];
  46.  
  47.     memset(temM,0,sizeof temM);
  48.     for(int i=0;i<3;i++)
  49.         for(int j=0;j<2;j++)
  50.             temM[i][j]+=rho_sortie[i]*h[j];
  51.  
  52.     for(int i=0;i<3;i++)
  53.         for(int j=0;j<2;j++)
  54.             Z[i][j]+=temM[i][j];
  55.  
  56.     for(int i=0;i<2;i++)
  57.     {
  58.         double tmp=0;
  59.         for(int j=0;j<3;j++)
  60.             tmp+=Z[j][i]*rho_sortie[j];
  61.         rho_cahe[i]=h[i]*(1-h[i])*tmp;
  62.     }
  63.  
  64.     memset(temM,0,sizeof temM);
  65.     for(int i=0;i<2;i++)
  66.         for(int j=0;j<3;j++)
  67.             temM[i][j]+=rho_cahe[i]*x[j];
  68.     for(int i=0;i<3;i++)
  69.         for(int j=0;j<2;j++)
  70.             W[i][j]+=temM[i][j];    
  71.  
  72.  
  73. }
  74.  
  75.  
  76. bool first_step()
  77. {
  78.     for(int i=0;i<2;i++)
  79.         for(int j=0;j<3;j++)
  80.             b[i]+=(x[j]*W[i][j]);
  81.     for(int i=0;i<2;i++)
  82.         h[i]=1.0/(1+exp(-1.0*b[i]));
  83.     for(int i=0;i<3;i++)
  84.         for(int j=0;j<2;j++)
  85.             a[i]+=(h[j]*Z[i][j]);
  86.     for(int i=0;i<3;i++)
  87.         o[i]=1.0/(1+exp(-1.0*a[i]));
  88.     bool verif = true;
  89.     for(int i=0;i<3;i++)
  90.     {
  91.         e[i]=t[i]-o[i];
  92.         if(e[i]!=0.0)
  93.             verif=false;
  94.     }
  95.     return verif;
  96. }
  97.  
  98.  
  99. int main()
  100. {
  101.     init_io();
  102.     while(!first_step()){
  103.         GoBack();
  104.     }
  105.        
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement