如何将对象写入磁盘(保存在文件中)

How to write object to disk(save in a file)

我正在使用下面来自 accord.net 框架网站的示例代码,这段代码工作正常的任何方式我想要实现的是将对象保存到文件中.. 我想将以下代码中的 DynamicTimeWarping class 类型对象,内核保存到磁盘(保存在文件中)

   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

在该框架中看起来像这样 Class 添加了 SerializableAttribute

这个例子应该做的:

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