Does anyone have any idea of why this code produces incorrect classification labels
public class Classifier {
// This is an MLP classifier
// We will use an MLP with structure 2-2-1 to do the task of classifying XOR problem
// MLP Attributes
ANN_MLP Digits_Classifier;
Mat Layers; // A mat contains number of neurons in each layer starting from the input till the output
Mat Training_Samples; // Training data
Mat Training_Responses; // Training data labels
Mat Test_Samples; // Test data
Mat Test_Responses; // Test data labels that will be produced by MLP
int Row_Index , Col_Index;
float Label;
// class constructor just declares / defines attributes
public Classifier()
{
Digits_Classifier = ANN_MLP.create();
Digits_Classifier.setLayerSizes(Layers); // Layers is a 3x1 matrix.
Digits_Classifier.setActivationFunction(ANN_MLP.SIGMOID_SYM, 1.0, 1.0); // the = new Mat(3,1,CvType.CV_32FC1);
Training_Samples = new Mat(4,2,CvType.CV_32FC1);
Training_Responses = new Mat(4,1,CvType.CV_32FC1);
Test_Samples = new Mat(1,2,CvType.CV_32FC1);
Test_Responses = new Mat(1,1,CvType.CV_32FC1);
}
public void Start()
{
// Fill in Layers Mat
int Layers_Arr[] = new int[]{2,2,1};
for(int i=0 ; i<3 ; i++)
Layers.put(i,0,Layers_Arr[i]);
Row_Index = 0;
Col_Index = 0;
// Fill in Training_Samples Mat
float[] Training_Samples_Arr = new float[]{0,0,0,1,1,0,1,1};
for(int i=0 ; i<8 ; i++) {
Training_Samples.put(Row_Index,Col_Index,Training_Samples_Arr[i]);
Col_Index++;
if(Col_Index >= 2)
{
Row_Index++;
Col_Index = 0;
}
}
// Fill in Training_Responses Mat
float[] Training_Responses_Arr = new float[]{0,1,1,0};
for(int i=0 ; i<4 ; i++)
Training_Responses.put(i,0,Training_Responses_Arr[i]);
// Fill in Test_Samples Mat
double[] Test_Samples_Arr = new double[] {1.0,0.0};
for(int i=0 ; i<2 ; i++)
Test_Samples.put(0,i,Test_Samples_Arr[i]);
Digits_Classifier.setLayerSizes(Layers);
// The other two 2 parameters other than the type are Alpha and Beta consts.
Beta
Digits_Classifier.setActivationFunction(ANN_MLP.SIGMOID_SYM, 1.0, 1.0);
// Term_Criteria object is created with 3 parameters : type , Iterations_Num , Error_Limit
TermCriteria Digits_Classifier_Term_Criteria = new TermCriteria(TermCriteria.EPS,500,0.001);
Digits_Classifier.setTermCriteria(Digits_Classifier_Term_Criteria);
Digits_Classifier.setTrainMethod(ANN_MLP.BACKPROP,0.1,0.1); // the // the other 2 other parameters other than type are momentum and weight scales.
Digits_Classifier.train(Training_Samples,Ml.ROW_SAMPLE,Training_Responses); // scales
Digits_Classifier.setTrainMethod(ANN_MLP.BACKPROP,0.1,0.1);
Digits_Classifier.train(Training_Samples,Ml.ROW_SAMPLE,Training_Responses);
// I think this method is the source of problem I face.
Label = Digits_Classifier.predict(Test_Samples,Test_Responses,StatModel.RAW_OUTPUT);
}
}
so to summarize this is the function a 2-2-1 Neural net I use to perform XOR problem.
I trained the network by using train method just once
I then tried to predict what will the output be for test_sample such as 1 , 0 but the predict method returns 0 although it must be one and another thing is that trains MLP the first parameter I provided this example as a training sample
what is a 4x2 mat and the third one is a 4x1 mat and both of them are defined somewhere
Label = Digits_Classifier.predict(Test_Samples,Test_Responses,StatModel.UPDATE_MODEL);
// this predict function is to test MLP on inputs not seen before
// Test_sanples is an 1x2 mat and Test_Responses is an 1x1 mat.
// Label is a float variable that as I understand (but I am not sure) is the class label of this test sample
but here is another
the problem how it will act if I have many test samples.?
My problem is Label ( predict returned value ) is always 0 even if I test with 1,0 which included in training with output 1.