Updating functions

This commit is contained in:
chabisik 2022-01-02 02:33:35 +01:00
parent 5534661b91
commit 26ae13b72e
3 changed files with 107 additions and 6 deletions

View file

@ -14,11 +14,8 @@ int main(int argc, char *argv[])
cout << "Bonjour et bienvenu" << endl;
vector<int> v = {1,2,3,4};
cout << "size = " << v.size() << endl;
cout << "size of bool = " << sizeof(bool) << endl;
Neuron n0(3,SIGMOID);
/*Neuron n0(3,SIGMOID);
Neuron n1(3,RELU);n1.set_output(1.0);
Neuron n2(3,RELU);n2.set_output(2.0);
@ -28,6 +25,7 @@ int main(int argc, char *argv[])
forward_list<Neuron>::iterator it(fl.begin());
n0.activate(it);
cout << "is = " << n0.get_output() << endl;
cout << "is = " << n0.get_output() << endl;*/
Network(4, 5);
return 0;
}

View file

@ -57,6 +57,108 @@ float Neuron::get_output()//to be deleted later
return output;
}
Network::Network(int n_layers, int n_neurons)
{
for(int i(1) ; i<=n_layers ; i++)
{
forward_list<Neuron> current_layer;
for(int j(1) ; j<=n_neurons ; j++)
{
if(i==1)
{
current_layer.push_front( Neuron(0, LINEAR) );
}else if(i==n_layers)
{
current_layer.push_front( Neuron(n_neurons, SIGMOID) );
}else
{
current_layer.push_front( Neuron(n_neurons, RELU) );
}
}
layers.push_back(current_layer);
}
h_activ = RELU;
o_activ = SIGMOID;
}
Network::Network(const std::vector<int> &n_neurons, Activ h_activ, Activ o_activ)
{
for(int i(0) ; i<n_neurons.size() ; i++)
{
forward_list<Neuron> current_layer;
for(int j(1) ; j<=n_neurons[i] ; j++)
{
if(i==0)
{
current_layer.push_front( Neuron(0, LINEAR) );
}else if(i==n_neurons.size()-1)
{
current_layer.push_front( Neuron(n_neurons[i-1], o_activ) );
}else
{
current_layer.push_front( Neuron(n_neurons[i-1], h_activ) );
}
}
layers.push_back(current_layer);
}
h_activ = h_activ;
o_activ = o_activ;
}
void Network::print()
{
cout << endl << "#>>==========================================<<#" << endl;
cout << "# NEURAL NETWORK #" << endl;
cout << "#>>==========================================<<#" << endl;
cout << ">> Number of layers : " << layers.size() << endl;
cout << "------------------------------------------------" << endl;
for(list<forward_list<Neuron>>::iterator it1(layers.begin()) ; it1!=layers.end() ; ++it1)
{
int current_layer_size = 0;
for(forward_list<Neuron>::iterator it2(it1) ; it2!=it1.end() ; ++it2)
{
current_layer_size++;
}
if(i==0)
{
cout << ">> Input layer" << endl;
cout << "size : " << layers << endl;
cout << "neurons' outputs : ";
temp = network->layers_first_neurons[i];
while(temp != NULL)
{
cout << ("%f ", temp->output);
temp = temp->same_layer_next_neuron;
}
cout << ("\n");
}else if(i==layers.size()-1)
{
cout << (">> Output layer\n");
cout << ("size : %d\n", network->neurons_per_layer[i]);
cout << ("neurons' outputs : ");
temp = network->layers_first_neurons[i];
while(temp != NULL)
{
cout << ("%f ", temp->output);
temp = temp->same_layer_next_neuron;
}
cout << ("\n");
}else
{
cout << (">> Hidden layer %d\n", i);
cout << ("size : %d\n", network->neurons_per_layer[i]);
}
cout << ("------------------------------------------------\n");
}
cout << ("Number of parameters : ");
for(i=1 ; i<network->n_layers ; i++)
{
n_params += network->neurons_per_layer[i] * (network->neurons_per_layer[i-1] + 1);
}
cout << ("%d\n", n_params);
cout << "#>>==========================================<<#" << endl << endl;
}
void Tools::activate_randomness()
{
srand(time(NULL));

View file

@ -30,8 +30,9 @@ private:
class Network
{
public:
Network(int n_neurons);
Network(int n_layers, int n_neurons);
Network(const std::vector<int> &n_neurons, Activ h_activ=RELU, Activ o_activ=SIGMOID);
void print() const;
bool forward(const std::vector<float> &input, const std::vector<float> &target);
bool backward();
private: