MAKHFI.COM
Fascinating World of Neural Nets  
Neural Network Forums
Home PageHome Page : General Neural Networks Topics : c++ neural network
  You are currently not logged in. You can view the forums, but cannot post messages. | HOME | Log In | Register | Search | Help
Post a Reply on This Topic Post a Reply on This Topic

Author Topic: c++ neural network
fredhenry Posted: 18-Aug-11 07:47
  Edit Edit
 
Email the Author Mail   View Author's Profile Profile  
Hi Guys, I've tried about three separate times to make my own neural network and then test it with xor in c++.
Every time it fails to converge to the right answer even when run over 100 000 epochs or with different numbers of neurons.

I'm using the logistic function and backprogation.
The class is structures recursively, each layer is input the input from the layer before and outputs the error of each element in the layer behind it. By error I mean the ,sum over the neurons 1 layer ahead of a neuron, of the error of the ahead neuron times the weight between the two neurons.

For adjusting the weights I used the equation: (start neuron is j, end is i)
new weight[i][j] = old weight[i][j] + (learning rate)*error[i]*input[j]

I used I slightly modified form of xor to make it easier for the network:
input output
0 0 0.25
0 1 0.75
1 0 0.75
1 1 0.25

does anyone know what the problem is.

Thanks in advance.


main
[code]
#include "custom2.h"
#include "neural3.h"

double xor(usi i,usi j){
if(i==j){
return 0.25;
}else{
return 0.75;
}
}

int main(){
nnetwork test(2,1,3,6);
for(uli t=0;t<10000;t++){
if(t%100==0){
cout<<t<<endl;
}
for(usi i=0;i<2;i++){
for(usi j=0;j<2;j++){
vd in(0);
in.push_back(i);
in.push_back(j);
vd out(1,xor(i,j));
test.train(in,out,0.4);
}
}
}
for(usi i=0;i<2;i++){
for(usi j=0;j<2;j++){
vd in(0);
in.push_back(i);
in.push_back(j);
cout<<i<<"\t"<<"\t"<<j<<"\t"<<tes
t.run(in)[0]<<endl;
}
}
system("PAUSE");
}

[/code]



class
[code]

class layer{
public:
layer * ahead;
bool end;
vvd weights;

double wgtFunc(){
return 0;
}
layer(){
}
layer(usi size,usi inSize,ssi hidLeft,usi outNo){
end = false;
if(hidLeft == -1){
end = true;
}
//cout<<size<<"\t"<< inSize<<"\t"<< hidLeft<<"\t"<<true<<"\t"<<end<<endl;
for(usi i=0;i<size;i++){
weights.push_back(vd(0));
for(usi j=0;j<inSize;j++){
weights[i].push_back(wgtFunc());
}
weights[i].push_back(wgtFunc());
}
if(hidLeft > 0){
ahead = new layer(size,size,hidLeft-1,outNo);
}else if(hidLeft == 0){
ahead = new layer(outNo,size,-1,outNo);
}
}

double activation(double in){
return 1/(1+exp(-in));
//return (exp(in) - exp(-in))/(exp(in) + exp(in));
}
double derActivation(double in){
double act = activation(in);
return act*(1-act);
//return 1-act*act;
}
vd train(vd input,vd desOut,double LR){
//cout<<"train a\n"<<endl;

vd a(0);
vd out(0);
for(usi i=0;i<weights.size();i++){
double ain = 0;
if(input.size()!=weights[i].size()){
cerr<<"input != weights"<<endl;
}
for(usi j=0;j<input.size();j++){
ain += input[j]*weights[i][j];
}
a.push_back(ain);
out.push_back(activation(ain));
}
//cout<<"train b\n"<<endl;

vd error(0);
if(end){
//cout<<"q\n";
for(usi i=0;i<weights.size();i++){
error.push_back(out[i]-desOut[i]);
}
}else{
//cout<<"w\n";
vd tempOut = out;
tempOut.push_back(1);
vd weightedErr = (*ahead).train(tempOut,desOut,LR);
for(usi i=0;i<weights.size();i++){
error.push_back(weightedErr[i] * derActivation(a[i]));
}
}
//cout<<"train c\n"<<endl;

vd ret(0);
for(usi j=0;j<weights[0].size();j++){
double inRet=0;
for(usi i=0;i<weights.size();i++){
inRet += weights[i][j] * error[i];
}
ret.push_back(inRet);
}
//cout<<"train d\n"<<endl;

for(usi i=0;i<weights.size();i++){
for(usi j=0;j<weights[i].size();j++){
weights[i][j] -= LR * error[i] * input[j];
}
}
//cout<<"train e\n"<<endl;

return ret;
}
vd run(vd input){
vd a(0);
vd out(0);
for(usi i=0;i<weights.size();i++){
double ain = 0;
if(input.size()!=weights[i].size()){
cerr<<"input != weights"<<endl;
}
for(usi j=0;j<input.size();j++){
ain += input[j]*weights[i][j];
}
a.push_back(ain);
out.push_back(activation(ain));
}
if(end){
return out;
}else{
vd tempOut = out;
tempOut.push_back(1);
return (*ahead).run(tempOut);
}
}
};


class nnetwork{
layer first;
public:
nnetwork(usi inNo,usi otNo,usi hidNo,usi hidSize){
first = layer(hidSize,inNo,hidNo-1,otNo);
}
void train(vd input,vd desOut,double LR){
input.push_back(1);
first.train(input,desOut,LR);
}
vd run(vd input){
input.push_back(1);
return first.run(input);
}
};
[/code]
 
fredhenry Posted: 18-Aug-11 08:45
Delete Delete    Edit Edit
 
Email the Author Mail   View Author's Profile Profile  
It turn out that I needed to set the initial weights to a random number instead of 0 but I have no idea why
 

Post a Reply on This Topic Post a Reply on This Topic
 

Copyright © 2001-2003 Pejman Makhfi. All rights Reserved.