3

我正在尝试使用 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
...
4

1 回答 1

1

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

于 2013-06-10T08:38:22.130 回答