Th3NiKo

Sieci neuronowe - ćwiczenia 7

May 6th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. float xs[25] = {};
  10. float c[25][25] {};
  11. float w[25][25] = {};
  12. float O[25] = {};
  13. float x[25] = {};
  14. float u[25] = {};
  15.  
  16. void DrawX(){
  17. int counter = 0;
  18. for(int i = 0; i < 5; i++){
  19.     for(int j = 0; j < 5; j++){
  20.         if(x[counter] == 0){
  21.             cout << "#";
  22.         } else {
  23.             cout << "*";
  24.         }
  25.         counter++;
  26.     }
  27.     cout << endl;
  28. }
  29.  
  30. }
  31.  
  32. float CalculateTheta(int number){
  33.  
  34.     float sum = 0.0f;
  35.     for(int i = 0; i < 25; i++){
  36.         sum += c[number][i];
  37.     }
  38.     return sum;
  39.  
  40. }
  41.  
  42. void CalculateC(){
  43.     for(int i = 0;i < 25; i++){
  44.         for(int j = 0; j < 25; j++){
  45.             if(i != j){
  46.                 c[i][j] = (xs[i] - 0.5f) * (xs[j] - 0.5f);
  47.             } else {
  48.                 c[i][j] = 0.0f;
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. void SetTheta(){
  55.     for(int i = 0; i < 25; i++){
  56.         O[i] = CalculateTheta(i);
  57.     }
  58. }
  59. void SetW(){
  60.     for(int i = 0; i < 25; i++){
  61.         for(int j = 0; j < 25; j++){
  62.             w[i][j] = 2 * c[i][j];
  63.         }
  64.     }
  65. }
  66.  
  67. void RandomizeX(){
  68.     for(int i = 0; i < 25; i++){
  69.         int temp = rand() % 2; // 50%
  70.         if(temp == 0){
  71.             x[i] = 1.0f;
  72.         } else {
  73.             x[i] = 0.0f;
  74.         }
  75.     }
  76. }
  77.  
  78. float CalculateU(){
  79.     for(int i = 0; i < 25; i++){
  80.         float sum = 0.0f;
  81.         for(int j = 0; j < 25; j++){
  82.             sum += (w[i][j] * x[j]) - O[i];
  83.         }
  84.         u[i] = sum;
  85.     }
  86.  
  87. }
  88.  
  89.  
  90.  
  91. int main()
  92. {
  93.     srand (time(NULL));
  94.     //Image draw [1]
  95.     xs[6] = 1.0f;
  96.     xs[7] = 1.0f;
  97.     xs[12] = 1.0f;
  98.     xs[17] = 1.0f;
  99.     xs[22] = 1.0f;
  100.  
  101.     CalculateC();
  102.     SetTheta();
  103.     SetW();
  104.     RandomizeX(); //First iteration
  105.  
  106.     int t = 0;
  107.     bool flag = true;
  108.  
  109.     while(flag){
  110.         cout << "Iteration neci umber: " << t << endl;
  111.         DrawX();
  112.         cout << endl;
  113.         CalculateU();
  114.         for(int i = 0; i < 25; i++){
  115.             if(u[i] > 0.0){
  116.                 x[i] = 1.0f;
  117.             } else if(u[i] < 0.0){
  118.                 x[i] = 0.0f;
  119.             }
  120.         }
  121.  
  122.  
  123.         t++;
  124.         getch();
  125.     }
  126.  
  127.  
  128.  
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment