2016-03-25 200 views
1

我使用的是以下来自accord.net框架网站的示例代码,该代码工作的任何方式都很好,我想实现的是将对象保存到该文件中.. I要保存DynamicTimeWarping类类型的对象,在下面的代码到磁盘内核(保存在文件中)如何将对象写入磁盘(保存在文件中)

DynamicTimeWarping kernel = new DynamicTimeWarping(length: 3); 

我一直在使用XMLSerializer的尝试,但Visual Studio是给错误,它不能被序列化,因为它不具备无参数构造函数。

double[][][] sequences = 
    { 
    { 
    new double[] { 1, 1, 1 }, // first observation of the first sequence 
    new double[] { 1, 2, 1 }, // second observation of the first sequence 
    new double[] { 1, 4, 2 }, // third observation of the first sequence 
    new double[] { 2, 2, 2 }, // fourth observation of the first sequence 
}, 

    new double[][] // second sequence (note that this sequence has a different length) 
    { 
    new double[] { 1, 1, 1 }, // first observation of the second sequence 
    new double[] { 1, 5, 6 }, // second observation of the second sequence 
    new double[] { 2, 7, 1 }, // third observation of the second sequence 
}, 

new double[][] // third sequence 
{ 
    new double[] { 8, 2, 1 }, // first observation of the third sequence 
}, 

new double[][] // fourth sequence 
{ 
    new double[] { 8, 2, 5 }, // first observation of the fourth sequence 
    new double[] { 1, 5, 4 }, // second observation of the fourth sequence 
} 
}; 



int[] outputs = 
{ 
-1,-1, // First two sequences are of class -1 (those start with {1,1,1}) 
    1, 1, // Last two sequences are of class +1 (don't start with {1,1,1}) 
}; 


double[][] inputs = new double[sequences.Length][]; 
for (int i = 0; i < sequences.Length; i++) 
    inputs[i] = Matrix.Concatenate(sequences[i]); 


// Now we have to setup the Dynamic Time Warping kernel. We will have to 
// inform the length of the fixed-length observations contained in each 
// arbitrary-length sequence: 
// 
DynamicTimeWarping kernel = new DynamicTimeWarping(length: 3); 

// Now we can create the machine. When using variable-length 
// kernels, we will need to pass zero as the input length: 
var svm = new KernelSupportVectorMachine(kernel, inputs: 0); 


// Create the Sequential Minimal Optimization learning algorithm 
var smo = new SequentialMinimalOptimization(svm, inputs, outputs) 
{ 
Complexity = 1.5 
}; 

// And start learning it! 
    double error = smo.Run(); // error will be 0.0 


// At this point, we should have obtained an useful machine. Let's 
// see if it can understand a few examples it hasn't seem before: 

double[][] a = 
    { 
    new double[] { 1, 1, 1 }, 
     new double[] { 7, 2, 5 }, 
     new double[] { 2, 5, 1 }, 
     }; 

double[][] b = 
    { 
    new double[] { 7, 5, 2 }, 
    new double[] { 4, 2, 5 }, 
    new double[] { 1, 1, 1 }, 
}; 



int resultA = System.Math.Sign(svm.Compute(Matrix.Concatenate(a))); // -1 
int resultB = System.Math.Sign(svm.Compute(Matrix.Concatenate(b))); // +1 
+0

你有没有尝试添加属性要系列化你的课吗? [Serializable()] – Karolis

+0

...所以添加一个无参数ctor – Plutonix

+0

@carl该类是accord.net框架的内置类。我无法自己添加任何属性 – user3544843

回答

1

看起来像这样在该框架中的类已添加SerializableAttribute

这个例子应该做的:

https://msdn.microsoft.com/en-us/library/system.serializableattribute%28v=vs.110%29.aspx

+0

我得到错误(它不能被序列化,因为它没有无参数的构造函数。) – user3544843

+0

看起来他们在开发这个类时没有测试过。必须有无参数的构造函数......我可以看到'Accord.Statistics.Kernels.KernelBase'有一个,也许这将足以让你序列化?加上它看起来不像密封'公共类DynamicTimeWarping:KernelBase, \t IKernel,ICloneable,IDisposable' –

+0

你能告诉我如何实现 – user3544843

相关问题