Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <xtensor/xarray.hpp>
- #include <xtensor/xio.hpp>
- #include <xtensor/xview.hpp>
- #include <xtensor.blas/xlinalg.hpp>
- #include <xtensor/xrandom.hpp>
- class LayerDense{
- public:
- Layer_Dense(size_t n_inputs, size_t n_neurons);
- void forward(const xt::xarray<float>& inputs);
- private:
- xt::array<float> weights;
- xt::array<float> biases;
- xt::array<float> output;
- }
- xt::array<float> dot_product_with_biases(const xt::array<float>& inputs,
- const xt::array<float>& weights,
- const xt::array<float>& biases){
- xt::xarray<float> transposed_weights = xt::transpose(weights);
- xt::xarray<float> dot_product_result = xt::linalg::dot(inputs, transposed_weights);
- xt::xarray<float> output = dot_product_result + biases;
- return output;
- }
- Layer_Dense::Layer_Dense(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});
- }
- void Layer_Dense::forward(const xt::xarray<float>& inputs){
- output = xt::linalg::dot(inputs, weights) + biases;
- return output;
- }
- int main(){
- xt::xarray<float> inputs = {
- {1.,1.,3.,2.5},
- {2.,5.,-1.,2.},
- {-1.5,2.7,3.3,-0.8}
- };
- xt::xarray<float> weights1 = {
- {.2,.8,-.5,1.}
- {.5,-.91,.26,-.5}
- {-.26,-.27,.17,.87}
- };
- xt::xarray<float> weights2 = {
- {.2,.8,-.5,1.}
- {.5,-.91,.26,-.5}
- {-.26,-.27,.17,.87}
- };
- xt::xarray<float> biases1 = {2.,3.,.5};
- xt::xarray<float> biases2 = {};
- LayerDense layer1(input_neurons, output_neurons);
- //run the forward pass
- layer1.forward(inputs);
- xt::array<float> layer1_results = dot_product_with_biases(inputs, weights1, biases1);
- xt::array<float> results = dot_product_with_biases(layer1_results, weights2, biases2);
- printf("product: %.2f\n", results);
- return 0;
- }
- //random number code in xtensor
- unsigned short n_inputs = ;
- unsigned short n_neurons = ;
- xt::array<float> random_numbers = 0.10 * xt::random::randn<float>({n_inputs, n_neurons});
- xt::array<float> zeros_function = xt::zeros<float>({1, n_neurons});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement