cleaning + Network getter and setter for input / output size
This commit is contained in:
@@ -10,7 +10,7 @@ namespace NeuralNetwork {
|
||||
* @brief Constructor for Network
|
||||
* @param _inputSize is number of inputs to network
|
||||
*/
|
||||
Network(std::size_t inputSize, std::size_t outputSize) : NeuralNetwork::Network(), _inputSize(inputSize), _outputSize(outputSize) {
|
||||
Network(std::size_t inputSize, std::size_t outputSize) : NeuralNetwork::Network(inputSize,outputSize) {
|
||||
_neurons.push_back(std::make_shared<BiasNeuron>());
|
||||
|
||||
for(std::size_t i = 0; i < inputSize; i++) {
|
||||
@@ -29,16 +29,16 @@ namespace NeuralNetwork {
|
||||
|
||||
compute[0] = 1.0;
|
||||
|
||||
for(std::size_t i = 1; i <= _inputSize; i++) {
|
||||
for(std::size_t i = 1; i <= _inputs; i++) {
|
||||
compute[i] = input[i - 1];
|
||||
}
|
||||
|
||||
// 0 is bias, 1-_inputSize is input
|
||||
for(std::size_t i = _inputSize + 1; i < _neurons.size(); i++) {
|
||||
for(std::size_t i = _inputs + 1; i < _neurons.size(); i++) {
|
||||
compute[i] = (*_neurons[i].get())(compute);
|
||||
}
|
||||
|
||||
return std::vector<float>(compute.end() - _outputSize, compute.end());
|
||||
return std::vector<float>(compute.end() - _outputs, compute.end());
|
||||
}
|
||||
|
||||
std::size_t getNeuronSize() const {
|
||||
@@ -52,16 +52,16 @@ namespace NeuralNetwork {
|
||||
std::shared_ptr<NeuronInterface> addNeuron() {
|
||||
_neurons.push_back(std::make_shared<Neuron>());
|
||||
auto neuron = _neurons.back();
|
||||
neuron->setInputSize(_neurons.size() - _outputSize);
|
||||
neuron->setInputSize(_neurons.size() - _outputs);
|
||||
// 0 is bias, 1-_inputSize is input
|
||||
std::size_t maxIndexOfNeuron = _neurons.size() - 1;
|
||||
// move output to right position
|
||||
for(std::size_t i = 0; i < _outputSize; i++) {
|
||||
for(std::size_t i = 0; i < _outputs; i++) {
|
||||
std::swap(_neurons[maxIndexOfNeuron - i], _neurons[maxIndexOfNeuron - i - 1]);
|
||||
}
|
||||
|
||||
for(std::size_t i = 0; i < _outputSize; i++) {
|
||||
_neurons[maxIndexOfNeuron - i]->setInputSize(_neurons.size() - _outputSize);
|
||||
for(std::size_t i = 0; i < _outputs; i++) {
|
||||
_neurons[maxIndexOfNeuron - i]->setInputSize(_neurons.size() - _outputs);
|
||||
}
|
||||
return neuron;
|
||||
}
|
||||
@@ -74,8 +74,8 @@ namespace NeuralNetwork {
|
||||
|
||||
return {
|
||||
{"class", "NeuralNetwork::Recurrent::Network"},
|
||||
{"inputSize", _inputSize},
|
||||
{"outputSize", _outputSize},
|
||||
{"inputSize", _inputs},
|
||||
{"outputSize", _outputs},
|
||||
{"neurons", neuronsSerialized}
|
||||
};
|
||||
}
|
||||
@@ -86,7 +86,7 @@ namespace NeuralNetwork {
|
||||
Network *net = new Network(inputSize, outputSize);
|
||||
net->_neurons.clear();
|
||||
|
||||
for(const auto& neuronObj: obj["neurons"].as<SimpleJSON::Type::Array>()) {
|
||||
for(const auto &neuronObj: obj["neurons"].as<SimpleJSON::Type::Array>()) {
|
||||
net->_neurons.push_back(Neuron::Factory::deserialize(neuronObj.as<SimpleJSON::Type::Object>()));
|
||||
}
|
||||
|
||||
@@ -94,9 +94,16 @@ namespace NeuralNetwork {
|
||||
}
|
||||
|
||||
|
||||
//I I H H O O 6
|
||||
void randomizeWeights() {
|
||||
for(std::size_t neuron = _neurons.size() - _outputs; neuron < _neurons.size(); neuron++) {
|
||||
for(std::size_t weight = 0; weight < _neurons.size() - _outputs; weight++) {
|
||||
_neurons[neuron]->weight(weight) = 1.0 - static_cast<float>(rand() % 2001) / 1000.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::size_t _inputSize;
|
||||
std::size_t _outputSize;
|
||||
std::vector<std::shared_ptr<NeuronInterface>> _neurons = {};
|
||||
|
||||
SIMPLEJSON_REGISTER(NeuralNetwork::Cascade::Network::Factory, NeuralNetwork::Cascade::Network, deserialize)
|
||||
Reference in New Issue
Block a user