public void ReadWriteTest_a9a() { string basePath = NUnit.Framework.TestContext.CurrentContext.TestDirectory; MemoryStream file = new MemoryStream(Encoding.Default.GetBytes(Resources.L1R_LR_a9a)); LibSvmModel model1 = LibSvmModel.Load(file); string savePath = Path.Combine(basePath, "svm.txt"); var svm1 = model1.CreateMachine(); model1.Save(savePath); LibSvmModel model2 = LibSvmModel.Load(savePath); var svm2 = model2.CreateMachine(); LibSvmModel model3 = LibSvmModel.FromMachine(svm1); var svm3 = model3.CreateMachine(); model3.Solver = LibSvmSolverType.L1RegularizedLogisticRegression; string aPath = Path.Combine(basePath, "a.txt"); string bPath = Path.Combine(basePath, "b.txt"); string cPath = Path.Combine(basePath, "c.txt"); model1.Save(aPath); model2.Save(bPath); model3.Save(cPath); string a = File.ReadAllText(aPath); string b = File.ReadAllText(bPath); string c = File.ReadAllText(cPath); Assert.AreEqual(a, b); Assert.AreEqual(a, c); Assert.AreEqual(svm1.Weights, svm2.Weights); Assert.AreEqual(svm1.SupportVectors, svm2.SupportVectors); Assert.AreEqual(svm1.Threshold, svm2.Threshold); Assert.AreEqual(svm1.Weights, svm3.Weights); Assert.AreEqual(svm1.SupportVectors, svm3.SupportVectors); Assert.AreEqual(svm1.Threshold, svm3.Threshold); }
public void WriteLinearMachineTest() { MemoryStream destination = new MemoryStream(); var model = new LibSvmModel() { Bias = -1, Classes = 2, Dimension = 123, Labels = new[] { +1, -1 }, Solver = LibSvmSolverType.L1RegularizedLogisticRegression, Weights = a9a_weights }; model.Save(destination); destination.Seek(0, SeekOrigin.Begin); TextReader textReader = new StreamReader(destination); string[] actual = textReader.ReadToEnd() .Split(new[] { Environment.NewLine }, StringSplitOptions.None); string[] expected = Resources.L1R_LR_a9a .Split(new[] { "\r\n" }, StringSplitOptions.None); Assert.AreEqual(expected.Length, actual.Length); for (int i = 0; i < 7; i++) { Assert.AreEqual(expected[i], actual[i]); } for (int i = 6; i < expected.Length; i++) { if (expected[i] == actual[i]) { continue; } double a = Double.Parse(expected[i], CultureInfo.InvariantCulture); double b = Double.Parse(actual[i], CultureInfo.InvariantCulture); Assert.AreEqual(a, b); } Assert.AreEqual(expected[expected.Length - 1], String.Empty); }
public void WriteLinearMachineTest_ExactCopy() { MemoryStream file = new MemoryStream( Encoding.Default.GetBytes(Resources.L1R_LR_a9a)); LibSvmModel reader = LibSvmModel.Load(file); MemoryStream destination = new MemoryStream(); reader.Save(destination); destination.Seek(0, SeekOrigin.Begin); TextReader textReader = new StreamReader(destination); string[] actual = textReader.ReadToEnd() .Split(new[] { Environment.NewLine }, StringSplitOptions.None); string[] expected = Resources.L1R_LR_a9a .Split(new[] { "\r\n" }, StringSplitOptions.None); Assert.AreEqual(expected.Length, actual.Length); for (int i = 0; i < 7; i++) { Assert.AreEqual(expected[i], actual[i]); } for (int i = 6; i < expected.Length; i++) { if (expected[i] == actual[i]) { continue; } double a = Double.Parse(expected[i], CultureInfo.InvariantCulture); double b = Double.Parse(actual[i], CultureInfo.InvariantCulture); Assert.AreEqual(a, b); } Assert.AreEqual(expected[expected.Length - 1], String.Empty); }