2016-03-25 122 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

相關問題