神经网络mlp麻烦 [英] Neural network mlp trouble

查看:225
本文介绍了神经网络mlp麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2012和Opencv 2.4.6在C ++中编程。
我有一组训练图像,我已经计算了这些特征向量。这些特征向量应该成为我的神经网络的输入,用CvANN_MLP类实现。
每个特征向量由60个属性组成,59个是神经网络的输入,最后一个是输出,只能是1或0.
我实现了这个神经网络:

I am programming in C++ with Visual Studio 2012 and Opencv 2.4.6. I have a set of training images for which I have calculated the feature vectors. These feature vectors should become the input of my neural network, realized with the class CvANN_MLP. Every feature vector is composed of 60 attributes, 59 are the "inputs" of the neural network, and the last is the "output", that can be only 1 or 0. I have realized this neural network:

CvANN_MLP machineBrain;

double td[NUMERO_ESEMPI_TOTALE][60]; 

CvMat* trainData = cvCreateMat(NUMERO_ESEMPI_TOTALE, 59, CV_32FC1); 

CvMat* trainClasses = cvCreateMat(NUMERO_ESEMPI_TOTALE, 1, CV_32FC1); 

CvMat* sampleWts = cvCreateMat(NUMERO_ESEMPI_TOTALE, 1, CV_32FC1); 
//The matrix representation of our ANN. We'll have four layers.
CvMat* neuralLayers = cvCreateMat(4, 1, CV_32SC1);
CvMat trainData1, trainClasses1, neuralLayers1, sampleWts1;

cvGetRows(trainData, &trainData1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(trainClasses, &trainClasses1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(trainClasses, &trainClasses1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(sampleWts, &sampleWts1, 0, NUMERO_ESEMPI_TOTALE);
cvGetRows(neuralLayers, &neuralLayers1, 0, 4);



cvSet1D(&neuralLayers1, 0, cvScalar(59));
cvSet1D(&neuralLayers1, 1, cvScalar(3));
cvSet1D(&neuralLayers1, 2, cvScalar(3));
cvSet1D(&neuralLayers1, 3, cvScalar(1));



for(int i=0;i<NUMERO_ESEMPI_TOTALE;i++){
    for(int j=0;j<59;j++){
        td[i][j] = featureVect[i][j];
    }
    if(i<45){
        td[i][59] = 0; //è una bocca!
    }else{
        td[i][59] = 1; //non è una bocca!
    }
}

//Mettiamo insieme i training data
for (int i=0; i<NUMERO_ESEMPI_TOTALE; i++){
    //I 59 input 
    for(int j=0;j<59;j++){
        cvSetReal2D(&trainData1, i, 0, td[i][j]);
    }
    //Output
    cvSet1D(&trainClasses1, i, cvScalar(td[i][59]));
    //I pesi (vengono tutti settati a 1)
    cvSet1D(&sampleWts1, i, cvScalar(1));
}


machineBrain.create(neuralLayers);
cout<<"Rete creata"<<endl;

//Train it with our data.
machineBrain.train(trainData,trainClasses,sampleWts,0,CvANN_MLP_TrainParams(cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,100000,/*1.0*/0.01/*riprovare 0.01*/),CvANN_MLP_TrainParams::BACKPROP,0.001,0.05));
cout<<"Rete addestrata"<<endl;

 Mat pred(num_test_sample, 1, CV_32FC1);
Mat pred1(num_test_sample, 1, CV_32FC1);
for(int i=0;i<NUMERO_ESEMPI_TEST; i++){
    float _sample[59];
    CvMat sample = cvMat(1, 59, CV_32FC1, _sample);
    float _predout[1];
    CvMat predout = cvMat(1, 1, CV_32FC1, _predout);
    for(int j=0;j<59;j++){
        sample.data.fl[j] = featureVectTest[i][j];
    }
    machineBrain.predict(&sample, &predout);
    cout<<endl<<predout.data.fl[i]<<endl;//risultato predizione!
    pred.at<float>(i,0)=predout.data.fl[i];
    pred1.at<float>(i,0)=predout.data.fl[i];
    file<<"Value Image "<<i<<": "<<predout.data.fl[i]<<"\n";
}

返回的值为以下类型:

 Value Image 0: 0.475639
 Value Image 1: 0
 Value Image 2: 4.2039e-044
 Value Image 3: 1.4013e-045
 Value Image 4: -7.88636e-016
 Value Image 5: 1.31722e-043
 Value Image 6: 4.2039e-044
 Value Image 7: 1.4013e-045
 Value Image 8: 0.0154511
 Value Image 9: 0.00100189
 Value Image 10: 0.00161414
 Value Image 11: 0.0449422
 Value Image 12: 7.5433
 Value Image 13: 65.8052
 Value Image 14: 24.301
 Value Image 15: 19.7311
 Value Image 16: 0.985553
 Value Image 17: 0.965309
 Value Image 18: 0.971295

所以我没有0或1的结果。如果没有,我的代码是什么错误?

So I haven't results of 0 or 1. Is it correct? If not, what is the mistake in my code?

推荐答案

我不知道有关C接口,但我有一个类似的问题在这里使用c ++介面:

I dont know much about about C interface but I had a similar problem with c++ interface here :

OpenCV神经网络Sigmoid输出

Mlp的创建函数默认参数不起作用,它们应该作为:

Mlp's create function default parameters was not working and they should be given as :

mlp.create(layers,CvANN_MLP :: SIGMOID_SYM,1,1)

mlp.create(layers, CvANN_MLP::SIGMOID_SYM, 1, 1)

但是正如我所说,知道c接口。因为C没有默认参数,所以可能有另一个函数来给出sigmoid的alpha和beta参数。

But as i told, I dont know about c interface. Since C doesnt have default arguments, there maybe another function to give alpha and beta parameters of sigmoid.

顺便说一下,OpenCV没有众所周知的sigmoid实现在0和1的范围内。如文档中所述,它在-1和1之间。

And by the way, OpenCV does not have well known implementation of sigmoid which in range of 0 and 1. As it is stated in the docs, it is between -1 and 1.

这篇关于神经网络mlp麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆