OpenCV中神经网络的层大小不起作用

时间:2013-05-30 03:35:44

标签: opencv machine-learning computer-vision neural-network regression

我正在尝试使用OpenCV的神经网络:

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
}

训练参数:

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
                    );
}

但隐藏图层的大小似乎根本不起作用,因为我将它从3改为30,结果根本没有改变

然后我改变了alpha和beta的值,但这也没有改变。

我的代码出了什么问题?

====我的训练和测试样本:====

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
...

1 个答案:

答案 0 :(得分:1)

你可能有一个大的epsilon导致学习分歧。 我假设你现在把它设置为0.1。 尝试将epsilon设置为更小的值,例如0.0000001,0.000001,0.00001。一个小的epsilon可能会给你很慢的收敛速度,但你应该至少看到进展。

顺便说一下,这是一个在opencv中使用svm和mlp的好教程。 https://raw.github.com/bytefish/opencv/master/machinelearning/doc/machinelearning.pdf