2016-03-25 3 views
1

Я использую примерный код ниже с сайта framework.net. Любой способ, которым этот код работает отлично, что я хочу достичь, - это сохранить объект в файл. I хотите сохранить алгоритм динамической трансформации временной шкалы объект типа класса, ядро ​​в следующий код на диск (сохранить в файл)Как записать объект на диск (сохранить в файле)

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 этот класс является встроенным классом framework.net framework. Я не могу добавить какой-либо атрибут самостоятельно – user3544843

ответ

1

Похоже, что этот класс в этой структуре содержит SerializableAttribute.

Этот пример должен сделать:

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

+0

Я получаю ошибку (она не может быть сериализована, потому что у нее нет конструктора без параметров). – user3544843

+0

Хорошо, похоже, что при разработке этого класса у них не было тестирования. Должен быть беспараметрический конструктор ... Я вижу, что в базе 'Accord.Statistics.Kernels.KernelBase' есть один, может быть, этого будет достаточно для сериализации? Плюс это не похоже на герметичность 'public class DynamicTimeWarping: KernelBase, \t IKernel, ICloneable, IDisposable' –

+0

Не могли бы вы показать мне, как это сделать? – user3544843

Смежные вопросы