Serializer Class |
Namespace: Accord.IO
The Serializer type exposes the following members.
Name | Description | |
---|---|---|
DeepCloneT |
Performs a deep copy of an object by serializing and deserializing it.
| |
GetValueT |
Retrieves a value from the SerializationInfo store.
| |
LoadT(String) |
Loads an object from a file.
| |
LoadT(Byte, SerializerCompression) |
Loads an object from a stream, represented as an array of bytes.
| |
LoadT(Stream, SerializerCompression) |
Loads an object from a stream.
| |
LoadT(String, SerializerCompression) |
Loads an object from a file.
| |
LoadT(String, T) |
Loads an object from a file.
| |
LoadT(Byte, T, SerializerCompression) |
Loads an object from a stream, represented as an array of bytes.
| |
LoadT(Stream, BinaryFormatter, SerializerCompression) |
Loads a model from a stream.
| |
LoadT(Stream, T, SerializerCompression) |
Loads an object from a stream.
| |
LoadT(String, T, SerializerCompression) |
Loads an object from a file.
| |
SaveT(T, SerializerCompression) |
Saves an object to a stream, represented as an array of bytes.
| |
SaveT(T, String) |
Saves an object to a stream.
| |
SaveT(T, Byte, SerializerCompression) |
Saves an object to a stream.
| |
SaveT(T, Stream, SerializerCompression) |
Saves an object to a stream.
| |
SaveT(T, String, SerializerCompression) |
Saves an object to a stream.
| |
SaveT(T, BinaryFormatter, Stream, SerializerCompression) |
Saves an object to a stream.
|
The first example shows the simplest way to use the serializer to persist objects:
// The serializer class can be used to save and load Accord.NET // models to and from the disk (and/or streams and byte arrays). // For example, we can create a new object: DoubleRange range = new DoubleRange(4, 2); // And we would like to save it as string filename = Path.Combine(path, "my_range.accord"); // We can call Serializer.Save to save it: Serializer.Save(obj: range, path: filename); // Then, we can load it back using: Serializer.Load(filename, out range); // Or using the explicit generic method call: range = Serializer.Load<DoubleRange>(filename);
The second example shows the same, but using compression:
// The serializer can also use compression // For example, we can create a new object: DoubleRange range = new DoubleRange(4, 2); // And we would like to save it as string filename = Path.Combine(path, "my_range.accord"); // We can call Serializer.Save to save it: Serializer.Save(obj: range, path: filename, compression: SerializerCompression.GZip); // Please note that in this case, the serializer will // append the ".gz" extension to the generated file, // if it doesn't have it already. // Then, again we could load it back using: Serializer.Load(filename + ".gz", out range);
The third and last example shows a complete example on how to create, save and re-load a classifier from disk using serialization:
// Create some sample learning data. In this data, // the first two instances belong to a class, the // four next belong to another class and the last // three to yet another. double[][] inputs = { // The first two are from class 0 new double[] { -5, -2, -1 }, new double[] { -5, -5, -6 }, // The next four are from class 1 new double[] { 2, 1, 1 }, new double[] { 1, 1, 2 }, new double[] { 1, 2, 2 }, new double[] { 3, 1, 2 }, // The last three are from class 2 new double[] { 11, 5, 4 }, new double[] { 15, 5, 6 }, new double[] { 10, 5, 6 }, }; int[] outputs = { 0, 0, // First two from class 0 1, 1, 1, 1, // Next four from class 1 2, 2, 2 // Last three from class 2 }; // Now we will create the K-Nearest Neighbors algorithm. For this // example, we will be choosing k = 4. This means that, for a given // instance, its nearest 4 neighbors will be used to cast a decision. var knn = new KNearestNeighbors(k: 4); // We learn the algorithm: knn.Learn(inputs, outputs); // After the algorithm has been created, we can classify a new instance: int answer = knn.Decide(new double[] { 11, 5, 4 }); // answer will be 2. // Let's say we would like to compute the error matrix for the classifier: var cm = GeneralConfusionMatrix.Estimate(knn, inputs, outputs); // We can use it to estimate measures such as double error = cm.Error; // should be double acc = cm.Accuracy; // should be double kappa = cm.Kappa; // should be
// After we have created and learned our model, let's say we would // like to save it to disk. For this, we can import the Accord.IO // namespace at the top of our source file namespace, and then use // Serializer's extension method Save: // Save to a file called "knn.bin" in the basePath directory: knn.Save(Path.Combine(basePath, "knn.bin")); // To load it back from the disk, we might need to use the Serializer class directly: var loaded_knn = Serializer.Load<KNearestNeighbors>(Path.Combine(basePath, "knn.bin")); // At this point, knn and loaded_knn should be // two different instances of identical objects.