Make ml::neural_net return spans

This commit is contained in:
Nikita Lisitsa 2022-01-20 13:52:13 +03:00
parent 0ab0ce9134
commit d37edd207b
2 changed files with 8 additions and 10 deletions

View file

@ -1,6 +1,7 @@
#pragma once
#include <psemek/ml/activation.hpp>
#include <psemek/util/span.hpp>
#include <vector>
#include <stdexcept>
@ -52,19 +53,16 @@ namespace psemek::ml
// A non-empty neural net is basically unusable
bool empty() const { return layer_sizes_.empty(); }
std::size_t layer_count() const { return layer_sizes_.size(); }
std::size_t const * layer_sizes() const { return layer_sizes_.data(); }
util::span<std::size_t const> layer_sizes() const { return layer_sizes_; }
std::size_t activation_type_count() const { return activation_types_.size(); }
activation_type const * activation_types() const { return activation_types_.data(); }
util::span<activation_type const> activation_types() const { return activation_types_; }
// Weights are stored in a sequential manner, in the order in which they
// appear in evalution of the neural net, i.e. first come the weights
// between layers 0 and 1 (including the bias) in row-major order, then
// 1-2 in row-major order, etc.
std::size_t weight_count() const { return weights_.size(); }
T const * weights() const { return weights_.data(); }
T * weights() { return weights_.data(); }
util::span<T const> weights() const { return weights_; }
util::span<T> weights() { return weights_; }
private:
std::vector<std::size_t> layer_sizes_;

View file

@ -29,13 +29,13 @@ namespace psemek::ml
template <typename T>
std::vector<T> neural_net_evaluator<T>::evaluate(neural_net<T> const & nn, std::vector<T> input) const
{
std::size_t const * layer_sizes = nn.layer_sizes();
T const * weights = nn.weights();
auto layer_sizes = nn.layer_sizes();
auto weights = nn.weights().begin();
if (layer_sizes[0] != input.size())
throw wrong_neural_net_input_size{layer_sizes[0], input.size()};
for (std::size_t l = 0; l + 1 < nn.layer_count(); ++l)
for (std::size_t l = 0; l + 1 < layer_sizes.size(); ++l)
{
temp_.resize(layer_sizes[l + 1]);