This commit is contained in:
2016-05-07 10:59:54 +02:00
parent 58f7f8f69b
commit 47484fc45c
3 changed files with 280 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include "../Network.h"
#include <random>
namespace NeuralNetwork {
namespace Cascade {
@@ -10,7 +11,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,outputSize) {
Network(std::size_t inputSize, std::size_t outputSize, const ActivationFunction::ActivationFunction &activationFunction=ActivationFunction::Sigmoid(-4.9)) : NeuralNetwork::Network(inputSize,outputSize) {
_neurons.push_back(std::make_shared<BiasNeuron>());
for(std::size_t i = 0; i < inputSize; i++) {
@@ -18,7 +19,7 @@ namespace NeuralNetwork {
}
for(std::size_t i = 0; i < outputSize; i++) {
_neurons.push_back(std::make_shared<Neuron>(_neurons.size()));
_neurons.push_back(std::make_shared<Neuron>(_neurons.size(),activationFunction));
_neurons.back()->setInputSize(inputSize + 1); // +1 is bias
}
}
@@ -29,8 +30,8 @@ namespace NeuralNetwork {
compute[0] = 1.0;
for(std::size_t i = 1; i <= _inputs; i++) {
compute[i] = input[i - 1];
for(std::size_t i = 0; i < _inputs; i++) {
compute[i+1] = input[i];
}
// 0 is bias, 1-_inputSize is input
@@ -45,14 +46,22 @@ namespace NeuralNetwork {
return _neurons.size();
}
const std::vector<std::shared_ptr<NeuronInterface>>& getNeurons() {
return _neurons;
}
std::shared_ptr<NeuronInterface> getNeuron(std::size_t id) {
return _neurons[id];
}
std::vector<std::shared_ptr<NeuronInterface>> getOutputNeurons() {
return std::vector<std::shared_ptr<NeuronInterface>>(_neurons.end()-_outputs,_neurons.end());
}
std::shared_ptr<NeuronInterface> addNeuron() {
_neurons.push_back(std::make_shared<Neuron>());
auto neuron = _neurons.back();
neuron->setInputSize(_neurons.size() - _outputs);
neuron->setInputSize(_neurons.size() - _outputs-1);
// 0 is bias, 1-_inputSize is input
std::size_t maxIndexOfNeuron = _neurons.size() - 1;
// move output to right position
@@ -93,12 +102,13 @@ namespace NeuralNetwork {
return std::unique_ptr<Network>(net);
}
//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;
std::mt19937 _generator(rand());
std::uniform_real_distribution<> _distribution(-0.3,0.3);
for(auto& neuron :getOutputNeurons()) {
for(std::size_t weight = 0; weight < neuron->getWeights().size(); weight++) {
neuron->weight(weight) = _distribution(_generator);
}
}
}