Advertisement
Guest User

singlethread

a guest
Mar 23rd, 2011
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <time.h>
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. #define LAYERS 100
  8. #define NEURONS 100
  9. #define INPUTS 100
  10.  
  11. unsigned long long getRealNanosecondsCount(timespec time)
  12. {
  13.     return (unsigned long long)time.tv_sec * 1000000000 + time.tv_nsec;
  14. }
  15.  
  16. float sigmoid(float x)
  17. {
  18.   return x/100;
  19. }
  20.  
  21. int main()
  22. {
  23.   struct timespec current_time;
  24.   struct timespec next_time;
  25.  
  26.   srand((unsigned)time(0));
  27.  
  28.   // array of inputs weights
  29.   float ** weights = new float * [LAYERS];
  30.   for(int i=0;i<LAYERS;i++)
  31.   {
  32.       weights[i] = new float[NEURONS*INPUTS];
  33.  
  34.       for(int j=0;j<NEURONS*INPUTS;j++)
  35.       {
  36.         weights[i][j] = (float)rand() / RAND_MAX;
  37.       }
  38.   }
  39.   //--------------------------
  40.   // array of inputs values
  41.   float * inputs = new float[INPUTS];
  42.   for(int i=0;i<INPUTS;i++)
  43.   {
  44.     inputs[i] = (float)rand() / RAND_MAX;
  45.   }
  46.   //---------------------------
  47.   // array of outputs
  48.   float * outputs = new float[NEURONS];
  49.   //---------------------------
  50.   float * temp = new float[INPUTS*NEURONS];
  51.  
  52.   cout << "Memory for weights: " << sizeof(float)*LAYERS*NEURONS*INPUTS << endl;
  53.   cout << "Memory for inputs: " << sizeof(float)*INPUTS << endl;
  54.   cout << "Memory for outputs: " << sizeof(float)*NEURONS << endl;
  55.   cout << "Memory for temp: " << sizeof(float)*NEURONS*INPUTS << endl;
  56.  
  57.   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &current_time);
  58.  
  59.   for(int i=0;i<LAYERS;i++)
  60.   {
  61.     int k = 0;
  62.     for(int j=0;j<INPUTS*NEURONS;j++)
  63.     {
  64.       temp[j] = inputs[k] * weights[i][j];
  65.      
  66.       k++;
  67.      
  68.       if(k >= INPUTS)
  69.         k = 0;
  70.     }
  71.    
  72.     for(int j=0;j<NEURONS;j++)
  73.     {
  74.       inputs[j] = 0;
  75.      
  76.       for(int l=0;l<INPUTS;l++)
  77.       {
  78.         inputs[j] += temp[j*INPUTS + l];
  79.       }
  80.      
  81.       inputs[j] = sigmoid(inputs[j]);
  82.     }
  83.   }
  84.  
  85.   clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &next_time);
  86.  
  87.   for(int i=0;i<NEURONS;i++)
  88.   {
  89.     cout << inputs[i] << endl;
  90.   }
  91.  
  92.   delete[] temp;
  93.   delete[] outputs;
  94.   delete[] inputs;
  95.  
  96.   for(int i=0;i<LAYERS;i++)
  97.   {
  98.       delete[] weights[i];
  99.   }
  100.   delete[] weights;
  101.  
  102.   cout << "Time: " << getRealNanosecondsCount(next_time) - getRealNanosecondsCount(current_time) << endl;
  103.  
  104.   return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement