【发布时间】:2015-12-21 19:35:26
【问题描述】:
以下代码显示了我使用 OpenCV 3.0 使用虚假值训练和测试神经网络的最小示例:
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
int main()
{
using namespace std;
using namespace cv;
int inputLayerSize = 1;
int outputLayerSize = 1;
int numSamples = 2;
vector<int> layerSizes = { inputLayerSize, outputLayerSize };
Ptr<ml::ANN_MLP> nnPtr = ml::ANN_MLP::create();
nnPtr->setLayerSizes( layerSizes );
Mat samples( Size( inputLayerSize, numSamples ), CV_32F );
samples.at<float>( Point( 0, 0 ) ) = 0.1f;
samples.at<float>( Point( 0, 1 ) ) = 0.2f;
Mat responses( Size( outputLayerSize, numSamples ), CV_32F );
responses.at<float>( Point( 0, 0 ) ) = 0.2f;
responses.at<float>( Point( 0, 1 ) ) = 0.4f;
cout << "samples:\n" << samples << endl;
cout << "\nresponses:\n" << responses << endl;
if ( !nnPtr->train( samples, ml::ROW_SAMPLE, responses ) )
return 1;
cout << "\nweights[0]:\n" << nnPtr->getWeights( 0 ) << endl;
cout << "\nweights[1]:\n" << nnPtr->getWeights( 1 ) << endl;
cout << "\nweights[2]:\n" << nnPtr->getWeights( 2 ) << endl;
cout << "\nweights[3]:\n" << nnPtr->getWeights( 3 ) << endl;
Mat output;
nnPtr->predict( samples, output );
cout << "\noutput:\n" << output << endl;
}
但预测只返回 NaN 而不是实际值。这是输出:
samples:
[0.1;
0.2]
responses:
[0.2;
0.40000001]
weights[0]:
[19.99999970197678, -3]
weights[1]:
[0.05355758607590463;
0.01063728662926916]
weights[2]:
[inf, -nan(ind)]
weights[3]:
[0, 0]
output:
[-nan(ind);
-nan(ind)]
我做错了什么?
【问题讨论】:
标签: c++ opencv neural-network opencv3.0