don't know
This commit is contained in:
8
src/Cuda/Makefile
Normal file
8
src/Cuda/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
all: test
|
||||
|
||||
VectorOperations.o: VectorOperations.cu VectorOperations.h
|
||||
/usr/local/cuda-6.5/bin/nvcc -c VectorOperations.cu -o VectorOperations.o
|
||||
|
||||
test: VectorOperations.o VectorOperations.h test.cpp
|
||||
g++ -c -std=c++14 -O0 ./test.cpp -o test.o
|
||||
/usr/local/cuda-6.5/bin/nvcc ./test.o VectorOperations.o -o test
|
||||
12
src/Cuda/VectorOperations.cpp
Normal file
12
src/Cuda/VectorOperations.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "./VectorOperations.h"
|
||||
|
||||
// CUDA kernel. Each thread takes care of one element of c
|
||||
__global__ void vecAdd(double *a, double *b, double *c, int n)
|
||||
{
|
||||
// Get our global thread ID
|
||||
int id = blockIdx.x*blockDim.x+threadIdx.x;
|
||||
|
||||
// Make sure we do not go out of bounds
|
||||
if (id < n)
|
||||
c[id] = a[id] + b[id];
|
||||
}
|
||||
47
src/Cuda/VectorOperations.cu
Normal file
47
src/Cuda/VectorOperations.cu
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "./VectorOperations.h"
|
||||
|
||||
// CUDA kernel. Each thread takes care of one element of c
|
||||
__global__ void __ADD(float *a, float *b, float *ret, int n)
|
||||
{
|
||||
// Get our global thread ID
|
||||
int id = blockIdx.x*blockDim.x+threadIdx.x;
|
||||
|
||||
if (id >= n)
|
||||
return;
|
||||
|
||||
// Make sure we do not go out of bounds
|
||||
float tmp=0.0;
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
tmp+=a[n] * b[n];
|
||||
}
|
||||
ret[id] = tmp;
|
||||
}
|
||||
|
||||
Shin::Cuda::VectorOperations::VectorOperations(int size)
|
||||
{
|
||||
cudaMalloc(&clientA, sizeof(float)*size);
|
||||
cudaMalloc(&clientB, sizeof(float)*size);
|
||||
cudaMalloc(&clientC, sizeof(float)*size);
|
||||
}
|
||||
|
||||
Shin::Cuda::VectorOperations::~VectorOperations()
|
||||
{
|
||||
cudaFree(clientA);
|
||||
cudaFree(clientB);
|
||||
cudaFree(clientC);
|
||||
}
|
||||
|
||||
void Shin::Cuda::VectorOperations::add(float *a, float *b, float *ret, int n)
|
||||
{
|
||||
//cudaMemcpyAsync(clientA, a, n*sizeof(float), cudaMemcpyHostToDevice, );
|
||||
//cudaMemcpyAsync(clientB, a, n*sizeof(float), cudaMemcpyHostToDevice, );
|
||||
cudaMemcpy( clientA, a, n*sizeof(float), cudaMemcpyHostToDevice);
|
||||
cudaMemcpy( clientB, b, n*sizeof(float), cudaMemcpyHostToDevice);
|
||||
cudaMemcpy( clientC, ret, n*sizeof(float), cudaMemcpyHostToDevice);
|
||||
int blockSize, gridSize;
|
||||
blockSize = 1024;
|
||||
gridSize = (int)ceil((float)n/blockSize);
|
||||
__ADD<<< 1, n>>>(clientA, clientB, clientC, n);
|
||||
cudaMemcpy( ret, clientC, n*sizeof(float), cudaMemcpyDeviceToHost );
|
||||
}
|
||||
20
src/Cuda/VectorOperations.h
Normal file
20
src/Cuda/VectorOperations.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef VECT_OP_
|
||||
#define VECT_OP_
|
||||
namespace Shin
|
||||
{
|
||||
namespace Cuda
|
||||
{
|
||||
class VectorOperations
|
||||
{
|
||||
public:
|
||||
VectorOperations(int maxSize);
|
||||
~VectorOperations();
|
||||
void add(float *a, float *b, float *ret, int n);
|
||||
protected:
|
||||
float *clientA;
|
||||
float *clientB;
|
||||
float *clientC;
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
BIN
src/Cuda/a
Executable file
BIN
src/Cuda/a
Executable file
Binary file not shown.
66
src/Cuda/answer.cu
Normal file
66
src/Cuda/answer.cu
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
|
||||
__global__ void vector_add(int *a, int *b, int *c)
|
||||
{
|
||||
/* insert code to calculate the index properly using blockIdx.x, blockDim.x, threadIdx.x */
|
||||
int index = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
c[index] = a[index] + b[index];
|
||||
}
|
||||
|
||||
/* experiment with N */
|
||||
/* how large can it be? */
|
||||
#define N (2048*2048)
|
||||
#define THREADS_PER_BLOCK 512
|
||||
|
||||
int main()
|
||||
{
|
||||
int *a, *b, *c;
|
||||
int *d_a, *d_b, *d_c;
|
||||
int size = N * sizeof( int );
|
||||
|
||||
/* allocate space for device copies of a, b, c */
|
||||
|
||||
cudaMalloc( (void **) &d_a, size );
|
||||
cudaMalloc( (void **) &d_b, size );
|
||||
cudaMalloc( (void **) &d_c, size );
|
||||
|
||||
/* allocate space for host copies of a, b, c and setup input values */
|
||||
|
||||
a = (int *)malloc( size );
|
||||
b = (int *)malloc( size );
|
||||
c = (int *)malloc( size );
|
||||
|
||||
for( int i = 0; i < N; i++ )
|
||||
{
|
||||
a[i] = b[i] = i;
|
||||
c[i] = 0;
|
||||
}
|
||||
|
||||
/* copy inputs to device */
|
||||
/* fix the parameters needed to copy data to the device */
|
||||
cudaMemcpy( d_a, a, size, cudaMemcpyHostToDevice );
|
||||
cudaMemcpy( d_b, b, size, cudaMemcpyHostToDevice );
|
||||
|
||||
/* launch the kernel on the GPU */
|
||||
/* insert the launch parameters to launch the kernel properly using blocks and threads */
|
||||
vector_add<<< (N + (THREADS_PER_BLOCK-1)) / THREADS_PER_BLOCK, THREADS_PER_BLOCK >>>( d_a, d_b, d_c );
|
||||
|
||||
/* copy result back to host */
|
||||
/* fix the parameters needed to copy data back to the host */
|
||||
cudaMemcpy( c, d_c, size, cudaMemcpyDeviceToHost );
|
||||
|
||||
|
||||
printf( "c[0] = %d\n",0,c[0] );
|
||||
printf( "c[%d] = %d\n",N-1, c[N-1] );
|
||||
|
||||
/* clean up */
|
||||
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
cudaFree( d_a );
|
||||
cudaFree( d_b );
|
||||
cudaFree( d_c );
|
||||
|
||||
return 0;
|
||||
} /* end main */
|
||||
66
src/Cuda/answer.cu~
Normal file
66
src/Cuda/answer.cu~
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
|
||||
__global__ void vector_add(int *a, int *b, int *c)
|
||||
{
|
||||
/* insert code to calculate the index properly using blockIdx.x, blockDim.x, threadIdx.x */
|
||||
int index = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
c[index] = a[index] + b[index];
|
||||
}
|
||||
|
||||
/* experiment with N */
|
||||
/* how large can it be? */
|
||||
#define N (2048*2048)
|
||||
#define THREADS_PER_BLOCK 512
|
||||
|
||||
int main()
|
||||
{
|
||||
int *a, *b, *c;
|
||||
int *d_a, *d_b, *d_c;
|
||||
int size = N * sizeof( int );
|
||||
|
||||
/* allocate space for device copies of a, b, c */
|
||||
|
||||
cudaMalloc( (void **) &d_a, size );
|
||||
cudaMalloc( (void **) &d_b, size );
|
||||
cudaMalloc( (void **) &d_c, size );
|
||||
|
||||
/* allocate space for host copies of a, b, c and setup input values */
|
||||
|
||||
a = (int *)malloc( size );
|
||||
b = (int *)malloc( size );
|
||||
c = (int *)malloc( size );
|
||||
|
||||
for( int i = 0; i < N; i++ )
|
||||
{
|
||||
a[i] = b[i] = i;
|
||||
c[i] = 0;
|
||||
}
|
||||
|
||||
/* copy inputs to device */
|
||||
/* fix the parameters needed to copy data to the device */
|
||||
cudaMemcpy( d_a, a, size, cudaMemcpyHostToDevice );
|
||||
cudaMemcpy( d_b, b, size, cudaMemcpyHostToDevice );
|
||||
|
||||
/* launch the kernel on the GPU */
|
||||
/* insert the launch parameters to launch the kernel properly using blocks and threads */
|
||||
add<<< (N + (THREADS_PER_BLOCK-1)) / THREADS_PER_BLOCK, THREADS_PER_BLOCK >>>( d_a, d_b, d_c );
|
||||
|
||||
/* copy result back to host */
|
||||
/* fix the parameters needed to copy data back to the host */
|
||||
cudaMemcpy( c, d_c, size, cudaMemcpyDeviceToHost );
|
||||
|
||||
|
||||
printf( "c[0] = %d\n",0,c[0] );
|
||||
printf( "c[%d] = %d\n",N-1, c[N-1] );
|
||||
|
||||
/* clean up */
|
||||
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
cudaFree( d_a );
|
||||
cudaFree( d_b );
|
||||
cudaFree( d_c );
|
||||
|
||||
return 0;
|
||||
} /* end main */
|
||||
BIN
src/Cuda/test
Executable file
BIN
src/Cuda/test
Executable file
Binary file not shown.
44
src/Cuda/test.cpp
Normal file
44
src/Cuda/test.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "./VectorOperations.h"
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
void _hack(float *a)
|
||||
{
|
||||
a[0]=a[0];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int size=50000000;
|
||||
float *a= new float[size];
|
||||
float *b= new float[size];
|
||||
float *c= new float[size];
|
||||
double sum = 0;
|
||||
Shin::Cuda::VectorOperations v(size);
|
||||
for(int i=0;i<size;i++)
|
||||
{
|
||||
a[i]=0.001;
|
||||
b[i]=2;
|
||||
c[i]=0;
|
||||
}
|
||||
|
||||
_hack(a);
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
for(int i=0;i<size;i++)
|
||||
{
|
||||
sum+=a[i]*b[i];
|
||||
}
|
||||
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
std::cout << "Time 1: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << "result: " << sum <<std::endl;
|
||||
|
||||
for(int i=0;i<size;i++)
|
||||
c[i]=0;
|
||||
t1 = std::chrono::high_resolution_clock::now();
|
||||
v.add(a,b,c,size);
|
||||
sum=0;
|
||||
for(int i=0;i<size;i++)
|
||||
sum+=c[i];
|
||||
t2 = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "Time 2: " << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count() << "result: " << sum << std::endl;
|
||||
}
|
||||
8
src/Makefile
Normal file
8
src/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
OBJFILES= sse_mathfun.o
|
||||
|
||||
include ../Makefile.const
|
||||
|
||||
all: $(OBJFILES)
|
||||
|
||||
clean:
|
||||
@rm -f ./*.o ./*.so ./*.a ./*.nm ./*/*.o
|
||||
@@ -122,13 +122,14 @@ void FeedForwardNetworkQuick::solvePart(float *newSolution, register size_t begi
|
||||
|
||||
Solution FeedForwardNetworkQuick::solve(const Problem& p)
|
||||
{
|
||||
std::vector<float> solution(p);
|
||||
register float* sol=sums[1];
|
||||
|
||||
for(size_t i=0;i<solution.size();i++)
|
||||
sums[0][0]=1;
|
||||
sol[0]=1;
|
||||
for(size_t i=0;i<p.size();i++)
|
||||
{
|
||||
sums[0][i+1]=solution[i];
|
||||
sol[i+1]=solution[i];
|
||||
sums[0][i+1]=p[i];
|
||||
sol[i+1]=p[i];
|
||||
}
|
||||
|
||||
register size_t prevSize=layerSizes[0];
|
||||
@@ -160,7 +161,7 @@ Solution FeedForwardNetworkQuick::solve(const Problem& p)
|
||||
prevSize=layerSizes[i];
|
||||
sol=newSolution;
|
||||
}
|
||||
std::vector<float> ret;
|
||||
Solution ret;
|
||||
for(size_t i=1;i<prevSize;i++)
|
||||
{
|
||||
ret.push_back(sol[i]);
|
||||
|
||||
@@ -20,7 +20,7 @@ Shin::NeuronNetwork::IO::~IO()
|
||||
|
||||
}
|
||||
|
||||
Shin::NeuronNetwork::IO Shin::NeuronNetwork::IO::operator+(IO &r)
|
||||
Shin::NeuronNetwork::IO Shin::NeuronNetwork::IO::operator+(const IO &r)
|
||||
{
|
||||
Shin::NeuronNetwork::IO tmp;
|
||||
for(float a:this->data)
|
||||
@@ -39,6 +39,11 @@ Shin::NeuronNetwork::IO::operator std::vector<float>&()
|
||||
return data;
|
||||
}
|
||||
|
||||
Shin::NeuronNetwork::IO::operator std::vector<float>()
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
float Shin::NeuronNetwork::IO::operator[](size_t pos) const
|
||||
{
|
||||
|
||||
@@ -15,8 +15,9 @@ class IO
|
||||
IO(std::vector<float> &d);
|
||||
IO(const IO &old);
|
||||
virtual ~IO();
|
||||
IO operator+(IO &r);
|
||||
IO operator+(const IO &r);
|
||||
virtual operator std::vector<float>&() final; // TOO CONST
|
||||
virtual operator std::vector<float>() final; // TOO CONST
|
||||
float operator[] (size_t pos) const;
|
||||
size_t size() const;
|
||||
protected:
|
||||
|
||||
@@ -99,7 +99,7 @@ float Shin::NeuronNetwork::Learning::BackPropagation::teach(const Shin::NeuronNe
|
||||
Shin::NeuronNetwork::Solution a=network.solve(p);
|
||||
double error=calculateError(solution,a);
|
||||
|
||||
std::vector<float> s;
|
||||
Solution s;
|
||||
if(entropy)
|
||||
{
|
||||
for(size_t i=0;i<solution.size();i++)
|
||||
|
||||
1
src/NeuronNetwork/Learning/QLearning
Symbolic link
1
src/NeuronNetwork/Learning/QLearning
Symbolic link
@@ -0,0 +1 @@
|
||||
./QLearning.h
|
||||
38
src/NeuronNetwork/Learning/QLearning.cpp
Normal file
38
src/NeuronNetwork/Learning/QLearning.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "./QLearning"
|
||||
|
||||
Shin::NeuronNetwork::Learning::QLearning::QLearning(size_t input, size_t size, size_t choices):fun()
|
||||
{
|
||||
fun.initialiseNetwork(input,size,choices);
|
||||
}
|
||||
|
||||
Shin::NeuronNetwork::Learning::QLearning::~QLearning()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::Learning::QLearning::learnDelayed(std::vector< std::pair< Shin::NeuronNetwork::Solution, Shin::NeuronNetwork::Problem > >& p, float quality)
|
||||
{
|
||||
fun.learnDelayed(p,quality);
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::Learning::QLearning::learnDelayed(std::vector< std::pair< Shin::NeuronNetwork::Problem,int > >& p, float quality)
|
||||
{
|
||||
fun.learnDelayed(p,quality);
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::Learning::QLearning::learn(Shin::NeuronNetwork::Solution& s, Shin::NeuronNetwork::Problem& p, float quality)
|
||||
{
|
||||
fun.learn(s,p,quality);
|
||||
}
|
||||
|
||||
|
||||
void Shin::NeuronNetwork::Learning::QLearning::learn(Shin::NeuronNetwork::Problem& s, int action, float quality)
|
||||
{
|
||||
fun.learn(s,action,quality);
|
||||
}
|
||||
|
||||
|
||||
int Shin::NeuronNetwork::Learning::QLearning::getChoice(Shin::NeuronNetwork::Problem& p)
|
||||
{
|
||||
return fun.getChoice(p);
|
||||
}
|
||||
70
src/NeuronNetwork/Learning/QLearning.h
Normal file
70
src/NeuronNetwork/Learning/QLearning.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef _QLEARNING_H_
|
||||
#define _QLEARNING_H_
|
||||
|
||||
#include <math.h>
|
||||
#include <cstddef>
|
||||
|
||||
#include "../Problem.h"
|
||||
#include "../FeedForwardQuick.h"
|
||||
#include "BackPropagation"
|
||||
#include "Unsupervised"
|
||||
#include "RL/QFunction.h"
|
||||
#include "OpticalBackPropagation.h"
|
||||
#include "functional"
|
||||
|
||||
/*
|
||||
* http://www2.econ.iastate.edu/tesfatsi/RLUsersGuide.ICAC2005.pdf
|
||||
* http://www.autonlab.org/tutorials/rl06.pdf
|
||||
* http://www.nbu.bg/cogs/events/2000/Readings/Petrov/rltutorial.pdf
|
||||
*
|
||||
* http://www.applied-mathematics.net/qlearning/qlearning.html
|
||||
* http://nn.cs.utexas.edu/downloads/papers/stanley.gecco02_1.pdf
|
||||
*
|
||||
* http://stackoverflow.com/questions/740389/good-implementations-of-reinforced-learning
|
||||
*
|
||||
* http://stackoverflow.com/questions/10722064/training-a-neural-network-with-reinforcement-learning
|
||||
*
|
||||
* http://remi.coulom.free.fr/Thesis/
|
||||
* http://remi.coulom.free.fr/Publications/Thesis.pdf
|
||||
*
|
||||
* http://link.springer.com/article/10.1007/BF00992696
|
||||
*
|
||||
* http://scholar.google.cz/scholar?start=10&q=reinforcement+learning+feedforward&hl=en&as_sdt=0,5&as_vis=1
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Shin
|
||||
{
|
||||
namespace NeuronNetwork
|
||||
{
|
||||
namespace Learning
|
||||
{
|
||||
class QLearning
|
||||
{
|
||||
public:
|
||||
QLearning(size_t input, size_t size, size_t choices);
|
||||
~QLearning();
|
||||
|
||||
QLearning(const QLearning&) =delete;
|
||||
QLearning& operator=(const QLearning&) =delete;
|
||||
|
||||
void learnDelayed(std::vector<std::pair<Solution,Problem>> &p, float quality);
|
||||
void learnDelayed(std::vector<std::pair<Problem,int>> &p, float quality);
|
||||
void learn(Solution &s, Problem &p, float quality);
|
||||
void learn(Problem &p,int action, float quality);
|
||||
|
||||
void learnNetwork(double maxError=0.01);
|
||||
void learningCoeficient(double t);
|
||||
|
||||
void initialise(size_t input, size_t size,size_t choices);
|
||||
int getChoice(Problem &p);
|
||||
Solution getSolution(Problem &p) {return fun.getSolution(p);}
|
||||
void setLearningCoeficient(double ok, double err) {fun.setLearningCoeficient(ok,err);};
|
||||
void opticalBackPropagation() {fun.opticalBackPropagation();};
|
||||
protected:
|
||||
RL::QFunctionNetwork fun;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -1,6 +1,174 @@
|
||||
#include "./QFunction"
|
||||
|
||||
Shin::NN::RL::QFunction::QFunction()
|
||||
Shin::NeuronNetwork::RL::QFunction::QFunction():learningCoeficient(0.1)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Shin::NeuronNetwork::RL::QFunction::~QFunction()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Shin::NeuronNetwork::RL::QFunctionTable::QFunctionTable(float maxIODiff) :QFunction(),diff(maxIODiff),data(Shin::NeuronNetwork::RL::QFunctionTable::IOComparator(maxIODiff))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float Shin::NeuronNetwork::RL::QFunctionTable::getScore(Shin::NeuronNetwork::Solution& s, Shin::NeuronNetwork::Problem& p)
|
||||
{
|
||||
auto element=data.find(p);
|
||||
if(element==data.end())
|
||||
return 0;
|
||||
auto solution=element->second.find(s);
|
||||
if(solution==element->second.end())
|
||||
return 0;
|
||||
return solution->second.first/solution->second.second;
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::RL::QFunctionTable::learn(Shin::NeuronNetwork::Solution& s, Shin::NeuronNetwork::Problem& p, float quality)
|
||||
{
|
||||
auto element=data.find(p);
|
||||
if(element==data.end())
|
||||
{
|
||||
data[p]=std::map<Solution,std::pair<float,int>,Shin::NeuronNetwork::RL::QFunctionTable::IOComparator>(Shin::NeuronNetwork::RL::QFunctionTable::IOComparator(diff));
|
||||
element=data.find(p);
|
||||
}
|
||||
auto solution=element->second.find(s);
|
||||
if(solution==element->second.end())
|
||||
{
|
||||
element->second[s].first=quality;
|
||||
element->second[s].second=1;
|
||||
}else
|
||||
{
|
||||
solution->second.first+=quality;
|
||||
solution->second.second++;
|
||||
}
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::RL::QFunctionTable::learnDelayed(std::vector< std::pair< Shin::NeuronNetwork::Solution, Shin::NeuronNetwork::Problem > >& p, float quality)
|
||||
{
|
||||
for(int i=p.size()-1;i>=0;i--)
|
||||
{
|
||||
auto &pair=p[i];
|
||||
learn(pair.first,pair.second,quality);
|
||||
quality*=0.3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Shin::NeuronNetwork::RL::QFunctionNetwork::QFunctionNetwork() : b(nullptr),function(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Shin::NeuronNetwork::RL::QFunctionNetwork::~QFunctionNetwork()
|
||||
{
|
||||
if(function !=nullptr)
|
||||
{
|
||||
delete b;
|
||||
delete function;
|
||||
}
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::RL::QFunctionNetwork::initialiseNetwork(size_t input, size_t size,size_t choices)
|
||||
{
|
||||
if(function == nullptr)
|
||||
{
|
||||
function = new FeedForwardNetworkQuick({(int)input,(int)size,(int)choices});
|
||||
b= new Learning::BackPropagation(*function);
|
||||
}
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::RL::QFunctionNetwork::learnDelayed(std::vector< std::pair< Shin::NeuronNetwork::Solution, Shin::NeuronNetwork::Problem > >& p, float quality)
|
||||
{
|
||||
if(quality>0)
|
||||
{
|
||||
b->setLearningCoeficient(learningA);
|
||||
// b->setLearningCoeficient(0.05);
|
||||
}else
|
||||
{
|
||||
b->setLearningCoeficient(learningB);
|
||||
// b->setLearningCoeficient(0.008);
|
||||
}
|
||||
|
||||
for(int i=p.size()-1;i>=0;i--)
|
||||
{
|
||||
learn(p[i].first,p[i].second,quality);
|
||||
quality*=0.95;
|
||||
}
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::RL::QFunctionNetwork::learnDelayed(std::vector< std::pair< Shin::NeuronNetwork::Problem,int> >& p, float quality) // TODO there must be better way
|
||||
{
|
||||
std::vector<std::pair<Solution,Problem>> q;
|
||||
register int solSize=0;
|
||||
if(p.size()>0)
|
||||
solSize=function->solve(p[0].first).size();
|
||||
if (!solSize)
|
||||
return;
|
||||
for(int i=0;i<p.size();i++)
|
||||
{
|
||||
Solution s;
|
||||
for(int j=0;j<solSize;j++)
|
||||
{
|
||||
s.push_back(j==p[i].second?1:0);
|
||||
}
|
||||
q.push_back(std::pair<Solution,Problem>(s,p[i].first));
|
||||
}
|
||||
learnDelayed(q,quality);
|
||||
}
|
||||
|
||||
|
||||
void Shin::NeuronNetwork::RL::QFunctionNetwork::learn(Shin::NeuronNetwork::Solution& s, Shin::NeuronNetwork::Problem& p, float quality)
|
||||
{
|
||||
register int loops=abs(quality)/10;
|
||||
if(quality > 0)
|
||||
{
|
||||
while(loops-- >= 0)
|
||||
{
|
||||
b->teach(p,s);
|
||||
}
|
||||
}else
|
||||
{
|
||||
Shin::NeuronNetwork::Solution e;
|
||||
for(int j=s.size()-1;j>=0;j--)
|
||||
{
|
||||
e.push_back(((float)(100-(rand()%101)))/100.0);
|
||||
}
|
||||
while(loops-->=0)
|
||||
{
|
||||
b->teach(p,e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Shin::NeuronNetwork::RL::QFunctionNetwork::learn(Shin::NeuronNetwork::Problem& p, int choice, float quality)
|
||||
{
|
||||
if(quality>0)
|
||||
{
|
||||
b->setLearningCoeficient(learningA);
|
||||
}else
|
||||
{
|
||||
b->setLearningCoeficient(learningB);
|
||||
}
|
||||
// int choice= getChoice(p);
|
||||
Shin::NeuronNetwork::Solution s=function->solve(p);
|
||||
learn(s,p,quality);
|
||||
}
|
||||
|
||||
int Shin::NeuronNetwork::RL::QFunctionNetwork::getChoice(Shin::NeuronNetwork::Problem& p)
|
||||
{
|
||||
Solution s=function->solve(p);
|
||||
float max=-888888888888; //TODO
|
||||
int j=0;
|
||||
for(size_t i=0;i<s.size();i++)
|
||||
{
|
||||
if(s[i]>max)
|
||||
{
|
||||
max=s[i];
|
||||
j=i;
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,96 @@
|
||||
namespace Shin
|
||||
#ifndef _Q_FUNCTION_H_
|
||||
#define _Q_FUNCTION_H_
|
||||
|
||||
#include "../../Solution"
|
||||
#include "../../FeedForwardQuick"
|
||||
#include "../BackPropagation.h"
|
||||
#include "../OpticalBackPropagation.h"
|
||||
#include <map>
|
||||
namespace Shin
|
||||
{
|
||||
namespace NN
|
||||
namespace NeuronNetwork
|
||||
{
|
||||
namespace RL
|
||||
{
|
||||
class QFunction
|
||||
{
|
||||
public:
|
||||
QFunction();
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
class QFunction
|
||||
{
|
||||
public:
|
||||
QFunction();
|
||||
virtual ~QFunction();
|
||||
virtual void learnDelayed(std::vector<std::pair<Solution,Problem>> &p, float quality)=0;
|
||||
virtual void learn(Solution &s, Problem &p, float quality)=0;
|
||||
protected:
|
||||
float learningCoeficient;
|
||||
};
|
||||
|
||||
class QFunctionTable : public QFunction
|
||||
{
|
||||
public:
|
||||
QFunctionTable(float maxIODiff=0.01);
|
||||
virtual void learnDelayed(std::vector<std::pair<Solution,Problem>> &p, float quality) override;
|
||||
virtual void learn(Solution &s, Problem &p, float quality) override;
|
||||
virtual float getScore(Solution &s, Problem &p);
|
||||
inline size_t size() {return data.size();}
|
||||
auto begin() {return data.begin();};
|
||||
auto end() {return data.end();};
|
||||
protected:
|
||||
float diff;
|
||||
struct IOComparator
|
||||
{
|
||||
IOComparator(float maxDiff=0):maxIODifference(maxDiff) {}
|
||||
bool operator() (const IO& l, const IO& r) const
|
||||
{
|
||||
if(l.size()!=r.size())
|
||||
{
|
||||
if(l.size() <r.size())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
for(size_t i=0;i<l.size();i++)
|
||||
{
|
||||
register float diff=l[i]-r[i];
|
||||
if(fabs(diff) > maxIODifference)
|
||||
{
|
||||
if(diff < maxIODifference)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
float maxIODifference=0.1;
|
||||
};
|
||||
std::map<Problem,std::map<Solution,std::pair<float,int>,IOComparator>,IOComparator> data;
|
||||
};
|
||||
|
||||
class QFunctionNetwork : public QFunction
|
||||
{
|
||||
public:
|
||||
QFunctionNetwork();
|
||||
QFunctionNetwork(const QFunctionNetwork&) =delete;
|
||||
virtual ~QFunctionNetwork();
|
||||
|
||||
QFunctionNetwork operator=(const QFunctionNetwork&) =delete;
|
||||
void initialiseNetwork(size_t input, size_t size,size_t choices);
|
||||
|
||||
virtual void learnDelayed(std::vector<std::pair<Solution,Problem>> &p, float quality) override;
|
||||
virtual void learnDelayed(std::vector<std::pair<Problem,int>> &p, float quality);
|
||||
virtual void learn(Solution &s, Problem &p, float quality) override;
|
||||
virtual void learn(Problem &s, int choice, float quality);
|
||||
|
||||
virtual int getChoice(Problem &p);
|
||||
virtual Solution getSolution(Problem &p) {return function->solve(p);}
|
||||
void setLearningCoeficient(double ok, double err) {learningA=ok;learningB=err;};
|
||||
void opticalBackPropagation() {delete b; b=new Learning::OpticalBackPropagation(*function);};
|
||||
private:
|
||||
Learning::BackPropagation *b;
|
||||
FeedForwardNetworkQuick * function;
|
||||
float learningA=0.05;
|
||||
float learningB=0.008;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -20,20 +20,22 @@ float Shin::NeuronNetwork::Learning::Reinforcement::learn(const Shin::NeuronNetw
|
||||
//network[2]->operator[](0)->setWeight(0,-5);
|
||||
Solution s=network.solve(problem);
|
||||
float quality=qualityFunction(problem,s);
|
||||
std::vector<float> q;
|
||||
Solution q;
|
||||
//std::cerr << s[0] << "\n";
|
||||
for(register size_t j=0;j<s.size();j++)
|
||||
{
|
||||
q.push_back(s[j]);//*((float)(990+(rand()%21))/1000.0));
|
||||
}
|
||||
|
||||
register int i=abs((int)quality);
|
||||
|
||||
if(quality <= 0 && i > 0)
|
||||
if(quality > 0)
|
||||
{
|
||||
for(register size_t j=0;j<s.size();j++)
|
||||
{
|
||||
q[j]=((float)(100-(rand()%101)))/100.0;
|
||||
q.push_back(s[j]);//*((float)(990+(rand()%21))/1000.0));
|
||||
}
|
||||
}else if(quality <= 0 && i >= 0)
|
||||
{
|
||||
for(register size_t j=0;j<s.size();j++)
|
||||
{
|
||||
q.push_back(((float)(100-(rand()%101)))/100.0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef _UNSUPERVISEDLEARNING_H_
|
||||
#define _UNSUPERVIESDLERANING_H_
|
||||
#define _UNSUPERVISEDLEARNING_H_
|
||||
|
||||
#include <math.h>
|
||||
#include <cstddef>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
OBJFILES= Neuron.o Network.o FeedForward.o FeedForwardQuick.o\
|
||||
Learning/Supervised.o Learning/BackPropagation.o Learning/OpticalBackPropagation.o\
|
||||
Learning/Unsupervised.o Learning/Reinforcement.o\
|
||||
Learning/Unsupervised.o Learning/Reinforcement.o Learning/RL/QFunction.o Learning/QLearning.o\
|
||||
Solution.o Problem.o ./IO.o
|
||||
|
||||
LINKFILES= ../sse_mathfun.o
|
||||
|
||||
@@ -21,7 +21,7 @@ Layer::~Layer()
|
||||
|
||||
Solution Layer::solve(const std::vector<float> &input)
|
||||
{
|
||||
std::vector <float> ret;
|
||||
Solution ret;
|
||||
for(Neuron *n:neurons)
|
||||
{
|
||||
ret.push_back(n->output(input));
|
||||
|
||||
@@ -7,7 +7,7 @@ Problem::Problem()
|
||||
|
||||
}
|
||||
|
||||
Problem::operator std::vector<float>() const
|
||||
Problem::Problem(std::vector< float >& p): IO(p)
|
||||
{
|
||||
return representation();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,18 +3,17 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include "IO.h"
|
||||
|
||||
namespace Shin
|
||||
{
|
||||
namespace NeuronNetwork
|
||||
{
|
||||
class Problem
|
||||
class Problem : public IO
|
||||
{
|
||||
public:
|
||||
Problem();
|
||||
virtual ~Problem(){};
|
||||
operator std::vector<float>() const;
|
||||
virtual std::vector<float> representation() const =0;
|
||||
Problem(std::vector<float> &p);
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
@@ -6,3 +6,13 @@ Solution::Solution(std::vector<float>&sol):IO(sol)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Solution::Solution(std::vector< float > solution):IO(solution)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Solution::push_back(float d)
|
||||
{
|
||||
data.push_back(d);
|
||||
}
|
||||
|
||||
@@ -11,8 +11,11 @@ namespace NeuronNetwork
|
||||
class Solution : public IO
|
||||
{
|
||||
public:
|
||||
Solution(const Problem& p);
|
||||
Solution(): IO() {};
|
||||
Solution(const Problem& p) :IO(p) {};
|
||||
Solution(std::vector<float> &solution);
|
||||
Solution(std::vector<float> solution);
|
||||
void push_back(float);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
BIN
src/NeuronNetwork/stl4jXKW
Normal file
BIN
src/NeuronNetwork/stl4jXKW
Normal file
Binary file not shown.
@@ -3,12 +3,14 @@ include ../Makefile.const
|
||||
OPTIMALIZATION=
|
||||
LIB_DIR = ../lib
|
||||
GEN_TESTS=g-01 g-02
|
||||
NN_TESTS= \
|
||||
NN_TESTS=\
|
||||
nn-01 nn-02 nn-03 nn-bp-sppeed \
|
||||
nn-bp-xor \
|
||||
nn-obp-xor \
|
||||
nn-rl-xor nn-rl-and \
|
||||
nn-rl-xor nn-rl-and nn-rl-qfun\
|
||||
nn-reinforcement nn-04
|
||||
# nn-test nn-rl-qfun\
|
||||
|
||||
ALL_TESTS=$(NN_TESTS) $(GEN_TESTS)
|
||||
|
||||
LIBS=$(LIB_DIR)/Genetics.a $(LIB_DIR)/NeuronNetwork.a
|
||||
|
||||
@@ -5,20 +5,15 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
//typedef Shin::NeuronNetwork::Problem X;
|
||||
|
||||
class X: public Shin::NeuronNetwork::Problem
|
||||
{
|
||||
public:
|
||||
X(const X& a) :q(a.q) {}
|
||||
X(const std::vector<float> &a):q(a) {}
|
||||
X(const std::vector<bool> &a):q() {for(bool s:a) q.push_back((float)s);}
|
||||
std::vector<float> representation() const
|
||||
{
|
||||
return q;
|
||||
}
|
||||
X(const X& a) :Problem(a) {}
|
||||
X(const std::vector<bool> &a):Problem() { for (bool s:a) data.push_back((float)s);}
|
||||
protected:
|
||||
std::vector<float> q;
|
||||
};
|
||||
|
||||
int main(int argc,char**)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
class X: public Shin::NeuronNetwork::Problem
|
||||
{
|
||||
public:
|
||||
X(const X& a) :q(a.q) {}
|
||||
X(const X& a) :Problem(),q(a.q) {}
|
||||
X(const std::vector<float> &a):q(a) {}
|
||||
std::vector<float> representation() const
|
||||
{
|
||||
@@ -33,8 +33,9 @@ int main()
|
||||
s.push_back(Shin::NeuronNetwork::Solution(std::vector<float>({1})));
|
||||
p.push_back(X(std::vector<float>({1,1})));
|
||||
|
||||
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,2,4,1});
|
||||
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,4,1});
|
||||
Shin::NeuronNetwork::Learning::BackPropagation b(q);
|
||||
b.setLearningCoeficient(10);
|
||||
|
||||
b.debugOn();
|
||||
for(int i=0;i<4;i++)
|
||||
@@ -45,7 +46,7 @@ int main()
|
||||
}
|
||||
b.debugOff();
|
||||
|
||||
for(int i=0;i<4000;i++)
|
||||
for(int i=0;i<40000;i++)
|
||||
{
|
||||
b.teach(p[i%4],s[i%4]);
|
||||
}
|
||||
|
||||
@@ -7,14 +7,8 @@
|
||||
class X: public Shin::NeuronNetwork::Problem
|
||||
{
|
||||
public:
|
||||
X(const X& a) :q(a.q) {}
|
||||
X(const std::vector<float> &a):q(a) {}
|
||||
std::vector<float> representation() const
|
||||
{
|
||||
return q;
|
||||
}
|
||||
protected:
|
||||
std::vector<float> q;
|
||||
X(const X& a) :Problem(a) {}
|
||||
X(const std::vector<float> &a):Problem() {data=a;}
|
||||
};
|
||||
|
||||
int main()
|
||||
@@ -66,7 +60,7 @@ int main()
|
||||
std::cerr << j << "(" << err <<"):\n";
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
std::cerr << "\t" << i%4 <<". FOR: [" << p[i%4]->representation()[0] << "," <<p[i%4]->representation()[1] << "] res: " <<
|
||||
std::cerr << "\t" << i%4 <<". FOR: [" << p[i%4]->operator[](0) << "," <<p[i%4]->operator[](1) << "] res: " <<
|
||||
q.solve(*p[i%4])[0] << " should be " << s[i%4]->operator[](0)<<"\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,8 @@
|
||||
class X: public Shin::NeuronNetwork::Problem
|
||||
{
|
||||
public:
|
||||
X(const X& a) :q(a.q) {}
|
||||
X(const std::vector<float> &a):q(a) {}
|
||||
std::vector<float> representation() const
|
||||
{
|
||||
return q;
|
||||
}
|
||||
protected:
|
||||
std::vector<float> q;
|
||||
X(const X& a) :Problem(a) {}
|
||||
X(const std::vector<float> &a):Problem() {data=a;}
|
||||
};
|
||||
|
||||
int main()
|
||||
@@ -67,7 +61,7 @@ int main()
|
||||
std::cerr << j << "(" << err <<"):\n";
|
||||
for(int i=0;i<4;i++)
|
||||
{
|
||||
std::cerr << "\t" << i%4 <<". FOR: [" << p[i%4]->representation()[0] << "," <<p[i%4]->representation()[1] << "] res: " <<
|
||||
std::cerr << "\t" << i%4 <<". FOR: [" << p[i%4]->operator[](0) << "," <<p[i%4]->operator[](1) << "] res: " <<
|
||||
q.solve(*p[i%4])[0] << " should be " << s[i%4]->operator[](0)<<"\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,8 @@
|
||||
class X: public Shin::NeuronNetwork::Problem
|
||||
{
|
||||
public:
|
||||
X(const X& a) :q(a.q) {}
|
||||
X(const std::vector<float> &a):q(a) {}
|
||||
std::vector<float> representation() const
|
||||
{
|
||||
return q;
|
||||
}
|
||||
protected:
|
||||
std::vector<float> q;
|
||||
X(const X& a) :Problem(a) {}
|
||||
X(const std::vector<float> &a):Problem() {data=a;}
|
||||
};
|
||||
|
||||
int main()
|
||||
@@ -28,14 +22,17 @@ int main()
|
||||
|
||||
p.push_back(new X(std::vector<float>({1,1})));
|
||||
|
||||
Shin::NeuronNetwork::FeedForwardNetworkQuick q({1,1});
|
||||
p.push_back(new X(std::vector<float>({1,0})));
|
||||
p.push_back(new X(std::vector<float>({0,1})));
|
||||
|
||||
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,1});
|
||||
Shin::NeuronNetwork::Learning::Reinforcement b(q);
|
||||
int i=0;
|
||||
double targetQuality=1.4;
|
||||
double targetQuality=0.5;
|
||||
b.setQualityFunction(
|
||||
[](const Shin::NeuronNetwork::Problem &pr,const Shin::NeuronNetwork::Solution &s)->float
|
||||
{
|
||||
if(pr.representation()[0]==0)
|
||||
if(pr[0]==1 && pr[1]==1)
|
||||
{
|
||||
//ocekavame 1
|
||||
int e=(s[0]-0.80)*15.0;//+(abs(s[1])-0.5)*100.0;
|
||||
@@ -54,12 +51,12 @@ int main()
|
||||
|
||||
if(i%100000==0)
|
||||
srand(time(NULL));
|
||||
if(err > targetQuality)
|
||||
if(err > targetQuality||i%1000==0)
|
||||
{
|
||||
std::cerr << i << " ("<< err <<").\n";
|
||||
for(int j=0;j<2;j++)
|
||||
for(int j=0;j<4;j++)
|
||||
{
|
||||
std::cerr << j%4 <<". FOR: [" << p[j%4]->representation()[0] << "," <<p[j%4]->representation()[0] << "] res: " << q.solve(*p[j%4])[0] << "\n";
|
||||
std::cerr << j%4 <<". FOR: [" << p[j%4]->operator[](0) << "," <<p[j%4]->operator[](0) << "] res: " << q.solve(*p[j%4])[0] << "\n";
|
||||
}
|
||||
}
|
||||
if(err >targetQuality)
|
||||
|
||||
@@ -5,19 +5,15 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
|
||||
class X: public Shin::NeuronNetwork::Problem
|
||||
{
|
||||
public:
|
||||
X(const X& a) :q(a.q) {}
|
||||
X(const std::vector<float> &a):q(a) {}
|
||||
std::vector<float> representation() const
|
||||
{
|
||||
return q;
|
||||
}
|
||||
protected:
|
||||
std::vector<float> q;
|
||||
X(const X& a) :Problem(a) {}
|
||||
X(const std::vector<float> &a):Problem() {data=a;}
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
srand(time(NULL));
|
||||
@@ -26,9 +22,9 @@ int main()
|
||||
Shin::NeuronNetwork::FeedForwardNetworkQuick q({2,4,1});
|
||||
Shin::NeuronNetwork::Learning::Reinforcement b(q);
|
||||
//b.setPropagator(new Shin::NeuronNetwork::Learning::OpticalBackPropagation(q));
|
||||
b.getPropagator().setLearningCoeficient(3);
|
||||
b.getPropagator().setLearningCoeficient(0.4);
|
||||
//b.getPropagator().allowEntropy();
|
||||
double targetQuality =1;
|
||||
double targetQuality =2.9;
|
||||
if(test==2)
|
||||
{
|
||||
targetQuality =1.62;
|
||||
@@ -38,9 +34,8 @@ int main()
|
||||
b.getPropagator().setLearningCoeficient(0.5);
|
||||
}
|
||||
b.setQualityFunction(
|
||||
[](const Shin::NeuronNetwork::Problem &pr,const Shin::NeuronNetwork::Solution &s)->float
|
||||
[](const Shin::NeuronNetwork::Problem &p,const Shin::NeuronNetwork::Solution &s)->float
|
||||
{
|
||||
std::vector <float> p=pr;
|
||||
float expect=0.0;
|
||||
if(p[0] && p[1])
|
||||
expect=0;
|
||||
@@ -55,10 +50,10 @@ int main()
|
||||
|
||||
if(expect==0)
|
||||
{
|
||||
expect=0.1-abs(s[0]);
|
||||
expect=0.3-abs(s[0]);
|
||||
}else
|
||||
{
|
||||
expect=s[0]-0.9;
|
||||
expect=s[0]-0.7;
|
||||
}
|
||||
|
||||
// std::cerr << " returnning " << expect*5.0 << "\n";
|
||||
@@ -93,7 +88,7 @@ int main()
|
||||
std::cerr << i << " ("<< err <<").\n";
|
||||
for(int j=0;j<4;j++)
|
||||
{
|
||||
std::cerr << "\t" << j%4 << ". FOR: [" << p[j%4]->representation()[0] << "," <<p[j%4]->representation()[1] << "] res: " <<
|
||||
std::cerr << "\t" << i%4 <<". FOR: [" << p[j%4]->operator[](0) << "," <<p[j%4]->operator[](1) << "] res: " <<
|
||||
q.solve(*p[j%4])[0] << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user