gpt4 book ai didi

c++ - 神经网络对测试样本给出相同的结果

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:43 25 4
gpt4 key购买 nike

我使用 2.4.6 版本的 Opencv 库创建了一个带有 CvANN_MLP 类的神经网络。我的 Cv_ANN mlp 网络是:

 Mat trainingData(NUMERO_ESEMPI_TOTALE, 59, CV_32FC1);  
Mat trainingClasses(NUMERO_ESEMPI_TOTALE, 1, CV_32FC1);

for(int i=0;i<NUMERO_ESEMPI_TOTALE;i++){
for(int j=0;j<59;j++){
trainingData.at<float>(i,j) = featureVect[i][j];
}
}

for(int i=0;i<NUMERO_ESEMPI_TOTALE;i++){
trainingClasses.at<float>(i,0) = featureVect[i][59];
}

Mat testData (NUMERO_ESEMPI_TEST , 59, CV_32FC1);
Mat testClasses (NUMERO_ESEMPI_TEST , 1, CV_32FC1);
for(int i=0;i<NUMERO_ESEMPI_TEST;i++){
for(int j=0;j<59;j++){
testData.at<float>(i,j) = featureVectTest[i][j];
}
}

//0 bocca, 1 non bocca.
testClasses.at<float>(0,0) = 1;
testClasses.at<float>(1,0) = 0;
testClasses.at<float>(2,0) = 1;
testClasses.at<float>(3,0) = 1;
testClasses.at<float>(4,0) = 0;
testClasses.at<float>(5,0) = 1;
testClasses.at<float>(6,0) = 0;
testClasses.at<float>(7,0) = 1;
testClasses.at<float>(8,0) = 1;
testClasses.at<float>(9,0) = 0;
testClasses.at<float>(10,0) = 0;
testClasses.at<float>(11,0) = 1;
testClasses.at<float>(12,0) = 0;
testClasses.at<float>(13,0) = 0;
testClasses.at<float>(14,0) = 0;
testClasses.at<float>(15,0) = 0;
testClasses.at<float>(16,0) = 0;
testClasses.at<float>(17,0) = 0;
testClasses.at<float>(18,0) = 0;
testClasses.at<float>(19,0) = 1;
testClasses.at<float>(20,0) = 1;
testClasses.at<float>(21,0) = 0;
testClasses.at<float>(22,0) = 1;
testClasses.at<float>(23,0) = 0;
testClasses.at<float>(24,0) = 1;
testClasses.at<float>(25,0) = 0;
testClasses.at<float>(26,0) = 0;
testClasses.at<float>(27,0) = 1;
testClasses.at<float>(28,0) = 1;
testClasses.at<float>(29,0) = 1;

Mat layers = Mat(3, 1, CV_32SC1);
layers.row(0) = Scalar(59);
layers.row(1) = Scalar(3);
layers.row(2) = Scalar(1);

CvANN_MLP mlp;
CvANN_MLP_TrainParams params;
CvTermCriteria criteria;
criteria.max_iter = 100;
criteria.epsilon = 0.0000001;
criteria.type = CV_TERMCRIT_ITER | CV_TERMCRIT_EPS;
params.train_method = CvANN_MLP_TrainParams :: BACKPROP;
params.bp_dw_scale = 0.05 ;
params.bp_moment_scale = 0.05 ;
params.term_crit = criteria ;
mlp.create(layers);
// train
mlp.train(trainingData,trainingClasses,Mat(),Mat(),params);

Mat response(1, 1, CV_32FC1);
Mat predicted(testClasses.rows, 1, CV_32F);
Mat pred(NUMERO_ESEMPI_TEST, 1, CV_32FC1);
Mat pred1(NUMERO_ESEMPI_TEST, 1, CV_32FC1);
for(int i = 0; i < testData . rows ; i++){
Mat response(1, 1, CV_32FC1);
Mat sample = testData.row(i);
mlp.predict(sample,response);
predicted.at<float>(i ,0) = response.at <float>(0,0);
pred.at<float>(i,0)=predicted.at<float>(i ,0);
pred1.at<float>(i,0)=predicted.at<float>(i ,0);
file<<"Value Image "<<i<<": "<<predicted.at<float>(i ,0)<<"\n";
//cout<<"Value Image "<<i<<": "<<predicted.at<float>(i ,0)<<endl;
}

问题是这个网络为每个测试样本返回相同的结果。我不知道为什么。我的网络将一组具有 59 个输入值和 1 个输出值的特征向量作为输入。你能帮帮我吗?

最佳答案

我遇到了类似的问题。出现问题是因为 mlp 的创建函数的默认参数。它没有成功创建 sigmoid 函数,也无法在该条件下进行训练,您会得到相同的结果。所以解决方案是使用这样的 creat 函数:

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

我的问题在这里:OpenCV Neural Network Sigmoid Output

关于c++ - 神经网络对测试样本给出相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19187311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com