Make ml::neural_net return spans
This commit is contained in:
parent
0ab0ce9134
commit
d37edd207b
2 changed files with 8 additions and 10 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <psemek/ml/activation.hpp>
|
#include <psemek/ml/activation.hpp>
|
||||||
|
#include <psemek/util/span.hpp>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
@ -52,19 +53,16 @@ namespace psemek::ml
|
||||||
// A non-empty neural net is basically unusable
|
// A non-empty neural net is basically unusable
|
||||||
bool empty() const { return layer_sizes_.empty(); }
|
bool empty() const { return layer_sizes_.empty(); }
|
||||||
|
|
||||||
std::size_t layer_count() const { return layer_sizes_.size(); }
|
util::span<std::size_t const> layer_sizes() const { return layer_sizes_; }
|
||||||
std::size_t const * layer_sizes() const { return layer_sizes_.data(); }
|
|
||||||
|
|
||||||
std::size_t activation_type_count() const { return activation_types_.size(); }
|
util::span<activation_type const> activation_types() const { return activation_types_; }
|
||||||
activation_type const * activation_types() const { return activation_types_.data(); }
|
|
||||||
|
|
||||||
// Weights are stored in a sequential manner, in the order in which they
|
// 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
|
// 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
|
// between layers 0 and 1 (including the bias) in row-major order, then
|
||||||
// 1-2 in row-major order, etc.
|
// 1-2 in row-major order, etc.
|
||||||
std::size_t weight_count() const { return weights_.size(); }
|
util::span<T const> weights() const { return weights_; }
|
||||||
T const * weights() const { return weights_.data(); }
|
util::span<T> weights() { return weights_; }
|
||||||
T * weights() { return weights_.data(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::size_t> layer_sizes_;
|
std::vector<std::size_t> layer_sizes_;
|
||||||
|
|
|
||||||
|
|
@ -29,13 +29,13 @@ namespace psemek::ml
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::vector<T> neural_net_evaluator<T>::evaluate(neural_net<T> const & nn, std::vector<T> input) const
|
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();
|
auto layer_sizes = nn.layer_sizes();
|
||||||
T const * weights = nn.weights();
|
auto weights = nn.weights().begin();
|
||||||
|
|
||||||
if (layer_sizes[0] != input.size())
|
if (layer_sizes[0] != input.size())
|
||||||
throw wrong_neural_net_input_size{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]);
|
temp_.resize(layer_sizes[l + 1]);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue