LibSVMsharp icon indicating copy to clipboard operation
LibSVMsharp copied to clipboard

Marshal.FreeHGlobal(x.weight); causes unknown error

Open MonsterMMORPG opened this issue 10 years ago • 4 comments

Hello. I am trying to use weights for each class. When i call

 double[] testResults = testSet.Predict(model);

The error happens inside SVMModel.Free(ptr_model);

    public static double[] Predict(this SVMProblem problem, SVMModel model)
    {
        IntPtr ptr_model = SVMModel.Allocate(model);
        double[] target = problem.X.Select(x => x.Predict(ptr_model)).ToArray();
        SVMModel.Free(ptr_model);
        return target;
    }

inside

  SVMModel.Free(x);

inside

SVMParameter.Free(x.param);

It causes error at this function

    public static void Free(svm_parameter x)
    {
        Marshal.FreeHGlobal(x.weight);
        x.weight = IntPtr.Zero;

        Marshal.FreeHGlobal(x.weight_label);
        x.weight_label = IntPtr.Zero;
    }

The error giving line is :

   Marshal.FreeHGlobal(x.weight);

Even in debug mode i don't get any particular error.

MonsterMMORPG avatar Oct 21 '15 10:10 MonsterMMORPG

Even wrapping them inside try catch causes error :(

The error does not happen if i don't use parameter.WeightLabels

MonsterMMORPG avatar Oct 21 '15 10:10 MonsterMMORPG

First, sorry for my late answer.

Could you explain how you use weights for each class? By using the properties Weights and WeightLabels in SVMParameter?

Please check the lines between 414 and 424 in this link, just to be sure.

ccerhan avatar Dec 17 '15 14:12 ccerhan

if you use Weights\ WeightLabels and model , you should reload model. like this is OK: SaveModel and LoadModel

parameter.WeightLabels = lableFeaturesBuilder.CreateWeightLables("行政湖景双床房").ToArray(); parameter.Weights = new double[] { 1.90, 1.90, 1.90, 1.99, 1.99 };

var model2 = LibSVMsharp.SVM.Train(problem, parameter); model2.SaveModel("roomMatching.model");

var model = LibSVMsharp.SVM.LoadModel("roomMatching.model"); foreach (var room in rooms1) { predictedY = LibSVMsharp.SVM.Predict(model, nodes);

you can see my sample:

https://github.com/zengfr/SVM-Neuro-Matching/blob/master/roomMatching/roomMatching/Program.cs

zengfr avatar Sep 05 '16 11:09 zengfr

To reproduce this issue just add weights to LibSVMsharp.Examples.Classification:

            // Select the parameter set
            SVMParameter parameter = new SVMParameter();
            parameter.Type = SVMType.C_SVC;
            parameter.Kernel = SVMKernelType.RBF;
            parameter.C = 1;
            parameter.Gamma = 1;
            parameter.WeightLabels = new[] { 1, 2, 3 };
            parameter.Weights = new[] { 1.0, 2.0, 3.0 };

I think there is an extra call of SVMModel.Free in SVMModel.Allocate Commenting this call resolves problem:

            // Allocate model.parameter
            IntPtr ptr_param = SVMParameter.Allocate(x.Parameter);
            y.param = (svm_parameter)Marshal.PtrToStructure(ptr_param, typeof(svm_parameter));
            // SVMParameter.Free(ptr_param);

slonoten avatar Sep 30 '16 14:09 slonoten