Advertisement
Trainlover08

Part 2 ChatGPT output eigen

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