API Reference‎ > ‎


template< class Net, class Input, class Targetclass nn_trainer_t

        This template trainer class is a generic helper for neural networks training
        Net is the neural network class type
        Input and Target are input and output class types



  • nu

Inner classes


  • nn_trainer_t(Net & nnsize_t epochsdouble min_err );

    Creates a neural network trainer

    • nn is a reference to training network
    • epochs: should be the max epoch count at which to stop training
    • min_errmin error value at which to stop training

Public methods

  • size_t get_epochs() const noexcept;
    Return the max number of epochs
  • double get_min_err() const noexcept;
    Return the min error value at which to stop training
  • double get_error() const noexcept;
    Return current epoch error
  • bool train(const Input& input, const Target& target, cost_func_t err_cost_f);
    const Inputinput:    [in] sample input
    const Targettarget:  [in] target output corresponding the given input
    cost_func_t err_cost_f:
    [in] function object which is used to calculate the error at each iteration

    Train the net using a single sample. Return true if get_error() < get_min_error()
  • template <class TSet> 
    size_t train(const TSet& training_set, cost_func_t err_cost_f,  progress_cbk_t * progress_cbk = nullptr
    TSet is a generic collection of std::pair of <Input,Target>
    const TSettraining_set:      [in] training samples set
    cost_func_t err_cost_f:        [in] function object which is used to calculate the error at each iteration
    progress_cbk_t * progress_cbk: [in, opt.] if specified is called back for each iteration.
    This template method trains the net using a training set of samples, repeating the training procedure for a max number of epoch equal to get_epochs()
    Every epoch it repeats the training for each sample in the set.
    Training procedure terminates if err_cost_f() return an error value smaller than get_min_err().
    The method returns the number of last epoch within the range 0..get_epochs(). If return value equals get_epochs() then the min error is not reached and training procedure terminated because the maximum number of epochs has been completed.


Training the XOR function using an MLP
In this example the training set consists in a map of pair K,V=std::vector<double>, std::vector<double>
It could be also a std::list or a std::vector of std::pair<std::vector<double>, std::vector<double>> or any other iterable object of std::pair<...>

// Construct a MLP network specifying topology, learning rate and momentum

nu::mlp_neural_net_t nn {

   // Topology

   { 2, // input layer takes a two dimensional vector as input
     2, // hidden layer size
     1  // output

   0.4, // learning rate
   0.9, // momentum

// Create a trainer for nu::mlp_neural_net_t class

   nu::mlp_neural_net_t,             //Net
   nu::mlp_neural_net_t::rvector_t,  //Input
   nu::mlp_neural_net_t::rvector_t>  //Target
   trainer(nn, 20000 /* epochs */, 0.01 /* min err */);

// Create a training set for XOR-function

using training_set_t = std::map< std::vector<double>, std::vector<double> >;

training_set_t traing_set = {
    { { 0, 0 },{ 0 } },
    { { 0, 1 },{ 1 } },
    { { 1, 0 },{ 1 } },
    { { 1, 1 },{ 0 } }

// Perform training

   [](nu::mlp_neural_net_t& net, const nu::mlp_neural_net_t::rvector_t & target) -> double
{ return net.mean_squared_error(target); });