WoooHaaaa
WoooHaaaa

Reputation: 20450

Layer size of Neural network in OpenCV doesn't work

I'm trying to use OpenCV's Neural Network:

ANN::ANN() {
  // 4 rows and 1 col with the type of 32 bits short.
  CvMat* neural_layers = cvCreateMat(4, 1, CV_32SC1);
  cvSet1D(neural_layers, 0, cvScalar(2));   // inputs
  cvSet1D(neural_layers, 1, cvScalar(30));  // hidden
  cvSet1D(neural_layers, 2, cvScalar(30));  // hidden
  cvSet1D(neural_layers, 3, cvScalar(1));   //output
  // Init ANN with sigmoid function.
  this->network = new CvANN_MLP(neural_layers, 
                                CvANN_MLP::SIGMOID_SYM, // active function
                                1,    // alpha = 1
                                1);   // beta = 1
}

Training params:

void ANN::train() {
  // Prepare for sample matrix.
  CvANN_MLP_TrainParams params = CvANN_MLP_TrainParams();
  // cvTermCriteria( CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 1000, 0.01 );
  params.train_method = CvANN_MLP_TrainParams::BACKPROP;
  params.bp_dw_scale = 0.01;
  params.bp_moment_scale = 0.05;
  // Terminate condition.
  params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,
                                    100000,   // 
                                    0.1);
  // Start to train the network.
  this->network->train(
                    this->inputs,
                    this->outputs,
                    this->weights,
                    0,          // Samples start index.
                    params,     // Traning parameters.
                    1           // UPDATE_WEIGHTS
                    );
}

But the hidden layer's size seems not work at all since I changed it from 3 to 30, the result doesn't change at all.

Then I changed alpha and beta's value, but that also changes nothing.

What's wrong in my code ?

==== My Training and Test samples: ====

y = cos(x) + sin(x)

-0.758732841028 41.0938207976   27.2367595423
1.15370020129   21.1456884544   38.852465807
0.298333522748  37.4369795032   51.2449385711
1.8800004748    96.2375790658   44.2418473915
-1.78419644641  80.3189155018   77.9060673705
...

Upvotes: 3

Views: 3178

Answers (1)

greeness
greeness

Reputation: 16114

You might have a large epsilon which causes the learning to diverge. I assume you set it to 0.1 now. Try to set epsilon to a much smaller one, e.g., 0.0000001, 0.000001,0.00001. A small epsilon might give you slow convergence rate, however you should see the progress at least.

Btw, here is a great tutorial of using svm and mlp in opencv. https://raw.github.com/bytefish/opencv/master/machinelearning/doc/machinelearning.pdf

Upvotes: 2

Related Questions