Advertisement
Aftaza

Untitled

Dec 3rd, 2023
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6. int model[3] = {1, 1, 1};
  7.  
  8. int getSum(int length, int *arr){
  9.     int sum = 0;
  10.     for(int i = 0; i < length ; i++){
  11.         sum+=arr[i];
  12.     }
  13.     return sum;
  14. }
  15.  
  16. void sortArr(int *arr, int len){
  17.     int array[len];
  18.     for (int i = 0; i < len; i++)
  19.     {
  20.         array[i] = arr[i];
  21.     }
  22.     sort(array, array + len, greater<int>());
  23.     model[0] = array[0];
  24.     model[1] = array[1];
  25.     model[2] = array[2];
  26. }
  27.  
  28. int average(int *loss, int len){
  29.     int avg;
  30.     for (int i = 0; i < len; i++)
  31.     {
  32.         avg += loss[i];
  33.     }
  34.     avg = avg / len;
  35.     return avg;
  36. }
  37.  
  38. void training(int epoch, int *train, int *valid, int *test){
  39.     int loss[epoch];
  40.     int lenTrain = sizeof(train)/sizeof(int);
  41.     int lenValid = sizeof(valid)/sizeof(int);
  42.     int lenTest = sizeof(test)/sizeof(int);
  43.     int tempModel[lenTrain];
  44.     int sumModel = 1000;
  45.     int sumTrain = getSum(lenTrain, train);
  46.     int sumValid = getSum(lenValid, valid);
  47.     int sumTest = getSum(lenTest, test);
  48.     for (int i = 1; i <= epoch; i++)
  49.     {
  50.         loss[i-1] = sumModel * 100 / (epoch * (sumTrain + sumValid + sumTest));
  51.         for(int it = 0; it < lenTest; it++)
  52.         {
  53.             for (int iv = 0; iv < lenValid; iv++)
  54.             {
  55.                 for (int iT = 0; iT < lenTrain; iT++)
  56.                 {
  57.                     tempModel[iT] = (train[iT] + valid[iv]) * test[it];
  58.                 }
  59.             }
  60.         }
  61.         sumModel = getSum(lenTrain, tempModel);
  62.         cout << "[Epoch-" << i << "/" << epoch << "] Loss " << loss[i-1] << "%" << endl;
  63.         sortArr(tempModel, lenTrain);
  64.     }
  65.     cout << "[Result] Algorithm CNN - avg Loss " << average(loss, epoch) << "%" << endl;
  66.     cout << "Model Output [" << model[0] << ", " << model[1] << ", " << model[2] << "] " << endl;
  67. }
  68.  
  69. void generateData(int max, int *array){
  70.     for (int i = 0; i < max; i++)
  71.     {
  72.         array[i] = rand() % 100;
  73.     }
  74. }
  75.  
  76. int main() {
  77.     int epoch;
  78.     int trainMax;
  79.     int validMax;
  80.     int testMax;
  81.     string algorithm;
  82.  
  83.     cin >> algorithm >> epoch;
  84.     cin >> trainMax >> validMax >> testMax;
  85.  
  86.     int dataTrain[trainMax];
  87.     int dataValid[validMax];
  88.     int dataTest[testMax];
  89.  
  90.     if (algorithm == "CNN")
  91.     {
  92.         generateData(trainMax, dataTrain);
  93.         generateData(validMax, dataValid);
  94.         generateData(testMax, dataTest);
  95.         training(epoch, dataTrain, dataValid, dataValid);
  96.     } else {
  97.         cout << "Error: Algorithm not found." << endl;
  98.     }
  99.    
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement