public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { svm_model model = value as svm_model; SVMModel smodel = new SVMModel(model); serializer.Serialize(writer, smodel); }
public svm_model initial(List <Dictionary <string, double> > docWordDicList, Dictionary <string, int> dictionary, List <int> trainingAnswer, double c, double gamma) { Console.WriteLine("==> Starting training..."); svm_problem prob = gen_svm_training_data(docWordDicList, dictionary, trainingAnswer); svm_parameter param = new svm_parameter(); // set the default setting value param.svm_type = svm_parameter.C_SVC; param.kernel_type = svm_parameter.RBF; param.degree = 3; param.gamma = gamma; param.coef0 = 0; param.nu = 0.5; param.cache_size = 100; param.C = c; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; param.nr_weight = 0; svm_model model = svm.svm_train(prob, param); svm.svm_save_model(SVM_MODEL_FILE_NAME, model); Console.WriteLine("==> Training done!!"); return(model); }
SVMClassifier(Dictionary <string, int> identifiersMap, svm_model model) { /* for serialization */ svmNetwork = new ProbabilitySupportVectorMachine(model); this.identifiersMap = identifiersMap; this.model = model; }
public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable <string> allowedInputVariables) : base(targetVariable) { this.name = ItemName; this.description = ItemDescription; this.model = model; this.rangeTransform = rangeTransform; this.allowedInputVariables = allowedInputVariables.ToArray(); }
protected void Train(double C, double gamma, List <ISample> trainingSamples, string[] trainingIdentifiers) { // generate a numeric mapping of our string identifiers to unique numeric values identifiersMap = trainingIdentifiers.Select((identifier, index) => new { identifier, index }).ToDictionary(id => id.identifier, id => id.index); // don't include imposter samples svmNetwork = TrainSVM(C, gamma, trainingSamples, (sample) => identifiersMap[sample.GetIdentifier()]); model = svmNetwork.Model; }
public static SVMModel Convert(IntPtr ptr) { if (ptr == IntPtr.Zero) { return(null); } svm_model x = (svm_model)Marshal.PtrToStructure(ptr, typeof(svm_model)); return(SVMModel.Convert(x)); }
private SupportVectorMachineModel(SupportVectorMachineModel original, Cloner cloner) : base(original, cloner) { // only using a shallow copy here! (gkronber) this.model = original.model; this.rangeTransform = original.rangeTransform; this.allowedInputVariables = (string[])original.allowedInputVariables.Clone(); if (original.classValues != null) { this.classValues = (double[])original.classValues.Clone(); } }
public static void Main(System.String[] argv) { int i, predict_probability = 0; // parse options for (i = 0; i < argv.Length; i++) { if (argv[i][0] != '-') { break; } ++i; switch (argv[i - 1][1]) { case 'b': predict_probability = atoi(argv[i]); break; default: System.Console.Error.Write("unknown option\n"); exit_with_help(); break; } } if (i >= argv.Length) { exit_with_help(); } try { //UPGRADE_TODO: Expected value of parameters of constructor 'java.io.BufferedReader.BufferedReader' are different in the equivalent in .NET. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1092"' System.IO.StreamReader input = new System.IO.StreamReader(argv[i]); // Input file System.IO.BinaryWriter output = new System.IO.BinaryWriter(new System.IO.FileStream(argv[i + 2], System.IO.FileMode.Create)); // Output file TextReader reader = File.OpenText(argv[i + 1]); reader.ReadToEnd(); System.Text.UnicodeEncoding unicodeEncoding = new System.Text.UnicodeEncoding(); byte[] sBytes = unicodeEncoding.GetBytes(reader.ToString()); svm_model model = svm.svm_load_model(sBytes); // Model file predict(input, output, model, predict_probability); } catch (System.IO.FileNotFoundException e) { string message = e.Message; exit_with_help(); } catch (System.IndexOutOfRangeException e) { string message = e.Message; exit_with_help(); } }
public static void Free(svm_model x) { Marshal.FreeHGlobal(x.rho); x.rho = IntPtr.Zero; Marshal.FreeHGlobal(x.probA); x.probA = IntPtr.Zero; Marshal.FreeHGlobal(x.probB); x.probB = IntPtr.Zero; Marshal.FreeHGlobal(x.sv_indices); x.sv_indices = IntPtr.Zero; Marshal.FreeHGlobal(x.label); x.label = IntPtr.Zero; Marshal.FreeHGlobal(x.nSV); x.nSV = IntPtr.Zero; SVMParameter.Free(x.param); IntPtr i_ptr_sv = x.SV; for (int i = 0; i < x.l; i++) { IntPtr ptr_nodes = (IntPtr)Marshal.PtrToStructure(i_ptr_sv, typeof(IntPtr)); SVMNode.Free(ptr_nodes); i_ptr_sv = IntPtr.Add(i_ptr_sv, Marshal.SizeOf(typeof(IntPtr))); } Marshal.FreeHGlobal(x.SV); x.SV = IntPtr.Zero; IntPtr i_ptr_svcoef = x.sv_coef; for (int i = 0; i < x.nr_class - 1; i++) { IntPtr temp = (IntPtr)Marshal.PtrToStructure(i_ptr_svcoef, typeof(IntPtr)); Marshal.FreeHGlobal(temp); temp = IntPtr.Zero; i_ptr_svcoef = IntPtr.Add(i_ptr_svcoef, Marshal.SizeOf(typeof(IntPtr))); } Marshal.FreeHGlobal(x.sv_coef); x.sv_coef = IntPtr.Zero; }
public static void Free(IntPtr ptr) { if (ptr == IntPtr.Zero) { return; } svm_model x = (svm_model)Marshal.PtrToStructure(ptr, typeof(svm_model)); SVMModel.Free(x); Marshal.DestroyStructure(ptr, typeof(svm_model)); Marshal.FreeHGlobal(ptr); ptr = IntPtr.Zero; }
/// <summary> /// Construct a SVM from a model. /// </summary> /// /// <param name="theModel">The model.</param> public SupportVectorMachine(svm_model theModel) { _model = theModel; _paras = _model.param; _inputCount = 0; // determine the input count foreach (var element in _model.SV) { foreach (svm_node t in element) { _inputCount = Math.Max(t.index, _inputCount); } } // }
public MaltLibsvmModel(svm_model model, svm_problem problem) { param = model.param; nr_class = model.nr_class; l = model.l; SV = model.SV; sv_coef = model.sv_coef; rho = model.rho; label = model.label; nSV = model.nSV; start = new int[nr_class]; start[0] = 0; for (int i = 1; i < nr_class; i++) { start[i] = start[i - 1] + nSV[i - 1]; } }
public bool LoadFromFile(string fileName) { if (File.Exists(fileName)) { FileStream fs = new FileStream(fileName, FileMode.Open); using (BinaryReader r = new BinaryReader(fs)) { this.model = new svm_model(); svm_parameter p = new svm_parameter(); p.C = r.ReadDouble(); p.cache_size = r.ReadDouble(); p.coef0 = r.ReadDouble(); p.degree = r.ReadDouble(); p.eps = r.ReadDouble(); p.gamma = r.ReadDouble(); p.kernel_type = r.ReadInt32(); p.nr_weight = r.ReadInt32(); p.nu = r.ReadDouble(); p.p = r.ReadDouble(); p.probability = r.ReadInt32(); p.shrinking = r.ReadInt32(); p.svm_type = r.ReadInt32(); p.weight = ReadDoubleArray(r); p.weight_label = ReadIntArray(r); this.model.param = p; this.model.nr_class = r.ReadInt32(); this.model.l = r.ReadInt32(); this.model.SV = ReadSvmNodeArray(r); this.model.sv_coef = ReadDouble2DArray(r); this.model.rho = ReadDoubleArray(r); this.model.probA = ReadDoubleArray(r); this.model.probB = ReadDoubleArray(r); this.model.label = ReadIntArray(r); this.model.nSV = ReadIntArray(r); return(true); } } this.model = null; return(false); }
static void Main(string[] args) { List <Dictionary <string, double> > docWordDicList = new List <Dictionary <string, double> >(); Dictionary <string, int> dictionary = new Dictionary <string, int>(); List <int> trainingAnswer = new List <int>(); Dictionary <string, double> wordIDFDictionary = new Dictionary <string, double>(); Hashtable stopWordTable = genStopwordTable(STOP_WORD_PATH); List <string> testFileNameList = new List <string>(); int dicSize = 100; Console.WriteLine("==> Starting prepare data..."); NLPAdapter nlpAdapter = new NLPAdapter(NLP_MODEL_PATH); trainModel(TRAINING_DATA_DIR, LOG_DIR, ref docWordDicList, ref dictionary, dicSize, ref trainingAnswer, ref wordIDFDictionary, stopWordTable, nlpAdapter ); #if KNN_MODE KNN knn = new KNN(); knn.set(dicSize, docWordDicList.Count()); knn.initial(docWordDicList, dictionary, trainingAnswer); knn.train(3, 20); knn.getAveDistance(); //knn.genLog(@"D:\work\KPMG\learning\classification\project1_0422\log"); List <KeyValuePair <int, int> > testAnswer = runKnnTest(knn, TEST_DATA_DIR, TEST_LOG_DIR, dictionary, wordIDFDictionary, stopWordTable, ref testFileNameList, nlpAdapter); #else Console.WriteLine("==> Starting get model..."); SVMAdapter svmAdapter = new SVMAdapter(); svm_model model = svmAdapter.getSVMModel(docWordDicList, dictionary, trainingAnswer, SVMAdapter.SVM_C_DEFAULT, SVMAdapter.SVM_GAMMA_DEFAULT); Console.WriteLine("==> Starting SVM test..."); List <KeyValuePair <int, int> > testAnswer = runSVMTest(svmAdapter, TEST_DATA_DIR, TEST_LOG_DIR, dictionary, wordIDFDictionary, stopWordTable, ref testFileNameList, model, nlpAdapter); Console.WriteLine("==> Starting SVM test done!!"); #endif Console.WriteLine("==> Starting saving result..."); genStatistic(testAnswer, testFileNameList, LOG_DIR); }
public void TrainModel(double[] labels, double[][] mlArray) { SvmProblemBuilder builder = new SvmProblemBuilder(labels, mlArray); svm_problem problem = builder.CreateProblem(); svm_parameter param = new svm_parameter() { svm_type = 0, kernel_type = 0, cache_size = 512, eps = 0.1, C = 10, nr_weight = 0, weight_label = null, weight = null }; this.model = svm.svm_train(problem, param); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { SVMModel dmodel = serializer.Deserialize <SVMModel>(reader); svm_model model = new svm_model() { SV = dmodel.SV }; model.SV = dmodel.SV; DataHelpers.SetFieldValue <int>(model, "l", dmodel.l); DataHelpers.SetFieldValue <int[]>(model, "label", dmodel.label); DataHelpers.SetFieldValue <int>(model, "nr_class", dmodel.nr_class); DataHelpers.SetFieldValue <int[]>(model, "nSV", dmodel.nSV); DataHelpers.SetFieldValue <double[]>(model, "probA", dmodel.probA); DataHelpers.SetFieldValue <double[]>(model, "probB", dmodel.probB); DataHelpers.SetFieldValue <double[]>(model, "rho", dmodel.rho); DataHelpers.SetFieldValue <double[][]>(model, "sv_coef", dmodel.sv_coef); DataHelpers.SetFieldValue <svm_parameter>(model, "param", dmodel.param); return(model); }
/// <summary> /// Load the models. /// </summary> /// <param name="xmlin">Where to read the models from.</param> /// <param name="network">Where the models are read into.</param> private void HandleModels(ReadXML xmlin, SVMNetwork network) { int index = 0; while (xmlin.ReadToTag()) { if (xmlin.IsIt(SVMNetworkPersistor.TAG_MODEL, true)) { svm_parameter param = new svm_parameter(); svm_model model = new svm_model(); model.param = param; network.Models[index] = model; HandleModel(xmlin, network.Models[index]); index++; } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_MODELS, false)) { break; } } }
private void run(System.String[] argv) { parse_command_line(argv); read_problem(); error_msg = svm.svm_check_parameter(prob, param); if ((System.Object)error_msg != null) { System.Console.Error.Write("Error: " + error_msg + "\n"); System.Environment.Exit(1); } if (cross_validation != 0) { do_cross_validation(); } else { model = svm.svm_train(prob, param); svm.svm_save_model(model_file_name, model); } }
/// <summary> /// Load the data from a model. /// </summary> /// <param name="xmlin">Where to read the data from.</param> /// <param name="model">The model to load data into.</param> private void HandleData(ReadXML xmlin, svm_model model) { int i = 0; int m = model.nr_class - 1; int l = model.l; model.sv_coef = EngineArray.AllocateDouble2D(m, l); model.SV = new svm_node[l][]; while (xmlin.ReadToTag()) { if (xmlin.IsIt(SVMNetworkPersistor.TAG_ROW, true)) { String line = xmlin.ReadTextToTag(); String[] st = xmlin.ReadTextToTag().Split(','); for (int k = 0; k < m; k++) { model.sv_coef[k][i] = Double.Parse(st[i]); } int n = st.Length / 2; model.SV[i] = new svm_node[n]; int idx = 0; for (int j = 0; j < n; j++) { model.SV[i][j] = new svm_node(); model.SV[i][j].index = int.Parse(st[idx++]); model.SV[i][j].value_Renamed = Double.Parse(st[idx++]); } i++; } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_DATA, false)) { break; } } }
public static IntPtr Allocate(SVMModel x) { if (x == null || x.ClassCount < 1 || x.Labels == null || x.Labels.Length < 1 || x.Parameter == null || x.Rho == null || x.Rho.Length < 1 || x.SVCoefs == null || x.SVCoefs.Count < 1 || x.TotalSVCount < 1 || x.SVCounts == null || x.SVCounts.Length < 1) { return(IntPtr.Zero); } svm_model y = new svm_model(); y.nr_class = x.ClassCount; y.l = x.TotalSVCount; y.free_sv = (int)x.Creation; // Allocate model.parameter IntPtr ptr_param = SVMParameter.Allocate(x.Parameter); y.param = (svm_parameter)Marshal.PtrToStructure(ptr_param, typeof(svm_parameter)); // Allocate model.rho y.rho = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.Rho.Length); Marshal.Copy(x.Rho, 0, y.rho, x.Rho.Length); // Allocate model.probA y.probA = IntPtr.Zero; if (x.ProbabilityA != null) { y.probA = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.ProbabilityA.Length); Marshal.Copy(x.ProbabilityA, 0, y.probA, x.ProbabilityA.Length); } // Allocate model.probB y.probB = IntPtr.Zero; if (x.ProbabilityB != null) { y.probB = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.ProbabilityB.Length); Marshal.Copy(x.ProbabilityB, 0, y.probB, x.ProbabilityB.Length); } // Allocate model.label y.label = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * x.Labels.Length); Marshal.Copy(x.Labels, 0, y.label, x.Labels.Length); // Allocate model.nSV y.nSV = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * x.SVCounts.Length); Marshal.Copy(x.SVCounts, 0, y.nSV, x.SVCounts.Length); // Allocate model.sv_coef y.sv_coef = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * x.SVCoefs.Count); IntPtr i_ptr_svcoef = y.sv_coef; for (int i = 0; i < x.SVCoefs.Count; i++) { IntPtr temp = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.SVCoefs[i].Length); Marshal.Copy(x.SVCoefs[i], 0, temp, x.SVCoefs[i].Length); Marshal.StructureToPtr(temp, i_ptr_svcoef, true); i_ptr_svcoef = IntPtr.Add(i_ptr_svcoef, Marshal.SizeOf(typeof(IntPtr))); } // Allocate model.sv_indices y.sv_indices = IntPtr.Zero; if (x.SVIndices != null) { y.sv_indices = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * x.SVIndices.Length); Marshal.Copy(x.SVIndices, 0, y.sv_indices, x.SVIndices.Length); } // Allocate model.SV y.SV = IntPtr.Zero; if (x.SV != null) { y.SV = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * x.SV.Count); IntPtr i_ptr_sv = y.SV; for (int i = 0; i < x.SV.Count; i++) { // Prepare each node array // 1) All nodes containing zero value is removed // 2) A node which index is -1 is added to the end List <SVMNode> temp = x.SV[i].Where(a => a.Value != 0).ToList(); temp.Add(new SVMNode(-1, 0)); SVMNode[] nodes = temp.ToArray(); // Allocate node array IntPtr ptr_nodes = SVMNode.Allocate(nodes); Marshal.StructureToPtr(ptr_nodes, i_ptr_sv, true); i_ptr_sv = IntPtr.Add(i_ptr_sv, Marshal.SizeOf(typeof(IntPtr))); } } // Allocate the model int size = Marshal.SizeOf(y); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(y, ptr, true); return(ptr); }
/// <summary> /// Handle a model. /// </summary> /// <param name="xmlin">Where to read the model from.</param> /// <param name="model">Where to load the model into.</param> private void HandleModel(ReadXML xmlin, svm_model model) { while (xmlin.ReadToTag()) { if (xmlin.IsIt(SVMNetworkPersistor.TAG_TYPE_SVM, true)) { int i = EngineArray.FindStringInArray( SVMNetworkPersistor.svm_type_table, xmlin.ReadTextToTag()); model.param.svm_type = i; } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_DEGREE, true)) { model.param.degree = int.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_GAMMA, true)) { model.param.gamma = double.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_COEF0, true)) { model.param.coef0 = double.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_NUMCLASS, true)) { model.nr_class = int.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_TOTALSV, true)) { model.l = int.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_RHO, true)) { int n = model.nr_class * (model.nr_class - 1) / 2; model.rho = new double[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) { model.rho[i] = double.Parse(st[i]); } } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_LABEL, true)) { int n = model.nr_class; model.label = new int[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) { model.label[i] = int.Parse(st[i]); } } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_PROB_A, true)) { int n = model.nr_class * (model.nr_class - 1) / 2; model.probA = new double[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) { model.probA[i] = Double.Parse(st[i]); } } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_PROB_B, true)) { int n = model.nr_class * (model.nr_class - 1) / 2; model.probB = new double[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) { model.probB[i] = Double.Parse(st[i]); } } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_NSV, true)) { int n = model.nr_class; model.nSV = new int[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) { model.nSV[i] = int.Parse(st[i]); } } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_TYPE_KERNEL, true)) { int i = EngineArray.FindStringInArray( SVMNetworkPersistor.kernel_type_table, xmlin .ReadTextToTag()); model.param.kernel_type = i; } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_DATA, true)) { HandleData(xmlin, model); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_MODEL, false)) { break; } } }
private static List <KeyValuePair <int, int> > runSVMTest(SVMAdapter svmAdapter, string testPath, string logPath, Dictionary <string, int> dictionary, Dictionary <string, double> wordIDFDictionary, Hashtable stopWordTable, ref List <string> testFileNameList, svm_model model, NLPAdapter nlpAdapter) { string[] categories = Directory.GetDirectories(testPath); List <KeyValuePair <int, int> > testAnswer = new List <KeyValuePair <int, int> >(); for (int i = 0; i < categories.Length; i++) //traverse Categories { Console.WriteLine(categories[i]); string[] files = Directory.GetFiles(categories[i]); for (int j = 0; j < files.Length; j++) { int testResult = -1; testFileNameList.Add(Path.GetFileName(files[j])); testResult = svmAdapter.runSVMTest(readDoc(files[j], stopWordTable, nlpAdapter), dictionary, wordIDFDictionary, model); testAnswer.Add(new KeyValuePair <int, int>(testResult, i)); Console.WriteLine(testResult + "," + i); } } return(testAnswer); }
public static SVMModel Convert(svm_model x) { SVMModel y = new SVMModel(); y.Creation = (CreationType)x.free_sv; y.ClassCount = x.nr_class; y.TotalSVCount = x.l; if (y.Creation == CreationType.LOAD_MODEL) { y.Parameter = new SVMParameter(); y.Parameter.Type = (SVMType)x.param.svm_type; y.Parameter.Kernel = (SVMKernelType)x.param.kernel_type; switch (y.Parameter.Kernel) { case SVMKernelType.LINEAR: break; case SVMKernelType.POLY: y.Parameter.Gamma = x.param.gamma; y.Parameter.Coef0 = x.param.coef0; y.Parameter.Degree = x.param.degree; break; case SVMKernelType.RBF: y.Parameter.Gamma = x.param.gamma; break; case SVMKernelType.SIGMOID: y.Parameter.Gamma = x.param.gamma; y.Parameter.Coef0 = x.param.coef0; break; } } else { y.Parameter = SVMParameter.Convert(x.param); } int problemCount = (int)(y.ClassCount * (y.ClassCount - 1) * 0.5); y.Rho = new double[problemCount]; Marshal.Copy(x.rho, y.Rho, 0, y.Rho.Length); y.ProbabilityA = null; if (x.probA != IntPtr.Zero) { y.ProbabilityA = new double[problemCount]; Marshal.Copy(x.probA, y.ProbabilityA, 0, y.ProbabilityA.Length); } y.ProbabilityB = null; if (x.probB != IntPtr.Zero) { y.ProbabilityB = new double[problemCount]; Marshal.Copy(x.probB, y.ProbabilityB, 0, y.ProbabilityB.Length); } y.SVCounts = new int[y.ClassCount]; Marshal.Copy(x.nSV, y.SVCounts, 0, y.SVCounts.Length); y.Labels = new int[y.ClassCount]; Marshal.Copy(x.label, y.Labels, 0, y.Labels.Length); y.SVCoefs = new List <double[]>(y.ClassCount - 1); IntPtr i_ptr_svcoef = x.sv_coef; for (int i = 0; i < y.ClassCount - 1; i++) { y.SVCoefs.Add(new double[y.TotalSVCount]); IntPtr coef_ptr = (IntPtr)Marshal.PtrToStructure(i_ptr_svcoef, typeof(IntPtr)); Marshal.Copy(coef_ptr, y.SVCoefs[i], 0, y.SVCoefs[i].Length); i_ptr_svcoef = IntPtr.Add(i_ptr_svcoef, Marshal.SizeOf(typeof(IntPtr))); } y.SVIndices = null; if (x.sv_indices != IntPtr.Zero) { y.SVIndices = new int[y.TotalSVCount]; Marshal.Copy(x.sv_indices, y.SVIndices, 0, y.SVIndices.Length); } y.SV = new List <SVMNode[]>(); IntPtr i_ptr_sv = x.SV; for (int i = 0; i < x.l; i++) { IntPtr ptr_nodes = (IntPtr)Marshal.PtrToStructure(i_ptr_sv, typeof(IntPtr)); SVMNode[] nodes = SVMNode.Convert(ptr_nodes); y.SV.Add(nodes); i_ptr_sv = IntPtr.Add(i_ptr_sv, Marshal.SizeOf(typeof(IntPtr))); } return(y); }
private static void predict(System.IO.StreamReader input, System.IO.BinaryWriter output, svm_model model, int predict_probability) { int correct = 0; int total = 0; double error = 0; double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; int svm_type = svm.svm_get_svm_type(model); int nr_class = svm.svm_get_nr_class(model); int[] labels = new int[nr_class]; double[] prob_estimates = null; if (predict_probability == 1) { if (svm_type == svm_parameter.EPSILON_SVR || svm_type == svm_parameter.NU_SVR) { System.Console.Out.Write("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=" + svm.svm_get_svr_probability(model) + "\n"); } else { svm.svm_get_labels(model, labels); prob_estimates = new double[nr_class]; //UPGRADE_ISSUE: Method 'java.io.DataOutputStream.Write' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaioDataOutputStreamWrite_javalangString"' output.Write("labels"); for (int j = 0; j < nr_class; j++) { //UPGRADE_ISSUE: Method 'java.io.DataOutputStream.Write' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaioDataOutputStreamWrite_javalangString"' output.Write(" " + labels[j]); } //UPGRADE_ISSUE: Method 'java.io.DataOutputStream.Write' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaioDataOutputStreamWrite_javalangString"' output.Write("\n"); } } while (true) { System.String line = input.ReadLine(); if ((System.Object) line == null) break; SupportClass.Tokenizer st = new SupportClass.Tokenizer(line, " \t\n\r\f:"); double target = atof(st.NextToken()); int m = st.Count / 2; svm_node[] x = new svm_node[m]; for (int j = 0; j < m; j++) { x[j] = new svm_node(); x[j].index = atoi(st.NextToken()); x[j].value = atof(st.NextToken()); } double v; if (predict_probability == 1 && (svm_type == svm_parameter.C_SVC || svm_type == svm_parameter.NU_SVC)) { v = svm.svm_predict_probability(model, x, prob_estimates); //UPGRADE_ISSUE: Method 'java.io.DataOutputStream.Write' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaioDataOutputStreamWrite_javalangString"' output.Write(v + " "); for (int j = 0; j < nr_class; j++) { //UPGRADE_ISSUE: Method 'java.io.DataOutputStream.Write' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaioDataOutputStreamWrite_javalangString"' output.Write(prob_estimates[j] + " "); } //UPGRADE_ISSUE: Method 'java.io.DataOutputStream.Write' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaioDataOutputStreamWrite_javalangString"' output.Write("\n"); } else { v = svm.svm_predict(model, x); //UPGRADE_ISSUE: Method 'java.io.DataOutputStream.Write' was not converted. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1000_javaioDataOutputStreamWrite_javalangString"' output.Write(v + "\n"); } if (v == target) ++correct; error += (v - target) * (v - target); sumv += v; sumy += target; sumvv += v * v; sumyy += target * target; sumvy += v * target; ++total; } System.Console.Out.Write("Accuracy = " + (double) correct / total * 100 + "% (" + correct + "/" + total + ") (classification)\n"); System.Console.Out.Write("Mean squared error = " + error / total + " (regression)\n"); System.Console.Out.Write("Squared correlation coefficient = " + ((total * sumvy - sumv * sumy) * (total * sumvy - sumv * sumy)) / ((total * sumvv - sumv * sumv) * (total * sumyy - sumy * sumy)) + " (regression)\n"); }
/// <inheritdoc/> public Object Read(Stream mask0) { var result = new SupportVectorMachine(); var ins0 = new EncogReadHelper(mask0); EncogFileSection section; while ((section = ins0.ReadNextSection()) != null) { if (section.SectionName.Equals("SVM") && section.SubSectionName.Equals("PARAMS")) { IDictionary <String, String> paras = section.ParseParams(); EngineArray.PutAll(paras, result.Properties); } if (section.SectionName.Equals("SVM") && section.SubSectionName.Equals("SVM-PARAM")) { IDictionary <String, String> p = section.ParseParams(); result.InputCount = EncogFileSection.ParseInt(p, PersistConst.InputCount); result.Params.C = EncogFileSection.ParseDouble(p, ParamC); result.Params.cache_size = EncogFileSection.ParseDouble( p, ParamCacheSize); result.Params.coef0 = EncogFileSection.ParseDouble(p, ParamCoef0); result.Params.degree = EncogFileSection.ParseDouble(p, ParamDegree); result.Params.eps = EncogFileSection.ParseDouble(p, ParamEps); result.Params.gamma = EncogFileSection.ParseDouble(p, ParamGamma); result.Params.kernel_type = EncogFileSection.ParseInt( p, ParamKernelType); result.Params.nr_weight = EncogFileSection.ParseInt( p, ParamNumWeight); result.Params.nu = EncogFileSection.ParseDouble(p, ParamNu); result.Params.p = EncogFileSection.ParseDouble(p, ParamP); result.Params.probability = EncogFileSection.ParseInt( p, ParamProbability); result.Params.shrinking = EncogFileSection.ParseInt( p, ParamShrinking); /*result.Params.statIterations = Encog.Persist.EncogFileSection.ParseInt( * params_0, PersistSVM.PARAM_START_ITERATIONS);*/ result.Params.svm_type = EncogFileSection.ParseInt(p, ParamSVMType); result.Params.weight = EncogFileSection.ParseDoubleArray( p, ParamWeight); result.Params.weight_label = EncogFileSection .ParseIntArray(p, ParamWeightLabel); } else if (section.SectionName.Equals("SVM") && section.SubSectionName.Equals("SVM-MODEL")) { try { #if !SILVERLIGHT var rdr = new StringReader( section.LinesAsString); TextReader br = rdr; svm_model model = svm.svm_load_model(rdr); result.Model = model; br.Close(); rdr.Close(); #endif } catch (IOException ex) { throw new PersistError(ex); } } } return(result); }
/// <summary> /// Save a model. /// </summary> /// <param name="xmlout">Where to save a model to.</param> /// <param name="model">The model to save to.</param> public static void SaveModel(WriteXML xmlout, svm_model model) { if (model != null) { xmlout.BeginTag(SVMNetworkPersistor.TAG_MODEL); svm_parameter param = model.param; xmlout.AddProperty(SVMNetworkPersistor.TAG_TYPE_SVM, svm_type_table[param.svm_type]); xmlout.AddProperty(SVMNetworkPersistor.TAG_TYPE_KERNEL, kernel_type_table[param.kernel_type]); if (param.kernel_type == svm_parameter.POLY) { xmlout.AddProperty(SVMNetworkPersistor.TAG_DEGREE, param.degree); } if (param.kernel_type == svm_parameter.POLY || param.kernel_type == svm_parameter.RBF || param.kernel_type == svm_parameter.SIGMOID) { xmlout.AddProperty(SVMNetworkPersistor.TAG_GAMMA, param.gamma); } if (param.kernel_type == svm_parameter.POLY || param.kernel_type == svm_parameter.SIGMOID) { xmlout.AddProperty(SVMNetworkPersistor.TAG_COEF0, param.coef0); } int nr_class = model.nr_class; int l = model.l; xmlout.AddProperty(SVMNetworkPersistor.TAG_NUMCLASS, nr_class); xmlout.AddProperty(SVMNetworkPersistor.TAG_TOTALSV, l); xmlout.AddProperty(SVMNetworkPersistor.TAG_RHO, model.rho, nr_class * (nr_class - 1) / 2); xmlout.AddProperty(SVMNetworkPersistor.TAG_LABEL, model.label, nr_class); xmlout.AddProperty(SVMNetworkPersistor.TAG_PROB_A, model.probA, nr_class * (nr_class - 1) / 2); xmlout.AddProperty(SVMNetworkPersistor.TAG_PROB_B, model.probB, nr_class * (nr_class - 1) / 2); xmlout.AddProperty(SVMNetworkPersistor.TAG_NSV, model.nSV, nr_class); xmlout.BeginTag(SVMNetworkPersistor.TAG_DATA); double[][] sv_coef = model.sv_coef; svm_node[][] SV = model.SV; StringBuilder line = new StringBuilder(); for (int i = 0; i < l; i++) { line.Length = 0; for (int j = 0; j < nr_class - 1; j++) { line.Append(sv_coef[j][i] + " "); } svm_node[] p = SV[i]; //if (param.kernel_type == svm_parameter.PRECOMPUTED) //{ // line.Append("0:" + (int) (p[0].value)); //} //else for (int j = 0; j < p.Length; j++) { line.Append(p[j].index + ":" + p[j].value_Renamed + " "); } xmlout.AddProperty(SVMNetworkPersistor.TAG_ROW, line.ToString()); } xmlout.EndTag(); xmlout.EndTag(); } }
public int runSVMTest(Dictionary <string, double> docWord, Dictionary <string, int> dictionary, Dictionary <string, double> wordIDFDictionary, svm_model model) { List <KeyValuePair <int, int> > result = new List <KeyValuePair <int, int> >(); // generate svm nodes List <KeyValuePair <string, string> > nodeList = new List <KeyValuePair <string, string> >(); List <svm_node> nodes = new List <svm_node>(); foreach (string word in docWord.Keys) { if (dictionary.ContainsKey(word)) { int index = dictionary[word]; double TF = docWord[word]; double IDF = wordIDFDictionary[word]; double value = TF * IDF; nodes.Add(new svm_node() // svm node { index = index, value = value, }); nodeList.Add(new KeyValuePair <string, string>(System.Convert.ToString(index), System.Convert.ToString(value))); //trainStr = trainStr + "\t" + index + ":" + value; } } if (nodeList.Count > 0) { // sort the list by feature index at first nodeList.Sort( delegate(KeyValuePair <string, string> firstPair, KeyValuePair <string, string> nextPair) { int a = int.Parse(firstPair.Key); int b = int.Parse(nextPair.Key); return(a.CompareTo(b)); } ); for (int k = 0; k < nodeList.Count; k++) { KeyValuePair <string, string> node = nodeList[k]; String index = node.Key; String value = node.Value; } } int resValue = (int)svm.svm_predict(model, nodes.ToArray()); return(resValue); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected void trainInternal(java.util.LinkedHashMap<String, String> libOptions) throws org.maltparser.core.exception.MaltChainedException protected internal override void trainInternal(LinkedHashMap <string, string> libOptions) { try { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final libsvm.svm_problem prob = readProblem(getInstanceInputStreamReader(".ins"), libOptions); svm_problem prob = readProblem(getInstanceInputStreamReader(".ins"), libOptions); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final libsvm.svm_parameter param = getLibSvmParameters(libOptions); svm_parameter param = getLibSvmParameters(libOptions); if (svm.svm_check_parameter(prob, param) != null) { throw new LibException(svm.svm_check_parameter(prob, param)); } Configuration config = Configuration; if (config.LoggerInfoEnabled) { config.logInfoMessage("Creating LIBSVM model " + getFile(".moo").Name + "\n"); } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.io.PrintStream out = System.out; PrintStream @out = System.out; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.io.PrintStream err = System.err; PrintStream err = System.err; System.Out = NoPrintStream.NO_PRINTSTREAM; System.Err = NoPrintStream.NO_PRINTSTREAM; svm_model model = svm.svm_train(prob, param); System.Out = err; System.Out = @out; ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(new FileStream(getFile(".moo").AbsolutePath, FileMode.Create, FileAccess.Write))); try { output.writeObject(new MaltLibsvmModel(model, prob)); } finally { output.close(); } bool saveInstanceFiles = ((bool?)Configuration.getOptionValue("lib", "save_instance_files")).Value; if (!saveInstanceFiles) { getFile(".ins").delete(); } } catch (OutOfMemoryException e) { throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e); } catch (ArgumentException e) { throw new LibException("The LIBSVM learner was not able to redirect Standard Error stream. ", e); } catch (SecurityException e) { throw new LibException("The LIBSVM learner cannot remove the instance file. ", e); } catch (IOException e) { throw new LibException("The LIBSVM learner cannot save the model file '" + getFile(".mod").AbsolutePath + "'. ", e); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: protected void trainExternal(String pathExternalTrain, java.util.LinkedHashMap<String, String> libOptions) throws org.maltparser.core.exception.MaltChainedException protected internal override void trainExternal(string pathExternalTrain, LinkedHashMap <string, string> libOptions) { try { binariesInstances2SVMFileFormat(getInstanceInputStreamReader(".ins"), getInstanceOutputStreamWriter(".ins.tmp")); Configuration config = Configuration; if (config.LoggerInfoEnabled) { config.logInfoMessage("Creating learner model (external) " + getFile(".mod").Name); } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final libsvm.svm_problem prob = readProblem(getInstanceInputStreamReader(".ins"), libOptions); svm_problem prob = readProblem(getInstanceInputStreamReader(".ins"), libOptions); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final String[] params = getLibParamStringArray(libOptions); string[] @params = getLibParamStringArray(libOptions); string[] arrayCommands = new string[@params.Length + 3]; int i = 0; arrayCommands[i++] = pathExternalTrain; for (; i <= @params.Length; i++) { arrayCommands[i] = @params[i - 1]; } arrayCommands[i++] = getFile(".ins.tmp").AbsolutePath; arrayCommands[i++] = getFile(".mod").AbsolutePath; if (verbosity == Verbostity.ALL) { config.logInfoMessage('\n'); } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Process child = Runtime.getRuntime().exec(arrayCommands); Process child = Runtime.Runtime.exec(arrayCommands); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.io.InputStream in = child.getInputStream(); Stream @in = child.InputStream; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.io.InputStream err = child.getErrorStream(); Stream err = child.ErrorStream; int c; while ((c = @in.Read()) != -1) { if (verbosity == Verbostity.ALL) { config.logInfoMessage((char)c); } } while ((c = err.Read()) != -1) { if (verbosity == Verbostity.ALL || verbosity == Verbostity.ERROR) { config.logInfoMessage((char)c); } } if (child.waitFor() != 0) { config.logErrorMessage(" FAILED (" + child.exitValue() + ")"); } @in.Close(); err.Close(); svm_model model = svm.svm_load_model(getFile(".mod").AbsolutePath); MaltLibsvmModel xmodel = new MaltLibsvmModel(model, prob); ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(new FileStream(getFile(".moo").AbsolutePath, FileMode.Create, FileAccess.Write))); try { output.writeObject(xmodel); } finally { output.close(); } bool saveInstanceFiles = ((bool?)Configuration.getOptionValue("lib", "save_instance_files")).Value; if (!saveInstanceFiles) { getFile(".ins").delete(); getFile(".mod").delete(); getFile(".ins.tmp").delete(); } if (config.LoggerInfoEnabled) { config.logInfoMessage('\n'); } } catch (InterruptedException e) { throw new LibException("Learner is interrupted. ", e); } catch (ArgumentException e) { throw new LibException("The learner was not able to redirect Standard Error stream. ", e); } catch (SecurityException e) { throw new LibException("The learner cannot remove the instance file. ", e); } catch (IOException e) { throw new LibException("The learner cannot save the model file '" + getFile(".mod").AbsolutePath + "'. ", e); } catch (OutOfMemoryException e) { throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e); } }
public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable <string> allowedInputVariables, IEnumerable <double> classValues) : this(model, rangeTransform, targetVariable, allowedInputVariables) { this.classValues = classValues.ToArray(); }