神经网络在OpenCV 3.0中只返回nan

Neural Network only returns NaNs in OpenCV 3.0

本文关键字:返回 nan OpenCV 神经网络      更新时间:2023-10-16

下面的代码显示了我用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)]

我做错了什么?

好的,解决了。激活函数需要显式设置。因此,在调用setLayerSizes之后的下面一行,问题就解决了:

nnPtr->setActivationFunction( cv::ml::ANN_MLP::SIGMOID_SYM );
输出:

samples:
[0.1;
 0.2]
responses:
[0.2;
 0.40000001]
weights[0]:
[19.99999970197678, -3]
weights[1]:
[1.811227207835904;
 -0.0006127133707308392]
weights[2]:
[0.1052631594632801, 0.3000000044703484]
weights[3]:
[9.49999985843897, -2.85]
output:
[0.20249137;
 0.39745635]