Advertisement
Trainlover08

Trying to add a second neural layer to the existing algorithm

Mar 20th, 2024 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <xtensor/xarray.hpp>
  4. #include <xtensor/xio.hpp>
  5. #include <xtensor/xview.hpp>
  6. #include <xtensor.blas/xlinalg.hpp>
  7. #include <xtensor/xrandom.hpp>
  8.  
  9. class LayerDense{    
  10. public:
  11.     Layer_Dense(size_t n_inputs, size_t n_neurons);
  12.    
  13.     void forward(const xt::xarray<float>& inputs);
  14. private:
  15.     xt::array<float> weights;
  16.     xt::array<float> biases;
  17.     xt::array<float> output;
  18. }
  19.    
  20.    
  21. xt::array<float> dot_product_with_biases(const xt::array<float>& inputs,
  22.                                          const xt::array<float>& weights,
  23.                                          const xt::array<float>& biases){
  24.     xt::xarray<float> transposed_weights = xt::transpose(weights);
  25.     xt::xarray<float> dot_product_result = xt::linalg::dot(inputs, transposed_weights);
  26.     xt::xarray<float> output = dot_product_result + biases;
  27.    
  28.     return output;
  29. }
  30.  
  31. Layer_Dense::Layer_Dense(size_t n_inputs, size_t n_neurons){
  32.     weights = 0.10 * xt::random::randn<float>({n_inputs, n_neurons});
  33.     biases = xt::zeros<float>({1, n_neurons});
  34. }
  35.  
  36. void Layer_Dense::forward(const xt::xarray<float>& inputs){
  37.     output = xt::linalg::dot(inputs, weights) + biases;
  38.    
  39.     return output;
  40. }
  41.  
  42.  
  43.  
  44. int main(){
  45.     xt::xarray<float> inputs = {
  46.                                 {1.,1.,3.,2.5},
  47.                                 {2.,5.,-1.,2.},
  48.                                 {-1.5,2.7,3.3,-0.8}
  49.                                 };
  50.     xt::xarray<float> weights1 = {
  51.                                  {.2,.8,-.5,1.}
  52.                                  {.5,-.91,.26,-.5}
  53.                                  {-.26,-.27,.17,.87}
  54.                                  };
  55.    
  56.     xt::xarray<float> weights2 = {
  57.                                  {.2,.8,-.5,1.}
  58.                                  {.5,-.91,.26,-.5}
  59.                                  {-.26,-.27,.17,.87}
  60.                                  };
  61.    
  62.     xt::xarray<float> biases1 = {2.,3.,.5};
  63.    
  64.     xt::xarray<float> biases2 = {};
  65.    
  66.     LayerDense layer1(input_neurons, output_neurons);
  67.    
  68.     //run the forward pass
  69.     layer1.forward(inputs);
  70.    
  71.     xt::array<float> layer1_results = dot_product_with_biases(inputs, weights1, biases1);
  72.    
  73.     xt::array<float> results = dot_product_with_biases(layer1_results, weights2, biases2);
  74.    
  75.     printf("product: %.2f\n", results);
  76.    
  77.     return 0;
  78. }
  79.  
  80. //random number code in xtensor
  81. unsigned short n_inputs = ;
  82. unsigned short n_neurons = ;
  83.  
  84. xt::array<float> random_numbers = 0.10 * xt::random::randn<float>({n_inputs, n_neurons});
  85. xt::array<float> zeros_function = xt::zeros<float>({1, n_neurons});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement