I'm trying to implement Multi-Layer Perceptrons (MLP) neural networks using EmguCV 3.1 (a dot NET wrapper for OpenCV library) in C#(Windows Form). In order to practice with this library I decide to implement OR
operation using MLP.
I create MLP using "Initialize" method and learn it using "Train" method as below:
private void Initialize()
{
NETWORK.SetActivationFunction(
ANN_MLP.AnnMlpActivationFunction.SigmoidSym);
NETWORK.SetTrainMethod(ANN_MLP.AnnMlpTrainMethod.Backprop);
Matrix<double> layers = new Matrix<double>(new Size(4, 1));
layers[0, 0] = 2;
layers[0, 1] = 2;
layers[0, 2] = 2;
layers[0, 3] = 1;
NETWORK.SetLayerSizes(layers);
}
private void Train()
{
// providing data for input
Matrix<float> input = new Matrix<float>(4, 2);
input[0, 0] = MIN_ACTIVATION_FUNCTION; input[0, 1] = MIN_ACTIVATION_FUNCTION;
input[1, 0] = MIN_ACTIVATION_FUNCTION; input[1, 1] = MAX_ACTIVATION_FUNCTION;
input[2, 0] = MAX_ACTIVATION_FUNCTION; input[2, 1] = MIN_ACTIVATION_FUNCTION;
input[3, 0] = MAX_ACTIVATION_FUNCTION; input[3, 1] = MAX_ACTIVATION_FUNCTION;
//providing data for output
Matrix<float> output = new Matrix<float>(4, 1);
output[0, 0] = MIN_ACTIVATION_FUNCTION;
output[1, 0] = MAX_ACTIVATION_FUNCTION;
output[2, 0] = MAX_ACTIVATION_FUNCTION;
output[3, 0] = MAX_ACTIVATION_FUNCTION;
// mixing input and output for training
TrainData mixedData = new TrainData(
input,
Emgu.CV.ML.MlEnum.DataLayoutType.RowSample,
output);
// stop condition = 1 million iterations
NETWORK.TermCriteria = new MCvTermCriteria(1000000);
// training
NETWORK.Train(mixedData);
}
Where MIN_ACTIVATION_FUNCTION
, and MAX_ACTIVATION_FUNCTION
are equal to -1.7159 and 1.7159, respectively (according to OpenCV Documentation). After 1000000 iterations of (as you see in my code in stop condition), I test my network for prediction using Predict method as below:
private void Predict()
{
Matrix<float> input = new Matrix<float>(1, 2);
input[0, 0] = MIN_ACTIVATION_FUNCTION;
input[0, 1] = MIN_ACTIVATION_FUNCTION;
Matrix<float> output = new Matrix<float>(1, 1);
NETWORK.Predict(input, output);
MessageBox.Show(output[0, 0].ToString());
//////////////////////////////////////////////
input[0, 0] = MIN_ACTIVATION_FUNCTION;
input[0, 1] = MAX_ACTIVATION_FUNCTION;
NETWORK.Predict(input, output);
MessageBox.Show(output[0, 0].ToString());
//////////////////////////////////////////////
input[0, 0] = MAX_ACTIVATION_FUNCTION;
input[0, 1] = MIN_ACTIVATION_FUNCTION;
NETWORK.Predict(input, output);
MessageBox.Show(output[0, 0].ToString());
////////////////////////////////////////////////
input[0, 0] = MAX_ACTIVATION_FUNCTION;
input[0, 1] = MAX_ACTIVATION_FUNCTION;
NETWORK.Predict(input, output);
MessageBox.Show(output[0, 0].ToString());
}
Here is a sample of what NETWORK predicts:
-0.00734469
-0.03184918
0.02080269
-0.006674092
I expect be some thing like this:
-1.7
+1.7
+1.7
+1.7
What is wrong among my codes?
Note that I also use 0, 1 for MIN_ACTIVATION_FUNCTION
and MAX_ACTIVATION_FUNCTION
values but I still do not any good results.
Update 1:
I edit my codes as first answer refers me (even I test my code with idea referenced in comments). Now I get NaN
when call predict
method.