Updating Network functions

This commit is contained in:
chabisik 2022-02-07 00:57:27 +01:00
parent 756056f55c
commit 8fd19c2d8f
3 changed files with 115 additions and 51 deletions

View file

@ -3,6 +3,7 @@
#include <cmath>
#include <forward_list>
#include <algorithm>
#include <numeric>
#include "annclasses.h"
using namespace std;
@ -114,6 +115,8 @@ Network::Network(int n_layers, int n_neurons)
h_activ = RELU;
//o_activ = SIGMOID;
o_activ = LINEAR;
neurons_number = n_layers*n_neurons;
}
Network::Network(const std::vector<int> &n_neurons, Activ h_activ, Activ o_activ)
@ -138,16 +141,62 @@ Network::Network(const std::vector<int> &n_neurons, Activ h_activ, Activ o_activ
}
h_activ = h_activ;
o_activ = o_activ;
neurons_number = accumulate(n_neurons.begin(), n_neurons.end(), 0);
}
bool Network::train(const std::vector<float> &input, const std::vector<float> &target, float learning_rate, int n_episodes)
int Network::get_neurons_number()
{
return neurons_number;
}
bool Network::train(const vector<vector<float>> &inputs, const vector<vector<float>> &targets, float learning_rate, int n_episodes, int batch_size)
{
if(inputs.size() == targets.size())
{
vector<vector<float>> all_activated_outputs(get_neurons_number());
vector<vector<float>> all_derrors(get_neurons_number()-inputs.at(0).size());
bool is_constructed = false;
for(int episode=1 ; episode<=n_episodes ; episode++)
{
forward(input, target);
set_errors(target);
for(int index(0) ; index<inputs.size() ; index++)//batch_size not yet used
{
forward(inputs.at(index), targets.at(index));
set_errors(targets.at(index));
int layer_counter = 0;
int neurons_counter1 = 0;
int neurons_counter2 = 0;
for(list<forward_list<Neuron>>::iterator current_layer(layers.begin()) ; current_layer!=layers.end() ; ++current_layer)
{
layer_counter++;
if(layer_counter==1)
{
for(forward_list<Neuron>::iterator current_neuron(current_layer->begin()) ; current_neuron!=current_layer->end() ; ++current_neuron)
{
all_activated_outputs.at(neurons_counter1).push_back( current_neuron->get_activated_output() );
neurons_counter1++;
}
}else
{
for(forward_list<Neuron>::iterator current_neuron(current_layer->begin()) ; current_neuron!=current_layer->end() ; ++current_neuron)
{
all_activated_outputs.at(neurons_counter1).push_back( current_neuron->get_activated_output() );
neurons_counter1++;
all_derrors.at(neurons_counter2).push_back( current_neuron->get_derror() );
neurons_counter2++;
}
}
}
}
backward(learning_rate);
}
}else
{
cerr << "Inputs and targets vectors have different size" << endl;
exit(-1);
}
return true;
}
@ -252,7 +301,10 @@ bool Network::backward(float learning_rate)
bool neuron_cmp(Neuron a, Neuron b){return a.get_activated_output()<b.get_activated_output();}
float Network::predict(const std::vector<float> &input, bool as_raw)
vector<float> Network::predict(const vector<vector<float>> &inputs, bool as_raw)
{
vector<float> results;
for(auto input : inputs)
{
int layer_counter = 0;
for(list<forward_list<Neuron>>::iterator current_layer(layers.begin()) ; current_layer!=layers.end() ; ++current_layer)
@ -278,7 +330,7 @@ float Network::predict(const std::vector<float> &input, bool as_raw)
}else
{
list<forward_list<Neuron>>::iterator temp_prev_layer = current_layer; //temp_prev_layer set at current layer
temp_prev_layer--; ////temp_prev_layer set now at previous layer
temp_prev_layer--; //temp_prev_layer set now at previous layer
for(forward_list<Neuron>::iterator current_neuron(current_layer->begin()) ; current_neuron!=current_layer->end() ; ++current_neuron)
{//inside current neuron
forward_list<Neuron>::iterator prev_layer_it(temp_prev_layer->begin());
@ -289,12 +341,14 @@ float Network::predict(const std::vector<float> &input, bool as_raw)
list<forward_list<Neuron>>::iterator output_layer = layers.end(); output_layer--;
if(as_raw)
{
return max_element(output_layer->begin(), output_layer->end(), neuron_cmp)->get_activated_output();
results.push_back( max_element(output_layer->begin(), output_layer->end(), neuron_cmp)->get_activated_output() );
}else
{
return distance( output_layer->begin(), max_element(output_layer->begin(),output_layer->end(),neuron_cmp) );
results.push_back( distance( output_layer->begin(), max_element(output_layer->begin(),output_layer->end(),neuron_cmp) ) );
}
}
return results;
}
void Network::print()
{

View file

@ -1,5 +1,5 @@
#ifndef MYCLASSES_H
#define MYCLASSES_H
#ifndef ANNCLASSES_H
#define ANNCLASSES_H
#include <forward_list>
#include <list>
@ -40,9 +40,11 @@ public:
Network(int n_layers, int n_neurons);
Network(const std::vector<int> &n_neurons, Activ h_activ=RELU, Activ o_activ=SIGMOID);
bool train(const std::vector<float> &input, const std::vector<float> &target, float learning_rate, int n_episodes);
int get_neurons_number();
float predict(const std::vector<float> &input, bool as_raw=true);
bool train(const std::vector<std::vector<float>> &inputs, const std::vector<std::vector<float>> &targets, float learning_rate=0.001, int n_episodes=30, int batch_size=32);
std::vector<float> predict(const std::vector<std::vector<float>> &inputs, bool as_raw=true);
void print();
//to be deleted
@ -51,6 +53,7 @@ public:
//bool backward(float learning_rate);
private:
std::list<std::forward_list<Neuron>> layers;
int neurons_number;
Activ h_activ;
Activ o_activ;

View file

@ -11,14 +11,21 @@ int main(int argc, char *argv[])
cout << "Bonjour et bienvenu" << endl;
Network network(15, 3);
Network network(15, 3);/*
network.print();
cout << endl << endl;
network.train({1.0,1.0,1.0}, {1.0,2.0,3.0}, 0.001, 100000);
//network.print();
cout << endl << endl;
network.print();
cout << "verdict : " << network.predict({1.0,1.0,1.0},false) << endl;
network.print();*/
cout << "Network has " << network.get_neurons_number() << " neurons" << endl;
for(auto e : network.predict({{1.0,1.0,1.0},{2.0,1.3,0.0}},false))
{
cout << e << " " << endl;
}
//vector<vector<float>> v(5);
//cout << v.at(0).size() << endl;
return 0;
}