Rewrite
This commit is contained in:
parent
8a8d56cc02
commit
e9defa3b86
811
main.cpp
811
main.cpp
|
@ -1,7 +1,12 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdlib>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
class Synapse;
|
||||||
|
class Neuron;
|
||||||
|
class Layer;
|
||||||
|
class Input;
|
||||||
|
class Output;
|
||||||
|
class NeuralNetwork;
|
||||||
|
|
||||||
float RandomFloat(int min, int max)
|
float RandomFloat(int min, int max)
|
||||||
{
|
{
|
||||||
|
@ -11,473 +16,273 @@ float RandomFloat(int min, int max)
|
||||||
srand(time(0) + counter++ * 50);
|
srand(time(0) + counter++ * 50);
|
||||||
value = (rand() % ((max - min) * 100));
|
value = (rand() % ((max - min) * 100));
|
||||||
result = (float)value / 100.0 + (float)min;
|
result = (float)value / 100.0 + (float)min;
|
||||||
std::clog << "Function RandomFloat: Between " << min << " and " << max << " returned value is " << result << "\n";
|
return result;
|
||||||
return 1.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma region Sinaps
|
#pragma region Synapse
|
||||||
class Sinaps
|
class Synapse
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
float weight; // Ağırlık
|
float weight;
|
||||||
float value; // Değer
|
float value;
|
||||||
float bias; // Öteleme
|
float bias;
|
||||||
public:
|
public:
|
||||||
Sinaps();
|
Synapse();
|
||||||
~Sinaps();
|
|
||||||
Sinaps(float, float, float); // Kaydedilen değerleri yeniden yazabilmek için
|
|
||||||
void SetSinaps(float, float, float); // Sonradan tamamen değiştirebilmek için
|
|
||||||
void SetWeight(float);
|
|
||||||
void SetValue(float);
|
void SetValue(float);
|
||||||
|
void SetWeight(float);
|
||||||
void SetBias(float);
|
void SetBias(float);
|
||||||
float Fire();
|
float Fire();
|
||||||
};
|
};
|
||||||
|
|
||||||
Sinaps::Sinaps()
|
|
||||||
{
|
|
||||||
// weight = value = bias = 0.0;
|
|
||||||
this -> weight = RandomFloat(-1, 1);
|
|
||||||
this -> value = RandomFloat(-1, 1);
|
|
||||||
this -> bias = RandomFloat(-1, 1);
|
|
||||||
std::clog << "Create Sinaps: Weight = " << weight
|
|
||||||
<< " Value = " << value
|
|
||||||
<< " Bias = " << bias << "\n" << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
Sinaps::~Sinaps()
|
Synapse::Synapse()
|
||||||
{
|
{
|
||||||
std::clog << "Delete Sinaps: Weight = " << weight
|
this -> value = this -> weight = this -> bias = 1.0;
|
||||||
<< " Value = " << value
|
|
||||||
<< " Bias = " << bias << "\n";
|
|
||||||
}
|
|
||||||
Sinaps::Sinaps(float weight, float value, float bias)
|
|
||||||
{
|
|
||||||
this -> weight = weight;
|
|
||||||
this -> value = value;
|
|
||||||
this -> bias = bias;
|
|
||||||
std::clog << "Create Sinaps: Weight = " << weight
|
|
||||||
<< " Value = " << value
|
|
||||||
<< " Bias = " << bias << "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sinaps::SetSinaps(float weight, float value, float bias)
|
void Synapse::SetValue(float value)
|
||||||
{
|
{
|
||||||
this -> weight = weight;
|
|
||||||
this -> value = value;
|
this -> value = value;
|
||||||
this -> bias = bias;
|
}
|
||||||
std::clog << "\n" << "Set Sinaps: Weight = " << weight
|
|
||||||
<< " Value = " << value
|
|
||||||
<< " Bias = " << bias << "\n";
|
|
||||||
}
|
|
||||||
void Sinaps::SetWeight(float weight) { std::clog << "Set Sinaps Weight: " << weight << "\n"; this -> weight = weight; }
|
|
||||||
void Sinaps::SetValue(float value) { std::clog << "Set Sinaps Value: " << value << "\n"; this -> value = value; }
|
|
||||||
void Sinaps::SetBias(float bias) { std::clog << "Set Sinaps Bias: " << bias << "\n"; this -> bias = bias; }
|
|
||||||
|
|
||||||
float Sinaps::Fire()
|
void Synapse::SetWeight(float weight)
|
||||||
{
|
{
|
||||||
float result = weight * value + bias;
|
this -> weight = weight;
|
||||||
std::clog << "Return Sinaps Fire: " << weight << " * " <<
|
}
|
||||||
value << " + " <<
|
|
||||||
bias << " = " <<
|
void Synapse::SetBias(float bias)
|
||||||
result << "\n";
|
{
|
||||||
|
this -> bias = bias;
|
||||||
|
}
|
||||||
|
|
||||||
|
float Synapse::Fire()
|
||||||
|
{
|
||||||
|
float result = 0.0;
|
||||||
|
|
||||||
|
result = this -> value * this -> weight + this -> bias;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
#pragma region Noron
|
#pragma region Neuron
|
||||||
class Noron
|
class Neuron
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Sinaps *forwards;
|
Synapse *incomings;
|
||||||
Sinaps *incoming;
|
Synapse *forwards;
|
||||||
int forwardsCount;
|
int incomingsSize;
|
||||||
int incomingCount;
|
int forwardsSize;
|
||||||
|
int layerSize;
|
||||||
|
float value;
|
||||||
public:
|
public:
|
||||||
Noron();
|
Neuron();
|
||||||
~Noron();
|
void ConnectIncomings(Synapse *, int);
|
||||||
bool SetForwards(Sinaps *, int);
|
void ConnectForwards(Synapse *, int, int);
|
||||||
bool SetIncoming(Sinaps *, int);
|
void SetValue(float);
|
||||||
float GetStatus();
|
float GetValue();
|
||||||
};
|
};
|
||||||
|
|
||||||
Noron::Noron()
|
Neuron::Neuron()
|
||||||
{
|
{
|
||||||
forwards = incoming = NULL;
|
incomings = forwards = NULL;
|
||||||
forwardsCount = incomingCount = 0;
|
incomingsSize = forwardsSize = layerSize = 0;
|
||||||
std::clog << "Create Noron: NULL" << "\n";
|
value = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Noron::~Noron()
|
void Neuron::SetValue(float value)
|
||||||
{
|
{
|
||||||
delete forwards;
|
this -> value = value;
|
||||||
delete incoming;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Noron::SetForwards(Sinaps *newForwards, int size)
|
void Neuron::ConnectIncomings(Synapse *incomings, int incomingsSize)
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "SetForwards: Allocating Memory of Size " << size << " Sinapses" << "\n";
|
this -> incomings = incomings;
|
||||||
if(forwards)
|
this -> incomingsSize = incomingsSize;
|
||||||
{
|
}
|
||||||
delete forwards;
|
|
||||||
forwards = NULL;
|
void Neuron::ConnectForwards(Synapse *forwards, int forwardsSize, int layerSize)
|
||||||
std::clog << "SetForwards: Old Forwards Has Been Deleted!" << "\n";
|
{
|
||||||
}
|
this -> forwards = forwards;
|
||||||
forwards = (Sinaps *)new char[sizeof(Sinaps) * size];
|
this -> forwardsSize = forwardsSize;
|
||||||
|
this -> layerSize = layerSize;
|
||||||
if(!forwards)
|
|
||||||
{
|
|
||||||
std::clog << "SetForwards: Memory Couldn't Allocated!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "SetForwards: Memory Allocated!" << "\n";
|
|
||||||
|
|
||||||
for (int i = 0; i < size; i++)
|
|
||||||
*(forwards+i) = *(newForwards+i);
|
|
||||||
|
|
||||||
forwardsCount = size;
|
|
||||||
std::clog << "SetForwards: Successfull!" << "\n";
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Noron::SetIncoming(Sinaps *newIncoming, int size)
|
float Neuron::GetValue()
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "SetIncoming: Allocating Memory of Size " << size << " Sinapses" << "\n";
|
float result = 0.0;
|
||||||
|
|
||||||
if(incoming)
|
|
||||||
{
|
|
||||||
delete incoming;
|
|
||||||
incoming = NULL;
|
|
||||||
std::clog << "SetIncoming: Old Incoming Has Been Deleted!" << "\n";
|
|
||||||
}
|
|
||||||
incoming = (Sinaps *)new char[sizeof(Sinaps) * size];
|
|
||||||
|
|
||||||
if(!incoming)
|
|
||||||
{
|
|
||||||
std::clog << "SetIncoming: Memory Couldn't Allocated!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "SetIncoming: Memory Allocated!" << "\n";
|
|
||||||
|
|
||||||
for (int i = 0; i < size; i++)
|
if(!incomings) return (value = result);
|
||||||
*(incoming+i) = *(newIncoming+i);
|
|
||||||
|
|
||||||
incomingCount = size;
|
|
||||||
std::clog << "SetIncoming: Successfull!" << "\n";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
float Noron::GetStatus()
|
for (int i = 0; i < incomingsSize; i++)
|
||||||
{
|
result += (incomings + i) -> Fire();
|
||||||
std::clog << "\n" << "GetStatus: Called!" << "\n";
|
|
||||||
float toplam = 0.0;
|
|
||||||
|
|
||||||
std::clog << "GetStatus: Firing All Sinapses!" << "\n" << "\n";
|
|
||||||
for (int i = 0; i < incomingCount; i++)
|
|
||||||
{
|
|
||||||
std::clog << "GetStatus: Firing Sinaps " << i << "\n";
|
|
||||||
toplam += (incoming + i) -> Fire();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < forwardsCount; i++)
|
|
||||||
{
|
|
||||||
std::clog << "GetStatus: Setting Value of Sinaps " << i << " to " << toplam << "\n";
|
|
||||||
(forwards + i) -> SetValue(toplam);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::clog << "Get Noron Status: Sum = " << toplam << "\n";
|
if(!forwards) return (value = result);
|
||||||
return toplam;
|
|
||||||
|
for (int i = 0; i < forwardsSize; i++)
|
||||||
|
// currentSynapse = (forwards -> synapses + (forwardNeuron * this -> neuronSize));
|
||||||
|
// (forwards + i) -> SetValue(result);
|
||||||
|
//BAK BURAYA
|
||||||
|
(forwards + i * layerSize) -> SetValue(result);
|
||||||
|
|
||||||
|
value = result;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
#pragma region Katman
|
#pragma region Layer
|
||||||
class Katman
|
class Layer
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
Noron *neurons;
|
Neuron *neurons;
|
||||||
Katman *forward;
|
Synapse *synapses;
|
||||||
Sinaps *layerSinapses;
|
int neuronSize;
|
||||||
int size;
|
int synapseSize;
|
||||||
Sinaps *CreateSinapsSet(int size);
|
Neuron *_CreateNeurons(int);
|
||||||
public:
|
public:
|
||||||
Katman();
|
Layer();
|
||||||
Katman(int);
|
Layer(int);
|
||||||
~Katman();
|
~Layer();
|
||||||
void FireLayer();
|
void FireLayer();
|
||||||
void RandomizeSinapsValues();
|
bool CreateNeurons(int);
|
||||||
bool SetForward(Katman *);
|
bool ConnectPrevious(Layer *);
|
||||||
bool SetIncoming(Sinaps *sinapsSet, int backwardsNeuronCount);
|
bool ConnectForwards(Layer *);
|
||||||
bool SetNoron(Noron *, int);
|
|
||||||
bool CreateNoron(int);
|
|
||||||
int GetSize();
|
int GetSize();
|
||||||
};
|
};
|
||||||
|
|
||||||
Katman::Katman()
|
Layer::Layer()
|
||||||
{
|
{
|
||||||
neurons = NULL;
|
neuronSize = synapseSize = 0;
|
||||||
forward = NULL;
|
neurons = NULL;
|
||||||
layerSinapses = NULL;
|
synapses = NULL;
|
||||||
this -> size = 0;
|
|
||||||
std::clog << "Create Layer: NULL" << "\n";
|
|
||||||
}
|
}
|
||||||
Katman::Katman(int size)
|
|
||||||
{
|
|
||||||
neurons = NULL;
|
|
||||||
forward = NULL;
|
|
||||||
layerSinapses = NULL;
|
|
||||||
this -> size = 0;
|
|
||||||
|
|
||||||
if(!CreateNoron(size))
|
|
||||||
{
|
|
||||||
std::clog << "Error Create Layer: Neurons Couldn't Created!" << "\n";
|
|
||||||
std::cout << "Katman Oluşturulamadı!" << "\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::clog << "Create Layer: " << size << " Neurons Has Been Created!" << "\n";
|
|
||||||
this -> size = size;
|
|
||||||
}
|
|
||||||
Katman::~Katman() { delete neurons; }
|
|
||||||
|
|
||||||
|
|
||||||
Sinaps *Katman::CreateSinapsSet(int size)
|
Layer::Layer(int size)
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "CreateSinapsSet: Allocating Memory Size of " << size << " Sinapses" << "\n";
|
neuronSize = synapseSize = 0;
|
||||||
Sinaps* sinapses = new Sinaps[size];
|
synapses = NULL;
|
||||||
|
neurons = _CreateNeurons(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
Layer::~Layer()
|
||||||
|
{
|
||||||
|
if(neurons) delete neurons;
|
||||||
|
if(synapses) delete synapses;
|
||||||
|
}
|
||||||
|
|
||||||
if(sinapses)
|
Neuron *Layer::_CreateNeurons(int size)
|
||||||
std::clog << "Create Sinaps Set: Memory Size of " << size << " Sinapses Allocated!" << "\n";
|
{
|
||||||
else
|
Neuron *newNeurons = NULL;
|
||||||
std::clog << "Error Create Sinaps Set!" << "\n";
|
newNeurons = (Neuron *) new char[sizeof(Neuron) * size];
|
||||||
|
|
||||||
return sinapses;
|
if(newNeurons)
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
*(newNeurons + i) = Neuron();
|
||||||
|
|
||||||
|
return newNeurons;
|
||||||
}
|
}
|
||||||
void Katman::RandomizeSinapsValues()
|
|
||||||
|
void Layer::FireLayer()
|
||||||
{
|
{
|
||||||
if(!forward)
|
for (int i = 0; i < neuronSize; i++)
|
||||||
return;
|
(neurons + i) -> GetValue();
|
||||||
|
|
||||||
float weight = 0;
|
|
||||||
float value = 0;
|
|
||||||
float bias = 0;
|
|
||||||
|
|
||||||
int sinapsCount = size * (forward -> GetSize());
|
|
||||||
for (int i = 0; i < sinapsCount; i++)
|
|
||||||
{
|
|
||||||
weight = RandomFloat(-1, 1);
|
|
||||||
value = RandomFloat(-1, 1);
|
|
||||||
bias = RandomFloat(-1, 1);
|
|
||||||
|
|
||||||
(layerSinapses + i) -> SetSinaps(weight, value, bias);
|
|
||||||
std::clog << "RandomizeSinapsValues: SetSinaps Called With Values of SetSinaps(" << weight << ", " << value << ", " << bias << ")" << "\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Katman::FireLayer()
|
bool Layer::CreateNeurons(int size)
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "FireLayer: Number of " << size << " Neurons' GetStatus is Being Called!" << "\n";
|
if(neurons = _CreateNeurons(size))
|
||||||
for (int i = 0; i < size; i++)
|
neuronSize = size;
|
||||||
std::clog << i << ". Neuron Status: " << (neurons + i) -> GetStatus() << "\n";
|
return neurons;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Katman::SetForward(Katman *forward)
|
bool Layer::ConnectPrevious(Layer *previous)
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "SetForward: Called!" << "\n";
|
int previousSize = previous -> GetSize();
|
||||||
Sinaps *sinapses = NULL; // Pointer to store all created sinapses
|
int synapseCount = (this -> neuronSize) * previousSize;
|
||||||
Sinaps *newSinapses = NULL; // Temporary Pointer for creating each neurons' s1inapses
|
int currentIndex = 0;
|
||||||
int forwardSize = 0;
|
Synapse *currentSynapse = NULL;
|
||||||
if(layerSinapses)
|
Neuron *currentNeuron = NULL;
|
||||||
{
|
// Synapse *connectSynapses = NULL;
|
||||||
delete layerSinapses;
|
|
||||||
layerSinapses = NULL;
|
|
||||||
std::clog << "SetForward: Old layerSinapses Has Been Deleted!" << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
this -> forward = forward;
|
if(synapses) delete synapses;
|
||||||
|
synapses = (Synapse *) new char[sizeof(Synapse) * synapseCount];
|
||||||
if(!forward)
|
if(!synapses) return false;
|
||||||
{
|
|
||||||
std::clog << "SetForward: Forward is NULL" << "\n";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
forwardSize = forward -> GetSize();
|
// connectSynapses = (Synapse *) new char[sizeof(Synapse) * previousSize];
|
||||||
std::clog << "SetForward: Creating Sinaps Set with Number of " << (size * forwardSize) << "\n";
|
|
||||||
if(sinapses)
|
|
||||||
{
|
|
||||||
delete sinapses;
|
|
||||||
sinapses = NULL;
|
|
||||||
std::clog << "SetForward: Old Sinapses Has Been Deleted!" << "\n";
|
|
||||||
}
|
|
||||||
sinapses = (Sinaps *)new char[sizeof(Sinaps) * size * forwardSize];
|
|
||||||
|
|
||||||
if(!sinapses)
|
for (int thisNeuron = 0; thisNeuron < this -> neuronSize; thisNeuron++)
|
||||||
{
|
{
|
||||||
std::clog << "Error SetForward: Couldn't Allocate Memory for Sinapses!" << "\n";
|
for (int prevNeuron = 0; prevNeuron < previousSize; prevNeuron++)
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "SetForward: SinapsSet Created!" << "\n";
|
|
||||||
|
|
||||||
// Set Forwards of each neuron in the Layer
|
|
||||||
for (int thisCounter = 0; thisCounter < size; thisCounter++)
|
|
||||||
{
|
|
||||||
newSinapses = CreateSinapsSet(forwardSize);
|
|
||||||
|
|
||||||
if(!newSinapses)
|
|
||||||
{
|
{
|
||||||
std::clog << "SetForward -> CreateSinapsSet: Couldn't Allocate Memory for Sinapses!" << "\n";
|
currentIndex = thisNeuron * previousSize + prevNeuron;
|
||||||
return false;
|
currentSynapse = (synapses + currentIndex);
|
||||||
|
currentNeuron = (previous -> neurons) + prevNeuron;
|
||||||
|
|
||||||
|
*currentSynapse = Synapse();
|
||||||
|
// currentSynapse = (Synapse *) new char[sizeof(Synapse)];
|
||||||
|
// currentSynapse -> SetWeight(1);
|
||||||
|
// currentSynapse -> SetValue(2);
|
||||||
|
// currentSynapse -> SetBias(3);
|
||||||
|
// currentSynapse -> SetRoot(currentNeuron);
|
||||||
}
|
}
|
||||||
std::clog << "SetForward -> CreateSinapsSet: SinapsSet Created!" << "\n";
|
|
||||||
|
|
||||||
std::clog << "SetForward -> SetForwards: " <<
|
currentNeuron = (neurons + thisNeuron);
|
||||||
(neurons + thisCounter) -> SetForwards(newSinapses, forwardSize)
|
currentNeuron -> ConnectIncomings((synapses + thisNeuron * previousSize), previousSize);
|
||||||
<< "\n";;
|
|
||||||
|
|
||||||
|
|
||||||
// Add each sinaps to the array
|
|
||||||
for (int forwardCounter = 0; forwardCounter < forwardSize; forwardCounter++)
|
|
||||||
*(sinapses + forwardCounter) = *(newSinapses + forwardCounter);
|
|
||||||
std::clog << "SetForward: Sinapses Are Added to the Array!" << "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
layerSinapses = sinapses;
|
return previous -> ConnectForwards(this);
|
||||||
std::clog << "SetForward: Sinaps Array Has Been Set to the Class' Pointer!" << "\n";
|
|
||||||
// Send the sinapses to the forward layer
|
|
||||||
return forward -> SetIncoming(sinapses, size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Katman::SetIncoming(Sinaps *sinapsSet, int backwardsNeuronCount)
|
bool Layer::ConnectForwards(Layer *forwards)
|
||||||
{
|
{
|
||||||
Sinaps *sinapses = NULL;
|
int forwardsSize = forwards -> neuronSize;
|
||||||
int sinapcCounter = 0;
|
Neuron *currentNeuron = NULL;
|
||||||
|
|
||||||
std::clog << "\n" << "SetIncoming: Creating Sinaps Set with Number of " << backwardsNeuronCount << "\n";
|
|
||||||
if(sinapses)
|
|
||||||
{
|
|
||||||
delete sinapses;
|
|
||||||
sinapses = NULL;
|
|
||||||
std::clog << "SetIncoming: Old Sinapses Has Been Deleted!" << "\n";
|
|
||||||
}
|
|
||||||
sinapses = (Sinaps *)new char[sizeof(Sinaps) * backwardsNeuronCount];
|
|
||||||
|
|
||||||
if(!sinapses)
|
for (int thisNeuron = 0; thisNeuron < this -> neuronSize; thisNeuron++)
|
||||||
{
|
{
|
||||||
std::clog << "Error SetIncoming: Couldn't Allocate Memory for Sinapses!" << "\n";
|
currentNeuron = (neurons + thisNeuron);
|
||||||
return false;
|
for (int forwardNeuron = 0; forwardNeuron < forwardsSize; forwardNeuron++)
|
||||||
|
currentNeuron -> ConnectForwards(forwards -> synapses + thisNeuron, forwardsSize, this -> neuronSize);
|
||||||
|
// currentSynapse = (forwards -> synapses + (thisNeuron + forwardNeuron * this -> neuronSize));
|
||||||
}
|
}
|
||||||
std::clog << "SetIncoming: SinapsSet Created!" << "\n";
|
|
||||||
|
|
||||||
for (int thisCounter = 0; thisCounter < size; thisCounter++)
|
|
||||||
{
|
|
||||||
std::clog << "SetIncoming: Sinapses Are Being Added to the Array!" << "\n";
|
|
||||||
// Add each sinaps to the array
|
|
||||||
for (int incomingCounter = 0; incomingCounter < backwardsNeuronCount; incomingCounter++)
|
|
||||||
*(sinapses + incomingCounter) = *(sinapsSet + sinapcCounter++);
|
|
||||||
|
|
||||||
std::clog << "SetIncoming -> Neuron SetIncoming: " <<
|
|
||||||
(neurons + thisCounter) -> SetIncoming(sinapses, backwardsNeuronCount)
|
|
||||||
<< "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Katman::SetNoron(Noron *newNeurons, int size)
|
int Layer::GetSize()
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "SetNoron: Creating Neurons with Number of " << size << "\n";
|
return neuronSize;
|
||||||
if(neurons)
|
|
||||||
{
|
|
||||||
delete neurons;
|
|
||||||
neurons = NULL;
|
|
||||||
std::clog << "SetNoron: Old Neurons Has Been Deleted!" << "\n";
|
|
||||||
}
|
|
||||||
neurons = (Noron *) new char[sizeof(Noron) * size];
|
|
||||||
|
|
||||||
if(!neurons)
|
|
||||||
{
|
|
||||||
std::clog << "Error SetNoron: Creating Neurons Failed!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "SetNoron: Neurons Created Successfully!" << "\n";
|
|
||||||
|
|
||||||
std::clog << "SetNoron: Setting Neurons to the Class' Neurons!" << "\n";
|
|
||||||
for (int i = 0; i < size; i++)
|
|
||||||
*(neurons+i) = *(newNeurons+i);
|
|
||||||
|
|
||||||
std::clog << "SetNoron: Neurons are Set Successfully!" << "\n";
|
|
||||||
this -> size = size;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
#pragma region Input-Output
|
||||||
bool Katman::CreateNoron(int size)
|
class Input : public Layer
|
||||||
{
|
|
||||||
std::clog << "\n" << "CreateNoron: Creating Neurons with Number of " << size << "\n";
|
|
||||||
if(neurons)
|
|
||||||
{
|
|
||||||
delete neurons;
|
|
||||||
neurons = NULL;
|
|
||||||
std::clog << "CreateNoron: Old Neurons Has Been Deleted!" << "\n";
|
|
||||||
}
|
|
||||||
neurons = new Noron[size];
|
|
||||||
|
|
||||||
if(!neurons)
|
|
||||||
{
|
|
||||||
std::clog << "Error CreateNoron: Creating Neurons Failed!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "CreateNoron: Neurons Created Successfully!" << "\n";
|
|
||||||
|
|
||||||
std::clog << "CreateNoron: Neurons are Set Successfully!" << "\n";
|
|
||||||
this -> size = size;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Katman::GetSize() { return size; }
|
|
||||||
#pragma endregion
|
|
||||||
#pragma region Girdi-Cikti
|
|
||||||
#pragma region Girdi
|
|
||||||
class Girdi : public Katman
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Girdi();
|
Input();
|
||||||
Girdi(int);
|
|
||||||
void SetValue(int, float);
|
void SetValue(int, float);
|
||||||
};
|
};
|
||||||
|
|
||||||
Girdi::Girdi() : Katman(1) { std::clog << "\n" << "Create Input Layer: Called!" << "\n"; }
|
Input::Input() : Layer() {}
|
||||||
Girdi::Girdi(int size) : Katman(size) {}
|
void Input::SetValue(int index, float value)
|
||||||
|
|
||||||
void Girdi::SetValue(int index, float value)
|
|
||||||
{
|
{
|
||||||
Sinaps *editedSinaps = NULL;
|
if(index >= this -> neuronSize || index < 0)
|
||||||
int forwardNeuronCount = forward -> GetSize();
|
return;
|
||||||
std::clog << "\n" << "SetValue: Index of " << index << " Neuron's Sinapses Values are Getting Set to Value of " << value << "\n";
|
|
||||||
|
|
||||||
for (int i = 0; i < forwardNeuronCount; i++)
|
(neurons + index) -> SetValue(value);
|
||||||
{
|
|
||||||
std::clog << "SetValue -> Sinaps SetValue: Index of " << index << " Neuron's " << i << " Sinaps Value is Getting Set to Value of " << value << "\n";
|
|
||||||
editedSinaps = (layerSinapses + index * forwardNeuronCount + i);
|
|
||||||
editedSinaps -> SetValue(value);
|
|
||||||
std::clog << "SetValue -> Sinaps SetValue: Successfull" << "\n";
|
|
||||||
}
|
|
||||||
std::clog << "SetValue: Successfull" << "\n";
|
|
||||||
}
|
}
|
||||||
#pragma endregion
|
|
||||||
#pragma region Cikti
|
class Output : public Layer
|
||||||
class Cikti : public Katman
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Cikti();
|
Output();
|
||||||
Cikti(int);
|
|
||||||
float GetValue(int);
|
float GetValue(int);
|
||||||
};
|
};
|
||||||
|
|
||||||
Cikti::Cikti() : Katman(1) { std::clog << "\n" << "Create Output Layer: Called!" << "\n"; }
|
Output::Output() : Layer() {}
|
||||||
Cikti::Cikti(int size) : Katman(size) {}
|
float Output::GetValue(int index)
|
||||||
|
|
||||||
float Cikti::GetValue(int index)
|
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "Call GetValue!" << "\n";
|
float result = 0.0;
|
||||||
float result = (neurons + index) -> GetStatus();
|
|
||||||
std::clog << "GetValue: " << result << "\n";
|
if(index >= this -> neuronSize || index < 0)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
result = (neurons + index) -> GetValue();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
@ -486,244 +291,110 @@ float RandomFloat(int min, int max)
|
||||||
class NeuralNetwork
|
class NeuralNetwork
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Girdi *input;
|
Input *input;
|
||||||
Katman *hiddenLayers;
|
Layer *hidden;
|
||||||
Cikti *output;
|
Output *output;
|
||||||
int hiddenSize;
|
int hiddenSize;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
NeuralNetwork();
|
NeuralNetwork();
|
||||||
NeuralNetwork(int);
|
NeuralNetwork(int);
|
||||||
~NeuralNetwork();
|
~NeuralNetwork();
|
||||||
void SetInput(int, float);
|
|
||||||
void RandomizeNetworkValues();
|
|
||||||
void FireNetwork();
|
void FireNetwork();
|
||||||
bool SetInputNeurons(int);
|
bool SetInputNeurons(int);
|
||||||
bool SetHiddenLayerNeurons(int, int);
|
bool SetHiddenNeurons(int, int);
|
||||||
bool SetOutputNeurons(int);
|
bool SetOutputNeurons(int);
|
||||||
bool ConnectLayers();
|
bool ConnectLayers();
|
||||||
float GetOutputValue(int);
|
float GetOutput(int);
|
||||||
|
void SetInput(int, float);
|
||||||
};
|
};
|
||||||
|
|
||||||
NeuralNetwork::NeuralNetwork()
|
NeuralNetwork::NeuralNetwork()
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "Create NeuralNetwork: NULL" << "\n";
|
|
||||||
hiddenSize = 0;
|
hiddenSize = 0;
|
||||||
input = NULL;
|
input = NULL;
|
||||||
hiddenLayers = NULL;
|
hidden = NULL;
|
||||||
output = NULL;
|
output = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
NeuralNetwork::NeuralNetwork(int hiddenSize)
|
NeuralNetwork::NeuralNetwork(int hiddenSize)
|
||||||
{
|
{
|
||||||
std::clog << "\n" << "Create NeuralNetwork: Called" << "\n";
|
|
||||||
input = new Girdi();
|
|
||||||
hiddenLayers = new Katman[hiddenSize];
|
|
||||||
output = new Cikti();
|
|
||||||
std::clog << "\n" << "Create NeuralNetwork: New Neural Network Created with " << hiddenSize << " Layers!" << "\n";
|
|
||||||
|
|
||||||
if(!input)
|
|
||||||
{
|
|
||||||
std::clog << "Error Create NeuralNetwork: Memory Couldn't Allocated for Input Layer!" << "\n";
|
|
||||||
std::cout << "Girdi Katmani Olusturulamadi!" << "\n";
|
|
||||||
}
|
|
||||||
if(!hiddenLayers)
|
|
||||||
{
|
|
||||||
std::clog << "Error Create NeuralNetwork: Memory Couldn't Allocated for Hidden Layers!" << "\n";
|
|
||||||
std::cout << "Ara Katmanlar Olusturulamadi!" << "\n";
|
|
||||||
}
|
|
||||||
if(!output)
|
|
||||||
{
|
|
||||||
std::clog << "Error Create NeuralNetwork: Memory Couldn't Allocated for Output Layer!" << "\n";
|
|
||||||
std::cout << "Cikti Katmani Olusturulamadi!" << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!input || !hiddenLayers || !output)
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::clog << "Create NeuralNetwork: Succesfull!" << "\n";
|
|
||||||
this -> hiddenSize = hiddenSize;
|
this -> hiddenSize = hiddenSize;
|
||||||
|
input = new Input();
|
||||||
|
hidden = new Layer(hiddenSize);
|
||||||
|
output = new Output();
|
||||||
}
|
}
|
||||||
|
|
||||||
NeuralNetwork::~NeuralNetwork()
|
NeuralNetwork::~NeuralNetwork()
|
||||||
{
|
{
|
||||||
delete input;
|
if(input) delete input;
|
||||||
delete hiddenLayers;
|
if(hidden) delete hidden;
|
||||||
delete output;
|
if(output) delete output;
|
||||||
}
|
|
||||||
|
|
||||||
bool NeuralNetwork::SetHiddenLayerNeurons(int index, int size)
|
|
||||||
{
|
|
||||||
bool result;
|
|
||||||
std::clog << "\n" << "SetHiddenLayerNeurons: Size of " << size << " at Index of " << index << "\n";
|
|
||||||
std::clog << "SetHiddenLayerNeurons: Allocating Memory Size of " << size << " Neurons!" << "\n";
|
|
||||||
Noron *neurons = (Noron *) new char[sizeof(Noron) * size];
|
|
||||||
|
|
||||||
if(!neurons)
|
|
||||||
{
|
|
||||||
std::clog << "Error SetHiddenLayerNeurons: Couldn't Allocate Memory for Neurons!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "SetHiddenLayerNeurons: Memory Allocated!" << "\n";
|
|
||||||
std::clog << "SetHiddenLayerNeurons -> CreateNoron: Called!" << "\n";
|
|
||||||
result = (hiddenLayers + index) -> CreateNoron(size);
|
|
||||||
std::clog << "SetHiddenLayerNeurons -> CreateNoron: " << result << "\n";
|
|
||||||
// if(result)
|
|
||||||
// return (hiddenLayers + index) -> SetNoron(neurons, size);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NeuralNetwork::SetInputNeurons(int size)
|
|
||||||
{
|
|
||||||
bool result;
|
|
||||||
std::clog << "\n" << "SetInputNeurons: Size of " << size << "\n";
|
|
||||||
std::clog << "SetInputNeurons: Allocating Memory Size of " << size << " Neurons!" << "\n";
|
|
||||||
Noron *neurons = (Noron *) new char[sizeof(Noron) * size];
|
|
||||||
|
|
||||||
if(!neurons)
|
|
||||||
{
|
|
||||||
std::clog << "Error SetInputNeurons: Couldn't Allocate Memory for Neurons!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "SetInputNeurons: Memory Allocated!" << "\n";
|
|
||||||
std::clog << "SetInputNeurons -> CreateNoron: Called!" << "\n";
|
|
||||||
result = input -> CreateNoron(size);
|
|
||||||
std::clog << "SetInputNeurons -> CreateNoron: " << result << "\n";
|
|
||||||
// return output -> SetNoron(neurons, size);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NeuralNetwork::SetOutputNeurons(int size)
|
|
||||||
{
|
|
||||||
bool result;
|
|
||||||
std::clog << "\n" << "SetOutputNeurons: Size of " << size << "\n";
|
|
||||||
std::clog << "SetOutputNeurons: Allocating Memory Size of " << size << " Neurons!" << "\n";
|
|
||||||
Noron *neurons = (Noron *) new char[sizeof(Noron) * size];
|
|
||||||
|
|
||||||
if(!neurons)
|
|
||||||
{
|
|
||||||
std::clog << "Error SetOutputNeurons: Couldn't Allocate Memory for Neurons!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "SetOutputNeurons: Memory Allocated!" << "\n";
|
|
||||||
|
|
||||||
std::clog << "SetOutputNeurons -> CreateNoron: Called!" << "\n";
|
|
||||||
result = input -> CreateNoron(size);
|
|
||||||
std::clog << "SetOutputNeurons -> CreateNoron: " << result << "\n";
|
|
||||||
// return input -> SetNoron(neurons, size);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NeuralNetwork::ConnectLayers()
|
|
||||||
{
|
|
||||||
if(!input -> SetForward(hiddenLayers))
|
|
||||||
{
|
|
||||||
std::clog << "\n" << "Error ConnectLayers: Input Couldn't Set to Forward!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "\n" << "ConnectLayers: Input is Set to Forward Successfully!" << "\n";
|
|
||||||
|
|
||||||
for (int i = 0; i < hiddenSize - 1; i++)
|
|
||||||
if(!(hiddenLayers + i) -> SetForward(hiddenLayers + i + 1))
|
|
||||||
{
|
|
||||||
std::clog << "Error ConnectLayers: Hidden Layer " << i << " Couldn't Set to Forward!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
std::clog << "ConnectLayers: Hidden Layer " << i << " is Set to Forward Successfully!" << "\n";
|
|
||||||
|
|
||||||
std::clog << "ConnectLayers: Hidden Layers are Set to Forward Successfully!" << "\n";
|
|
||||||
|
|
||||||
if(!(hiddenLayers + hiddenSize - 1) -> SetForward(output))
|
|
||||||
{
|
|
||||||
std::clog << "Error ConnectLayers: Output Couldn't Set to Forward!" << "\n";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
std::clog << "ConnectLayers: Output is Set to Forward Successfully!" << "\n";
|
|
||||||
|
|
||||||
return output -> SetForward(NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NeuralNetwork::FireNetwork()
|
void NeuralNetwork::FireNetwork()
|
||||||
{
|
{
|
||||||
// input -> FireLayer();
|
|
||||||
// std::clog << "\n" << "FireNetwork: Input Fired!" << "\n";
|
|
||||||
|
|
||||||
for (int i = 0; i < hiddenSize; i++)
|
for (int i = 0; i < hiddenSize; i++)
|
||||||
{
|
(hidden + i) -> FireLayer();
|
||||||
(hiddenLayers + i) -> FireLayer();
|
|
||||||
std::clog << "FireNetwork: Hidden Layer " << i << " Fired!" << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
output -> FireLayer();
|
output -> FireLayer();
|
||||||
std::clog << "FireNetwork: Output Fired!" << "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool NeuralNetwork::SetInputNeurons(int size)
|
||||||
|
{
|
||||||
|
return input -> CreateNeurons(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NeuralNetwork::SetHiddenNeurons(int index, int size)
|
||||||
|
{
|
||||||
|
return (hidden + index) -> CreateNeurons(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NeuralNetwork::SetOutputNeurons(int size)
|
||||||
|
{
|
||||||
|
return output -> CreateNeurons(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NeuralNetwork::ConnectLayers()
|
||||||
|
{
|
||||||
|
if(!hidden -> ConnectPrevious(input))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (int i = 1; i < hiddenSize; i++)
|
||||||
|
if(!(hidden + i) -> ConnectPrevious((hidden + i - 1)))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(output -> ConnectPrevious((hidden + hiddenSize - 1)))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
float NeuralNetwork::GetOutput(int index)
|
||||||
|
{
|
||||||
|
return output -> GetValue(index);
|
||||||
|
}
|
||||||
|
|
||||||
void NeuralNetwork::SetInput(int index, float value)
|
void NeuralNetwork::SetInput(int index, float value)
|
||||||
{
|
{
|
||||||
if(!input)
|
|
||||||
{
|
|
||||||
std::clog << "\n" << "SetInput: There's no Input Layer Set!" << "\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::clog << "\n" << "SetInput -> Input SetValue: SetValue(" << index << ", " << value << ")!" << "\n";
|
|
||||||
input -> SetValue(index, value);
|
input -> SetValue(index, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NeuralNetwork::RandomizeNetworkValues()
|
|
||||||
{
|
|
||||||
std::clog << "\n" << "RandomizeNetworkValues: Input Sinapses Are Getting Randomized!" << "\n";
|
|
||||||
input -> RandomizeSinapsValues();
|
|
||||||
|
|
||||||
for (int i = 0; i < hiddenSize; i++)
|
|
||||||
{
|
|
||||||
std::clog << "RandomizeNetworkValues: Hidden Layer " << i << " Sinapses Are Getting Randomized!" << "\n";
|
|
||||||
(hiddenLayers + i) -> RandomizeSinapsValues();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::clog << "RandomizeNetworkValues: Output Sinapses Are Getting Randomized!" << "\n";
|
|
||||||
output -> RandomizeSinapsValues();
|
|
||||||
}
|
|
||||||
|
|
||||||
float NeuralNetwork::GetOutputValue(int index)
|
|
||||||
{
|
|
||||||
float result = output -> GetValue(index);
|
|
||||||
std::clog << "\n" << "GetOutputValue: " << result << "\n";
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char const *argv[])
|
int main(int argc, char const *argv[])
|
||||||
{
|
{
|
||||||
float result;
|
|
||||||
FILE *file;
|
|
||||||
NeuralNetwork network(3);
|
NeuralNetwork network(3);
|
||||||
|
|
||||||
network.SetInputNeurons(1);
|
network.SetInputNeurons(1);
|
||||||
network.SetHiddenLayerNeurons(0, 2);
|
network.SetHiddenNeurons(0, 2);
|
||||||
network.SetHiddenLayerNeurons(1, 3);
|
network.SetHiddenNeurons(1, 3);
|
||||||
network.SetHiddenLayerNeurons(2, 2);
|
network.SetHiddenNeurons(2, 2);
|
||||||
network.SetOutputNeurons(1);
|
network.SetOutputNeurons(1);
|
||||||
|
|
||||||
network.ConnectLayers();
|
network.ConnectLayers();
|
||||||
std::cout << "m1\n";
|
network.SetInput(0, 2);
|
||||||
network.RandomizeNetworkValues();
|
|
||||||
std::cout << "m2\n";
|
|
||||||
|
|
||||||
network.SetInput(0, 1);
|
|
||||||
std::cout << "m3\n";
|
|
||||||
|
|
||||||
network.FireNetwork();
|
network.FireNetwork();
|
||||||
result = network.GetOutputValue(0);
|
std::cout << "Result = " << network.GetOutput(0) << "\n";
|
||||||
std::cout << "Output is " << result << "\n";
|
|
||||||
file = fopen("result.txt", "w");
|
|
||||||
fprintf(file, "%f", result);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
// std::cout << "Sinaps = " << sizeof(Sinaps) * 30 << "\n";
|
|
||||||
// std::cout << "Noron = " << sizeof(Noron) * 9 << "\n";
|
|
||||||
// std::cout << "Katman = " << sizeof(Katman) * 3 << "\n";
|
|
||||||
// std::cout << "Girdi = " << sizeof(Girdi) * 1 << "\n";
|
|
||||||
// std::cout << "Cikti = " << sizeof(Cikti) * 1 << "\n";
|
|
||||||
// std::cout << "NeuralNetwork = " << sizeof(NeuralNetwork) * 1 << "\n";
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue