cleaning + Network getter and setter for input / output size

This commit is contained in:
2016-05-03 22:03:03 +02:00
parent 6a17694a6b
commit 58f7f8f69b
9 changed files with 85 additions and 58 deletions

View File

@@ -4,14 +4,7 @@
#include <SimpleJSON/Factory.h>
#define NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION(name,function) SIMPLEJSON_REGISTER(NeuralNetwork::ActivationFunction::Factory,name,function)
/*public: \
static const class __FACT_REGISTER_ {\
public: \
__FACT_REGISTER_() {\
NeuralNetwork::ActivationFunction::Factory::registerCreator( #name ,function);\
}\
} __FACT_REGISTER;
*/
#define NEURAL_NETWORK_REGISTER_ACTIVATION_FUNCTION_FINISH(name,function) SIMPLEJSON_REGISTER_FINISH(NeuralNetwork::ActivationFunction::Factory,name,function)
namespace NeuralNetwork {

View File

@@ -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)

View File

@@ -20,7 +20,7 @@ namespace FeedForward {
* @brief Constructor for Network
* @param _inputSize is number of inputs to network
*/
inline Network(size_t _inputSize):NeuralNetwork::Network(),layers() {
inline Network(size_t _inputSize):NeuralNetwork::Network(_inputSize,_inputSize),layers() {
appendLayer(_inputSize);
};
@@ -36,8 +36,13 @@ namespace FeedForward {
Layer& appendLayer(std::size_t size=1, const ActivationFunction::ActivationFunction &activationFunction=ActivationFunction::Sigmoid(-4.9)) {
layers.push_back(new Layer(size,activationFunction));
if(layers.size() > 1)
layers.back()->setInputSize(layers[layers.size()-2]->size());
if(layers.size() > 1) {
layers.back()->setInputSize(layers[layers.size() - 2]->size());
} else {
_inputs=size;
}
_outputs=size;
return *layers[layers.size()-1];//.back();
}
@@ -77,7 +82,7 @@ namespace FeedForward {
std::vector<Layer*> layers;
private:
inline Network():NeuralNetwork::Network(),layers() {
inline Network():NeuralNetwork::Network(0,0),layers() {
};
SIMPLEJSON_REGISTER(NeuralNetwork::FeedForward::Network::Factory, NeuralNetwork::FeedForward::Network,NeuralNetwork::FeedForward::Network::deserialize)

View File

@@ -9,48 +9,61 @@
#define NEURAL_NETWORK_INIT() const static bool ______TMP= NeuralNetwork::Network::loaded()
namespace NeuralNetwork
{
namespace NeuralNetwork {
/**
* @author Tomas Cernik (Tom.Cernik@gmail.com)
* @brief Abstract model of simple Network
*/
class Network : public SimpleJSON::SerializableObject
{
class Network : public SimpleJSON::SerializableObject {
public:
/**
* @brief Constructor for Network
*/
inline Network() {
inline Network(std::size_t inputs, std::size_t outputs) : _inputs(inputs), _outputs(outputs) {
loaded();
};
Network(const Network &r) = default;
/**
* @brief Virtual destructor for Network
*/
virtual ~Network() {};
virtual ~Network() { };
/**
* @brief This is a virtual function for all networks
* @param input is input of network
* @returns output of network
*/
virtual std::vector<float> computeOutput(const std::vector<float>& input)=0;
virtual std::vector<float> computeOutput(const std::vector<float> &input) = 0;
std::size_t inputs() {
return _inputs;
}
std::size_t outputs() {
return _outputs;
}
/**
* @param t is number of threads, if set to 0 or 1 then threading is disabled
* @param threads is number of threads, if set to 0 or 1 then threading is disabled
* @brief Enables or disables Threaded computing of ANN
*/
inline virtual void setThreads(const unsigned& t) final {threads=t;}
inline virtual void setThreads(const unsigned &threads) final {
_threads = threads;
}
protected:
/**
* @brief Number of threads used by network
*/
unsigned threads=1;
unsigned _threads = 1;
std::size_t _inputs;
std::size_t _outputs;
public:
static bool loaded();
};

View File

@@ -31,6 +31,14 @@ namespace NeuralNetwork
*/
virtual ~NeuronInterface() {};
const std::vector<float> & getWeights() const {
return weights;
}
void setWeights(const std::vector<float> &weights_) {
weights=weights_;
}
/**
* @brief getter for neuron weight
* @param &neuron is neuron it's weight is returned

View File

@@ -24,14 +24,14 @@ namespace Recurrent {
* @param _outputSize is size of output from network
* @param hiddenUnits is number of hiddenUnits to be created
*/
inline Network(size_t _inputSize, size_t _outputSize,size_t hiddenUnits=0):NeuralNetwork::Network(),inputSize(_inputSize),outputSize(_outputSize), neurons(0),outputs(0) {
inline Network(size_t inputSize, size_t outputSize,size_t hiddenUnits=0):NeuralNetwork::Network(inputSize,outputSize), neurons(0),outputs(0) {
neurons.push_back(new NeuralNetwork::BiasNeuron());
for(size_t i=0;i<_inputSize;i++) {
for(size_t i=0;i<inputSize;i++) {
neurons.push_back(new NeuralNetwork::InputNeuron(neurons.size()));
}
for(size_t i=0;i<_outputSize;i++) {
for(size_t i=0;i<outputSize;i++) {
addNeuron();
}
@@ -40,7 +40,7 @@ namespace Recurrent {
}
};
Network(const Network &r) :inputSize(r.inputSize), outputSize(r.outputSize), neurons(0), outputs(r.outputs) {
Network(const Network &r) : NeuralNetwork::Network(r), neurons(0), outputs(r.outputs) {
neurons.push_back(new NeuralNetwork::BiasNeuron());
for(std::size_t i=1;i<r.neurons.size();i++) {
neurons.push_back(r.neurons[i]->clone());
@@ -109,8 +109,6 @@ namespace Recurrent {
typedef SimpleJSON::Factory<Network> Factory;
protected:
size_t inputSize=0;
size_t outputSize=0;
std::vector<NeuronInterface*> neurons;
std::vector<float> outputs;