Advertisement
Trainlover08

2nd step ChatGPT output

Mar 21st, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <xtensor/xarray.hpp>
  3. #include <xtensor/xrandom.hpp>
  4. #include <xtensor/xview.hpp>
  5. #include <xtensor/xio.hpp>
  6. #include <xtensor/xlinalg.hpp>
  7.  
  8. class LayerDense {
  9. public:
  10.     LayerDense(size_t n_inputs, size_t n_neurons);
  11.    
  12.     xt::xarray<float> forward(const xt::xarray<float>& inputs);
  13.    
  14. private:
  15.     xt::xarray<float> weights;
  16.     xt::xarray<float> biases;
  17. };
  18.  
  19. LayerDense::LayerDense(size_t n_inputs, size_t n_neurons) {
  20.     weights = 0.10 * xt::random::randn<float>({n_inputs, n_neurons});
  21.     biases = xt::zeros<float>({1, n_neurons});
  22. }
  23.  
  24. xt::xarray<float> LayerDense::forward(const xt::xarray<float>& inputs) {
  25.     return xt::linalg::dot(inputs, weights) + biases;
  26. }
  27.  
  28. int main() {
  29.     // Define input data
  30.     xt::xarray<float> inputs = {
  31.         {1., 1., 3., 2.5},
  32.         {2., 5., -1., 2.},
  33.         {-1.5, 2.7, 3.3, -0.8}
  34.     };
  35.  
  36.     // Define weights and biases for the layers
  37.     xt::xarray<float> weights1 = {
  38.         {0.2, 0.8, -0.5, 1.},
  39.         {0.5, -0.91, 0.26, -0.5},
  40.         {-0.26, -0.27, 0.17, 0.87}
  41.     };
  42.  
  43.     xt::xarray<float> weights2 = {
  44.         {0.2, 0.8, -0.5, 1.},
  45.         {0.5, -0.91, 0.26, -0.5},
  46.         {-0.26, -0.27, 0.17, 0.87}
  47.     };
  48.  
  49.     xt::xarray<float> biases1 = {2., 3., 0.5};
  50.     xt::xarray<float> biases2 = {1., .4, 4};
  51.  
  52.     // Create the layers
  53.     LayerDense layer1(inputs.shape()[1], biases1.shape()[0]);
  54.     LayerDense layer2(layer1.forward(inputs).shape()[1], biases2.shape()[0]);
  55.  
  56.     // Perform forward pass through the layers
  57.     xt::xarray<float> layer1_output = layer1.forward(inputs);
  58.     xt::xarray<float> layer2_output = layer2.forward(layer1_output);
  59.  
  60.     // Display the output
  61.     std::cout << "Layer 1 output:\n" << layer1_output << std::endl;
  62.     std::cout << "Layer 2 output:\n" << layer2_output << std::endl;
  63.  
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement