Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

training neural net..

I am using opencv 2.4 with visual studio 2012... here is the code ..

Ptr<ANN_MLP>neural_net=ANN_MLP::create();
neural_net->setActivationFunction(ANN_MLP::SIGMOID_SYM);
neural_net->setTrainMethod(ANN_MLP::BACKPROP);

Mat layers(3,1,CV_32FC1);
layers.row(0)=2;
layers.row(1)=2;
layers.row(2)=1;

std::cout<<"neural layers\n"<<layers;
neural_net->setLayerSizes(layers);
TermCriteria Term(CV_TERMCRIT_ITER,2,1.0);
neural_net->setTermCriteria(Term);
Mat trainig_data(4,1,CV_32FC1);

Ptr<TrainData>Train_data=TrainData::create();

I want to train this network for AND operation ..I am confused about creating its training data sets... how shall I move if I want to train for following datasets...

image description

assume that input A and B are fed to input layer neurons and expected output from output layer in C Thank you..

training neural net..

I am using opencv 2.4 with visual studio 2012... here is the code ..

Ptr<ANN_MLP>neural_net=ANN_MLP::create();
neural_net->setActivationFunction(ANN_MLP::SIGMOID_SYM);
neural_net->setTrainMethod(ANN_MLP::BACKPROP);

Mat layers(3,1,CV_32FC1);
layers.row(0)=2;
layers.row(1)=2;
layers.row(2)=1;

std::cout<<"neural layers\n"<<layers;
neural_net->setLayerSizes(layers);
TermCriteria Term(CV_TERMCRIT_ITER,2,1.0);
neural_net->setTermCriteria(Term);
Mat trainig_data(4,1,CV_32FC1);

Ptr<TrainData>Train_data=TrainData::create();

I want to train this network for AND OR operation ..I am confused about creating its training data sets... how shall I move if I want to train for following datasets...

image description

assume that input A and B are fed to input layer neurons and expected output from output layer in C Thank you..

training neural net..

I am using opencv 2.4 with visual studio 2012... here is the code ..

Ptr<ANN_MLP>neural_net=ANN_MLP::create();
neural_net->setActivationFunction(ANN_MLP::SIGMOID_SYM);
neural_net->setTrainMethod(ANN_MLP::BACKPROP);

Ptr<ANN_MLP>neural_net=ANN_MLP::create(); //empty neural model..
    neural_net->setActivationFunction(ANN_MLP::SIGMOID_SYM); //neural activation function...
    neural_net->setTrainMethod(ANN_MLP::BACKPROP);  ///training algorithm...
    Mat layers(3,1,CV_32FC1);
 layers.row(0)=2;
 layers.row(1)=2;
 layers.row(2)=1;
  std::cout<<"neural layers\n"<<layers;
 neural_net->setLayerSizes(layers);
 TermCriteria Term(CV_TERMCRIT_ITER,2,1.0);
Term(CV_TERMCRIT_ITER,10000,0.1);
    neural_net->setTermCriteria(Term);
Mat trainig_data(4,1,CV_32FC1);

Ptr<TrainData>Train_data=TrainData::create();
    Mat trainig_data(4,2,CV_32FC1);
    Mat sampleDat(4,1,CV_32FC1);
Mat trainDat(4,2,CV_32FC1);
int count=0;
for(int r=0;r<trainDat.rows;r++)
{
    for(int c=0;c<trainDat.cols;c++)
    {
    trainDat.at<float>(r,0)=a[count];
    trainDat.at<float>(r,1)=b[count];
    }
    count+=1;

}
std::cout<<"\n"<<trainDat;
count=0;
for(int r=0;r<sampleDat.rows;r++)
{
    for(int c=0;c<sampleDat.cols;c++)
    {
        sampleDat.at<float>(r,c)=C[count];
    }
    count+=1;

}
std::cout<<"\n"<<sampleDat;

Ptr<TrainData>TrainingData=TrainData::create(trainDat,ROW_SAMPLE,sampleDat);
printf("training...");
neural_net->train(TrainingData);
printf("..done!");
Mat test(1,2,CV_32FC1);
test.at<float>(0,0)=1;
test.at<float>(0,1)=1;
printf("neural output %f",neural_net->predict(test));

    _getch();

This is the code for my neural net implementation... Actuallty my program is crashing in runtime..when I want to train this network for OR operation ..I am confused about creating its training data sets... how shall calling prediction function().It is encountering (memory access violation ) I move if I want to train for following datasets...

image description

assume that input A and B are fed to input layer neurons and expected output from output layer in C don't know why..??? Thank you..

training neural net..

I am using opencv 2.4 with visual studio 2012... here is the code ..

using namespace std;
using namespace cv;
using namespace ml;
int val=0;
float a[]={0,0,1,1};
float b[]={0,1,0,1};
float C[]={0,0,0,1};
    void main()
{
    Ptr<ANN_MLP>neural_net=ANN_MLP::create(); //empty neural model..
    neural_net->setActivationFunction(ANN_MLP::SIGMOID_SYM); //neural activation function...
    neural_net->setTrainMethod(ANN_MLP::BACKPROP);  ///training algorithm...
    Mat layers(3,1,CV_32FC1);
    layers.row(0)=2;
    layers.row(1)=2;
    layers.row(2)=1;
    std::cout<<"neural layers\n"<<layers;
    neural_net->setLayerSizes(layers);
    TermCriteria Term(CV_TERMCRIT_ITER,10000,0.1);
    neural_net->setTermCriteria(Term);
    Mat trainig_data(4,2,CV_32FC1);
    Mat sampleDat(4,1,CV_32FC1);
Mat trainDat(4,2,CV_32FC1);
int count=0;
for(int r=0;r<trainDat.rows;r++)
{
    for(int c=0;c<trainDat.cols;c++)
    {
    trainDat.at<float>(r,0)=a[count];
    trainDat.at<float>(r,1)=b[count];
    }
    count+=1;

}
std::cout<<"\n"<<trainDat;
count=0;
for(int r=0;r<sampleDat.rows;r++)
{
    for(int c=0;c<sampleDat.cols;c++)
    {
        sampleDat.at<float>(r,c)=C[count];
    }
    count+=1;

}
std::cout<<"\n"<<sampleDat;

Ptr<TrainData>TrainingData=TrainData::create(trainDat,ROW_SAMPLE,sampleDat);
printf("training...");
neural_net->train(TrainingData);
printf("..done!");
Mat test(1,2,CV_32FC1);
test.at<float>(0,0)=1;
test.at<float>(0,1)=1;
printf("neural output %f",neural_net->predict(test));

    _getch();

}

This is the code for my neural net implementation... Actuallty my program is crashing in runtime..when I am calling prediction function().It is encountering (memory access violation ) I don't know why..??? Thank you..