C# ENCOG SVM classification with my own dataset

只愿长相守 提交于 2019-12-12 06:38:26

问题


I would like to do a multiclass classification application in C#. I decided to use encog to do so. Now I am stuck at one point. I found a XOR example, which I understand. But when I am going to use my own dataset, app is computing only with one feature from one example. Here is my code:

 namespace ConsoleApplication1
 {

   public static class Load
   {
       public static double[][] FromFile(string path)
       {
        var rows = new List<double[]>();
        foreach (var line in File.ReadAllLines(path))
        {
            rows.Add(line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());
        }
        return rows.ToArray();
    }
}

 public class Program
 {

 static void Main( string [ ] args )
 {

 // LOADING MY OWN DATASET
 string cestain = @"E:\vstup.txt";
 double[][] innput = Load.FromFile(cestain);   //Training INPUTS

 string cestaout = @"E:\vystup.txt";
 double[][] ooutput = Load.FromFile(cestaout); //Desired OUTPUTS

 string cestatest = @"E:\te1.txt";
 double[][] teest = Load.FromFile(cestatest);   // Test Example


    // create a neural network 
    var svm = new SupportVectorMachine(10, false); // 2 input, & false for classification


    // create training data
    IMLDataSet trainingSet = new BasicMLDataSet(innput, ooutput);

    // train the neural network
    IMLTrain train = new SVMSearchTrain(svm, trainingSet);

    int epoch = 1;
    do
    {
        train.Iteration();
        Console.WriteLine(@"Epoch #" + epoch + @" Error:" + train.Error);
        epoch++;
    } while (train.Error > 0.01);

    // test the neural network

    Console.WriteLine(@"SVM Results:");
    foreach (IMLDataPair pair in trainingSet)
    {
        IMLData output = svm.Compute(pair.Input);
        Console.WriteLine(pair.Input[0]
                          + @", actual=" + output[0] + @",ideal=" + pair.Ideal[0]);
    }

    Console.WriteLine("Done");
    Console.ReadKey();
 }
 }
 } 

INPUTS looks like this (this is just a sample):

166 163 180 228

165 162 160 226

166 163 180 228

166 164 180 228

DESIRED OUTPUTS looks like this (this is just a sample):

1

2

1

1

TEST EXAMPLE looks like this:

152 151 98 219

When I run my app, it is computing the Error, but it shows only values from first column of my INPUTS (so I am not sure if it is computing with whole examples - 4 values). I am also not sure how to pass my TEST example to the SVM instead of that pair.Input.

Or is there a more efficient way to do this, than with encog? Thank you.


回答1:


If you are doing and xor operator, you only need a pair of numerals... but in your sample input, you have four, which I do not understand.

I do not know how to use SupportVectorMachine so I use this:

BasicNetwork network = new BasicNetwork(); 
network.AddLayer(new BasicLayer(2)); //input layer
network.AddLayer(new BasicLayer(2)); //hidden layer
network.AddLayer(new BasicLayer(1)); //ouput layer
network.Structure.FinalizeStructure(); 
network.Reset();

I wish this helps.



来源:https://stackoverflow.com/questions/21847695/c-sharp-encog-svm-classification-with-my-own-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!