internal static extern void cveANN_MLPSetActivationFunction(IntPtr model, ANN_MLP.AnnMlpActivationFunction type, double param1, double param2);
public bool trainingMLP(Matrix <float> inputData, Matrix <float> outputData, string modelName, int iteration = 1000, double learningRate = 0.01, int hiddenLayers = 2, ANN_MLP.AnnMlpActivationFunction activationType = ANN_MLP.AnnMlpActivationFunction.SigmoidSym, double backpropWeightScale = 0.1, double backpropMomentumScale = 0.2) { try { layerSize = new Matrix <int>(new int[] { inputData.Cols, hiddenLayers, 1 });// Integer vector specifying the number of neurons in each layer including the input and output layers. The very first element specifies the number of elements in the input layer. The last element - number of elements in the output layer. IInputArray sample_in = inputData; IInputArray response = outputData; //=========================================================== using (ANN_MLP network = new ANN_MLP()) { network.SetActivationFunction(activationType); network.SetLayerSizes(layerSize); network.TermCriteria = new MCvTermCriteria(iteration, learningRate); // Number of Iteration for training network.SetTrainMethod(ANN_MLP.AnnMlpTrainMethod.Backprop); network.BackpropWeightScale = backpropWeightScale; network.BackpropMomentumScale = backpropMomentumScale; //network.Save("tmp.xml"); // Save temp weights to file for correction before training ActivationFunctionHardFix(network); // Fix min max values network.Read(new FileStorage("tmp.xml", FileStorage.Mode.Read).GetFirstTopLevelNode()); // Read Fixed values for training TrainData training = new TrainData(sample_in, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, response); // Creating training data network.Train(training); // Start Training network.Save(modelName + ".xml"); } return(true); } catch (Exception ee) { return(false); } }
/// <summary> /// Initialize the activation function for each neuron. /// </summary> /// <param name="function">Currently the default and the only fully supported activation function is SigmoidSym </param> /// <param name="param1">The first parameter of the activation function.</param> /// <param name="param2">The second parameter of the activation function.</param> public void SetActivationFunction(ANN_MLP.AnnMlpActivationFunction function, double param1 = 0, double param2 = 0) { MlInvoke.cveANN_MLPSetActivationFunction(_ptr, function, param1, param2); }
public Matrix <float> testingMLP(Matrix <float> testData, string modelName, int hiddenLayers = 2, ANN_MLP.AnnMlpActivationFunction activationType = ANN_MLP.AnnMlpActivationFunction.SigmoidSym) { Matrix <float> finalResult = null; layerSize = new Matrix <int>(new int[] { testData.Cols, hiddenLayers, 1 }); try { using (ANN_MLP network1 = new ANN_MLP()) // Testing trainned Data { network1.SetActivationFunction(activationType); network1.SetLayerSizes(layerSize); network1.Read(new FileStorage(modelName + ".xml", FileStorage.Mode.Read).GetFirstTopLevelNode()); // Load trainned ANN weights IInputArray Sample_test = testData; IOutputArray Result = new Matrix <float>(1, 1); network1.Predict(Sample_test, Result); //Start Network prediction finalResult = (Matrix <float>)Result; return(finalResult); } } catch (Exception ee) { return(finalResult); } }