removed old links and moved FF -> LayerNetwork and add empty wrapper FeedForwardNetwork
This commit is contained in:
@@ -1 +0,0 @@
|
||||
./FeedForward.h
|
||||
@@ -1,142 +1,13 @@
|
||||
#ifndef _S_NN_FF_H_
|
||||
#define _S_NN_FF_H_
|
||||
|
||||
#include "../Problem"
|
||||
#include "../Solution"
|
||||
#include "Network"
|
||||
|
||||
#include "ActivationFunction/Sigmoid.h"
|
||||
#include "ActivationFunction/ActivationFunction.h"
|
||||
|
||||
#include "BasisFunction/BasisFunction.h"
|
||||
#include "BasisFunction/StreamingBasisFunction.h"
|
||||
#include "BasisFunction/FeedForward.h"
|
||||
|
||||
#include <vector>
|
||||
#include <initializer_list>
|
||||
#include <thread>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
#include <mmintrin.h>
|
||||
#include <xmmintrin.h>
|
||||
#include <emmintrin.h>
|
||||
#include <xmmintrin.h>
|
||||
#include <pmmintrin.h>
|
||||
|
||||
#include "../sse_mathfun.h"
|
||||
#include "LayerNetwork.h"
|
||||
|
||||
namespace NeuralNetwork
|
||||
{
|
||||
class FFNeuron : public Neuron
|
||||
class FeedForward : public LayerNetwork
|
||||
{
|
||||
public:
|
||||
inline FFNeuron(float *w, float &outputF, float &i,float lam,ActivationFunction::ActivationFunction &fun):function(fun),weights(w),out(outputF),inputs(i),lambda(lam) { }
|
||||
|
||||
FFNeuron() = delete;
|
||||
FFNeuron(const FFNeuron&) = delete;
|
||||
FFNeuron& operator=(const FFNeuron&) = delete;
|
||||
|
||||
inline virtual float getWeight(const int& i ) const override { return weights[i+1]; }
|
||||
inline virtual void setWeight(const int& i,const float &p) override { weights[i+1]=p; }
|
||||
|
||||
inline virtual float output() const override { return out; }
|
||||
inline virtual float input() const override { return inputs; }
|
||||
inline virtual float derivatedOutput() const override { return function.derivatedOutput(inputs,out); }
|
||||
|
||||
inline virtual float getBias() const override { return weights[0]; }
|
||||
inline virtual void setBias(const float & bias) override { weights[0]=bias; }
|
||||
protected:
|
||||
ActivationFunction::ActivationFunction &function;
|
||||
float *weights;
|
||||
float &out;
|
||||
float &inputs;
|
||||
float lambda;
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
class FFLayer: public Layer
|
||||
{
|
||||
public:
|
||||
inline FFLayer(size_t s, float **w,float *out,float *in,float lam,ActivationFunction::ActivationFunction &fun): function(fun), layerSize(s),weights(w),outputs(out),inputs(in),lambda(lam) {}
|
||||
~FFLayer();
|
||||
|
||||
FFLayer(const FFLayer &) = delete;
|
||||
FFLayer& operator=(const FFLayer &) = delete;
|
||||
|
||||
virtual FFNeuron& operator[](const size_t& neuron) override;
|
||||
inline virtual size_t size() const override {return layerSize-1;};
|
||||
protected:
|
||||
ActivationFunction::ActivationFunction &function;
|
||||
FFNeuron **neurons=nullptr;
|
||||
size_t layerSize;
|
||||
float **weights;
|
||||
float *outputs;
|
||||
float *inputs;
|
||||
float lambda;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief typedef for FeedForward network initializating function
|
||||
*/
|
||||
typedef std::function<float(const size_t&layer, const size_t &neuron, const size_t &weight)> FeedForwardInitializer;
|
||||
|
||||
/**
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Class representing FeedForward network
|
||||
* @see ACyclicNetwork
|
||||
*
|
||||
* @b Usage:
|
||||
* @code
|
||||
* Shin::NeuralNetwork::FeedForward net({1,5,2});
|
||||
* net.setThreads(2); // it alows network to use 2 threads if it needs to.
|
||||
* Shin::Solution sol = net.solve(Shin::Problem(0.1)) // and finaly, solve Problem
|
||||
* @endcode
|
||||
*/
|
||||
class FeedForward:public ACyclicNetwork
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for FeedForward
|
||||
* @param s is initiaizer for layers (it's sizes)
|
||||
* @param lam is parametr for TransferFunction
|
||||
* @param weightInit is weight initializer function
|
||||
*/
|
||||
FeedForward(std::initializer_list<size_t> s, double lam=NeuralNetwork::lambda,
|
||||
FeedForwardInitializer weightInit=
|
||||
[](const size_t&, const size_t &, const size_t &)->float{ return 1.0-((float)(rand()%2001))/1000.0;}
|
||||
);
|
||||
virtual ~FeedForward();
|
||||
|
||||
/**
|
||||
* @brief we don't want to allow network to be copied
|
||||
*/
|
||||
FeedForward(const FeedForward &f) = delete; //TODO
|
||||
/**
|
||||
* @brief we don't want to allow network to be assigned
|
||||
*/
|
||||
FeedForward operator=(const FeedForward &f)=delete;
|
||||
|
||||
/**
|
||||
* @brief computes output Solution from input Problem
|
||||
*/
|
||||
virtual Shin::Solution solve(const Shin::Problem& p) override;
|
||||
virtual size_t size() const override { return layers;};
|
||||
virtual FFLayer& operator[](const size_t& l) override;
|
||||
protected:
|
||||
void solvePart(float *newSolution, size_t begin, size_t end,size_t prevSize, float* sol,size_t layer);
|
||||
private:
|
||||
FFLayer **ffLayers=nullptr;
|
||||
float ***weights=nullptr;
|
||||
float **outputs=nullptr;
|
||||
float **inputs=nullptr;
|
||||
ActivationFunction::ActivationFunction **transfer=nullptr;
|
||||
BasisFunction::BasisFunction *basisFunction = new BasisFunction::FeedForward();
|
||||
size_t *layerSizes=nullptr;
|
||||
size_t layers;/**< Number of layers */
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "FeedForward"
|
||||
#include "LayerNetwork.h"
|
||||
|
||||
using namespace NeuralNetwork;
|
||||
|
||||
FFLayer::~FFLayer()
|
||||
LayerNetworkLayer::~LayerNetworkLayer()
|
||||
{
|
||||
if(neurons!=nullptr)
|
||||
{
|
||||
@@ -14,14 +14,14 @@ FFLayer::~FFLayer()
|
||||
}
|
||||
}
|
||||
|
||||
FFNeuron& FFLayer::operator[](const size_t& neuron)
|
||||
LayerNetworkNeuron& LayerNetworkLayer::operator[](const size_t& neuron)
|
||||
{
|
||||
if(neurons==nullptr)
|
||||
{
|
||||
neurons=new FFNeuron*[layerSize-1];
|
||||
neurons=new LayerNetworkNeuron*[layerSize-1];
|
||||
for(size_t i=1;i<layerSize;i++)
|
||||
{
|
||||
neurons[i-1]=new FFNeuron(weights[i],outputs[i],inputs[i],lambda,function);
|
||||
neurons[i-1]=new LayerNetworkNeuron(weights[i],outputs[i],inputs[i],lambda,function);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ FFNeuron& FFLayer::operator[](const size_t& neuron)
|
||||
|
||||
}
|
||||
|
||||
FeedForward::FeedForward(std::initializer_list<size_t> s, double lam, FeedForwardInitializer weightInit): ACyclicNetwork(lam),layers(s.size())
|
||||
LayerNetwork::LayerNetwork(std::initializer_list<size_t> s, double lam, LayerNetworkInitializer weightInit): ACyclicNetwork(lam),layers(s.size())
|
||||
{
|
||||
transfer = new ActivationFunction::ActivationFunction*[s.size()];
|
||||
weights= new float**[s.size()];
|
||||
@@ -68,7 +68,7 @@ FeedForward::FeedForward(std::initializer_list<size_t> s, double lam, FeedForwar
|
||||
}
|
||||
}
|
||||
|
||||
FeedForward::~FeedForward()
|
||||
LayerNetwork::~LayerNetwork()
|
||||
{
|
||||
if(weights != nullptr)
|
||||
{
|
||||
@@ -100,7 +100,7 @@ FeedForward::~FeedForward()
|
||||
delete basisFunction;
|
||||
}
|
||||
|
||||
void FeedForward::solvePart(float *newSolution, register size_t begin, size_t end,size_t prevSize, float *sol,size_t layer)
|
||||
void LayerNetwork::solvePart(float *newSolution, register size_t begin, size_t end,size_t prevSize, float *sol,size_t layer)
|
||||
{
|
||||
ActivationFunction::StreamingActivationFunction *function=dynamic_cast<ActivationFunction::StreamingActivationFunction*>(transfer[layer]);
|
||||
BasisFunction::StreamingBasisFunction *bFunc=dynamic_cast<BasisFunction::StreamingBasisFunction*>(basisFunction);
|
||||
@@ -136,7 +136,7 @@ void FeedForward::solvePart(float *newSolution, register size_t begin, size_t en
|
||||
}
|
||||
}
|
||||
|
||||
Shin::Solution FeedForward::solve(const Shin::Problem& p)
|
||||
Shin::Solution LayerNetwork::solve(const Shin::Problem& p)
|
||||
{
|
||||
register float* sol=outputs[0];
|
||||
|
||||
@@ -183,14 +183,14 @@ Shin::Solution FeedForward::solve(const Shin::Problem& p)
|
||||
return ret;
|
||||
}
|
||||
|
||||
FFLayer& FeedForward::operator[](const size_t& l)
|
||||
LayerNetworkLayer& LayerNetwork::operator[](const size_t& l)
|
||||
{
|
||||
if(ffLayers==nullptr)
|
||||
{
|
||||
ffLayers=new FFLayer*[layers];
|
||||
ffLayers=new LayerNetworkLayer*[layers];
|
||||
for(size_t i=0;i<layers;i++)
|
||||
{
|
||||
ffLayers[i]=new FFLayer(layerSizes[i],weights[i],outputs[i],inputs[i],lambda,*transfer[i]);
|
||||
ffLayers[i]=new LayerNetworkLayer(layerSizes[i],weights[i],outputs[i],inputs[i],lambda,*transfer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
140
src/NeuralNetwork/LayerNetwork.h
Normal file
140
src/NeuralNetwork/LayerNetwork.h
Normal file
@@ -0,0 +1,140 @@
|
||||
#ifndef _NN_LN_H_
|
||||
#define _NN_LN_H_
|
||||
|
||||
#include "Network.h"
|
||||
|
||||
#include "ActivationFunction/Sigmoid.h"
|
||||
#include "ActivationFunction/ActivationFunction.h"
|
||||
|
||||
#include "BasisFunction/BasisFunction.h"
|
||||
#include "BasisFunction/StreamingBasisFunction.h"
|
||||
#include "BasisFunction/FeedForward.h"
|
||||
|
||||
#include <vector>
|
||||
#include <initializer_list>
|
||||
#include <thread>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
#include <mmintrin.h>
|
||||
#include <xmmintrin.h>
|
||||
#include <emmintrin.h>
|
||||
#include <xmmintrin.h>
|
||||
#include <pmmintrin.h>
|
||||
|
||||
#include "../sse_mathfun.h"
|
||||
|
||||
namespace NeuralNetwork
|
||||
{
|
||||
class LayerNetworkNeuron : public Neuron
|
||||
{
|
||||
public:
|
||||
inline LayerNetworkNeuron(float *w, float &outputF, float &i,float lam,ActivationFunction::ActivationFunction &fun):function(fun),weights(w),out(outputF),inputs(i),lambda(lam) { }
|
||||
|
||||
LayerNetworkNeuron() = delete;
|
||||
LayerNetworkNeuron(const LayerNetworkNeuron&) = delete;
|
||||
LayerNetworkNeuron& operator=(const LayerNetworkNeuron&) = delete;
|
||||
|
||||
inline virtual float getWeight(const int& i ) const override { return weights[i+1]; }
|
||||
inline virtual void setWeight(const int& i,const float &p) override { weights[i+1]=p; }
|
||||
|
||||
inline virtual float output() const override { return out; }
|
||||
inline virtual float input() const override { return inputs; }
|
||||
inline virtual float derivatedOutput() const override { return function.derivatedOutput(inputs,out); }
|
||||
|
||||
inline virtual float getBias() const override { return weights[0]; }
|
||||
inline virtual void setBias(const float & bias) override { weights[0]=bias; }
|
||||
protected:
|
||||
ActivationFunction::ActivationFunction &function;
|
||||
float *weights;
|
||||
float &out;
|
||||
float &inputs;
|
||||
float lambda;
|
||||
private:
|
||||
};
|
||||
|
||||
class LayerNetworkLayer: public Layer
|
||||
{
|
||||
public:
|
||||
inline LayerNetworkLayer(size_t s, float **w,float *out,float *in,float lam,ActivationFunction::ActivationFunction &fun): function(fun), layerSize(s),weights(w),outputs(out),inputs(in),lambda(lam) {}
|
||||
~LayerNetworkLayer();
|
||||
|
||||
LayerNetworkLayer(const LayerNetworkLayer &) = delete;
|
||||
LayerNetworkLayer& operator=(const LayerNetworkLayer &) = delete;
|
||||
|
||||
virtual LayerNetworkNeuron& operator[](const size_t& neuron) override;
|
||||
inline virtual size_t size() const override {return layerSize-1;};
|
||||
protected:
|
||||
ActivationFunction::ActivationFunction &function;
|
||||
LayerNetworkNeuron **neurons=nullptr;
|
||||
size_t layerSize;
|
||||
float **weights;
|
||||
float *outputs;
|
||||
float *inputs;
|
||||
float lambda;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief typedef for LayerNetwork network initializating function
|
||||
*/
|
||||
typedef std::function<float(const size_t&layer, const size_t &neuron, const size_t &weight)> LayerNetworkInitializer;
|
||||
|
||||
/**
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Class representing LayerNetwork network
|
||||
* @see ACyclicNetwork
|
||||
*
|
||||
* @b Usage:
|
||||
* @code
|
||||
* Shin::NeuralNetwork::LayerNetwork net({1,5,2});
|
||||
* net.setThreads(2); // it alows network to use 2 threads if it needs to.
|
||||
* Shin::Solution sol = net.solve(Shin::Problem(0.1)) // and finaly, solve Problem
|
||||
* @endcode
|
||||
*/
|
||||
class LayerNetwork:public ACyclicNetwork
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for LayerNetwork
|
||||
* @param s is initiaizer for layers (it's sizes)
|
||||
* @param lam is parametr for TransferFunction
|
||||
* @param weightInit is weight initializer function
|
||||
*/
|
||||
LayerNetwork(std::initializer_list<size_t> s, double lam=NeuralNetwork::lambda,
|
||||
LayerNetworkInitializer weightInit=
|
||||
[](const size_t&, const size_t &, const size_t &)->float{ return 1.0-((float)(rand()%2001))/1000.0;}
|
||||
);
|
||||
virtual ~LayerNetwork();
|
||||
|
||||
/**
|
||||
* @brief we don't want to allow network to be copied
|
||||
*/
|
||||
LayerNetwork(const LayerNetwork &f) = delete; //TODO
|
||||
/**
|
||||
* @brief we don't want to allow network to be assigned
|
||||
*/
|
||||
LayerNetwork operator=(const LayerNetwork &f)=delete;
|
||||
|
||||
/**
|
||||
* @brief computes output Solution from input Problem
|
||||
*/
|
||||
virtual Shin::Solution solve(const Shin::Problem& p) override;
|
||||
virtual size_t size() const override { return layers;};
|
||||
virtual LayerNetworkLayer& operator[](const size_t& l) override;
|
||||
protected:
|
||||
void solvePart(float *newSolution, size_t begin, size_t end,size_t prevSize, float* sol,size_t layer);
|
||||
private:
|
||||
LayerNetworkLayer **ffLayers=nullptr;
|
||||
float ***weights=nullptr;
|
||||
float **outputs=nullptr;
|
||||
float **inputs=nullptr;
|
||||
ActivationFunction::ActivationFunction **transfer=nullptr;
|
||||
BasisFunction::BasisFunction *basisFunction = new BasisFunction::FeedForward();
|
||||
size_t *layerSizes=nullptr;
|
||||
size_t layers;/**< Number of layers */
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <cstddef>
|
||||
|
||||
#include "../../Solution.h"
|
||||
#include "../FeedForward.h"
|
||||
#include "../LayerNetwork.h"
|
||||
#include "Learning.h"
|
||||
|
||||
/*
|
||||
@@ -28,7 +28,7 @@ namespace Learning
|
||||
class BackPropagation : public Learning
|
||||
{
|
||||
public:
|
||||
BackPropagation(FeedForward &n): Learning(), network(n) {}
|
||||
BackPropagation(LayerNetwork &n): Learning(), network(n) {}
|
||||
virtual ~BackPropagation();
|
||||
|
||||
BackPropagation(const NeuralNetwork::Learning::BackPropagation&) =delete;
|
||||
@@ -38,7 +38,7 @@ namespace Learning
|
||||
virtual void propagate(const Shin::Solution& expectation);
|
||||
|
||||
protected:
|
||||
FeedForward &network;
|
||||
LayerNetwork &network;
|
||||
inline virtual float correction(const float& expected, const float& computed) { return expected - computed;};
|
||||
|
||||
float **deltas=nullptr;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef _OPT_BACK_PROPAGATION_H_
|
||||
#define _OPT_BACK_PROPAGATION_H_
|
||||
|
||||
#include "../FeedForward.h"
|
||||
#include "BackPropagation.h"
|
||||
|
||||
/*
|
||||
@@ -15,7 +14,7 @@ namespace Learning
|
||||
class OpticalBackPropagation : public BackPropagation
|
||||
{
|
||||
public:
|
||||
inline OpticalBackPropagation(FeedForward &n): BackPropagation(n) {}
|
||||
inline OpticalBackPropagation(LayerNetwork &n): BackPropagation(n) {}
|
||||
protected:
|
||||
virtual float correction(const float& expected, const float& computed) override
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
OBJFILES=\
|
||||
FeedForward.o\
|
||||
LayerNetwork.o\
|
||||
Learning/Learning.o Learning/BackPropagation.o ../sse_mathfun.o
|
||||
|
||||
LINKFILES=
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
././Network.h
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <cstdarg>
|
||||
#include <vector>
|
||||
#include <initializer_list>
|
||||
|
||||
#include "../Problem.h"
|
||||
#include "../Solution.h"
|
||||
@@ -39,10 +38,10 @@ namespace NeuralNetwork
|
||||
virtual size_t size() const=0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Abstract model of simple Network
|
||||
*/
|
||||
/**
|
||||
* @author Tomas Cernik (Tom.Cernik@gmail.com)
|
||||
* @brief Abstract model of simple Network
|
||||
*/
|
||||
class Network
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
././Neuron.h
|
||||
Reference in New Issue
Block a user