Search code examples
c++opencvneural-networkopencv3.3

Does OpenCV initialize weights matrix with zeros, cv::ml::ANN_MLP


Its mentioned on OpenCV docs here

Artificial Neural Networks - Multi-Layer Perceptrons.

Unlike many other models in ML that are constructed and trained at once, in the MLP model these steps are separated. First, a network with the specified topology is created using the non-default constructor or the method ANN_MLP::create. All the weights are set to zeros. Then, the network is trained using a set of input and output vectors. The training procedure can be repeated more than once, that is, the weights can be adjusted based on the new training data.

And also it is mentioned:

UPDATE_WEIGHTS

Update the network weights, rather than compute them from scratch. In the latter case the weights are initialized using the Nguyen-Widrow algorithm.

So I wanted to know what happens exactly with the weight initialization when I go onto train a model. Answers related to OpenCV 3.3.1 are also appreciated


Solution

  • Do you have any reason to doubt documentation? OpenCV is open source library, so you can see what is beneath by yourself here

    ANN_MLPImpl()
    {
        clear();
        setActivationFunction( SIGMOID_SYM, 0, 0);
        setLayerSizes(Mat());
        setTrainMethod(ANN_MLP::RPROP, 0.1, FLT_EPSILON);
    }
    

    When you call train init_weights() may be called

    bool train( const Ptr<TrainData>& trainData, int flags )
    {
        // Some code
        // ... and link weights
        if( !(flags & UPDATE_WEIGHTS) )
            init_weights();
        // Even more code
    

    And here is init_weights()

    void init_weights()
    {
        //... More code
            // initialize weights using Nguyen-Widrow algorithm
            for( j = 0; j < n2; j++ )
            {
                double s = 0;
               // .. more initialization code