Advertisement
Trainlover08

Full linear algebra for the ai

Apr 11th, 2024 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4. #include <vector>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. vector<vector<float>> transpose(vector<vector<float>> input){
  10.     float input_rows = input.size();
  11.     float input_cols = input[0].size();
  12.     vector<vector<float>> output (input_cols, vector<float>(input_rows));
  13.     for(short i = 0; i < input_rows;){
  14.         for(short k = 0; k < input_cols;){
  15.             output[k][i] = input[i][k];
  16.             ++k;
  17.         }
  18.         ++i;
  19.     }
  20.     return output;
  21. }
  22.  
  23. vector<vector<float>> dotProduct (vector<vector<float>> inputs, vector<vector<float>> weights, vector<float> biases) {
  24.     short col_count = inputs.size();
  25.     vector<vector<float>> output (col_count);
  26.     for(short i = 0; i < col_count; ++i){
  27.         short row_count = inputs[i].size();
  28.         output[i].resize(row_count);
  29.         for(short k = 0; k < row_count; ++k){
  30.             output[i][k] = inputs[i][k] * weights[i][k] + biases[i];
  31.         }
  32.     }
  33.     return output;
  34. }
  35.  
  36. vector<vector<float>> full_dot(vector<vector<float>> inputs, vector<vector<float>> weights, vector<float> biases) {
  37.     return dotProduct(inputs, transpose(weights), biases);
  38. }
  39.  
  40. int main() {
  41.     vector<vector<float>> inputs {
  42.         {1.2, 3.5, 0.6, 0.91},
  43.         {4.3, 2.1, 1.9, 0.13},
  44.         {1.9, 1.2, 4.0, 0.20}
  45.                                  };
  46.     vector<vector<float>> weights {
  47.         {2, 4, 6.8, 1.3},
  48.         {3.4, 1.2, 3., 2.1},
  49.         {4.1, 5.3, 0.9, .12}
  50.     };
  51.     vector<float> biases {
  52.         1.2, 3, 0.1, 2.1
  53.     };
  54.    
  55.     vector<vector<float>> output = full_dot(inputs, weights, biases);
  56.    
  57.     for(char j; j < output.size(); ++j) {
  58.         for(char k; k < output[j].size(); ++k){
  59.             cout << output[j][k] << endl;
  60.         }
  61.     }
  62.    
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement