Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Eigen/Dense>
- class LayerDense {
- public:
- LayerDense(size_t n_inputs, size_t n_neurons);
- Eigen::MatrixXf forward(const Eigen::MatrixXf& inputs);
- private:
- Eigen::MatrixXf weights;
- Eigen::MatrixXf biases;
- };
- LayerDense::LayerDense(size_t n_inputs, size_t n_neurons) {
- weights = 0.10 * Eigen::MatrixXf::Random(n_inputs, n_neurons);
- biases = Eigen::MatrixXf::Zero(1, n_neurons);
- }
- Eigen::MatrixXf LayerDense::forward(const Eigen::MatrixXf& inputs) {
- return inputs * weights + biases.replicate(inputs.rows(), 1);
- }
- int main() {
- // Define input data
- Eigen::MatrixXf inputs(3, 4);
- inputs << 1., 1., 3., 2.5,
- 2., 5., -1., 2.,
- -1.5, 2.7, 3.3, -0.8;
- // Define weights and biases for the layers
- Eigen::MatrixXf weights1(4, 3);
- weights1 << 0.2, 0.8, -0.5,
- 0.5, -0.91, 0.26,
- -0.26, -0.27, 0.17,
- 0.87, 0.2, -0.5;
- Eigen::MatrixXf weights2(3, 3);
- weights2 << 0.2, 0.8, -0.5,
- 0.5, -0.91, 0.26,
- -0.26, -0.27, 0.17;
- Eigen::MatrixXf biases1(1, 3);
- biases1 << 2., 3., 0.5;
- Eigen::MatrixXf biases2(1, 3);
- biases2 << 1., 0.4, 4.;
- // Create the layers
- LayerDense layer1(inputs.cols(), biases1.cols());
- LayerDense layer2(layer1.forward(inputs).cols(), biases2.cols());
- // Perform forward pass through the layers
- Eigen::MatrixXf layer1_output = layer1.forward(inputs);
- Eigen::MatrixXf layer2_output = layer2.forward(layer1_output);
- // Display the output
- std::cout << "Layer 1 output:\n" << layer1_output << std::endl;
- std::cout << "Layer 2 output:\n" << layer2_output << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement