Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <xtensor/xarray.hpp>
- #include <xtensor/xrandom.hpp>
- #include <xtensor/xview.hpp>
- #include <xtensor/xio.hpp>
- #include <xtensor/xlinalg.hpp>
- class LayerDense {
- public:
- LayerDense(size_t n_inputs, size_t n_neurons);
- xt::xarray<float> forward(const xt::xarray<float>& inputs);
- private:
- xt::xarray<float> weights;
- xt::xarray<float> biases;
- };
- LayerDense::LayerDense(size_t n_inputs, size_t n_neurons) {
- weights = 0.10 * xt::random::randn<float>({n_inputs, n_neurons});
- biases = xt::zeros<float>({1, n_neurons});
- }
- xt::xarray<float> LayerDense::forward(const xt::xarray<float>& inputs) {
- return xt::linalg::dot(inputs, weights) + biases;
- }
- int main() {
- // Define input data
- xt::xarray<float> 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
- xt::xarray<float> weights1 = {
- {0.2, 0.8, -0.5, 1.},
- {0.5, -0.91, 0.26, -0.5},
- {-0.26, -0.27, 0.17, 0.87}
- };
- xt::xarray<float> weights2 = {
- {0.2, 0.8, -0.5, 1.},
- {0.5, -0.91, 0.26, -0.5},
- {-0.26, -0.27, 0.17, 0.87}
- };
- xt::xarray<float> biases1 = {2., 3., 0.5};
- xt::xarray<float> biases2 = {1., .4, 4};
- // Create the layers
- LayerDense layer1(inputs.shape()[1], biases1.shape()[0]);
- LayerDense layer2(layer1.forward(inputs).shape()[1], biases2.shape()[0]);
- // Perform forward pass through the layers
- xt::xarray<float> layer1_output = layer1.forward(inputs);
- xt::xarray<float> 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