Click or drag to resize
Accord.NET (logo)

Serializer Class

Model serializer. Can be used to serialize and deserialize (i.e. save and load) models from the framework to and from the disk and other streams.
Inheritance Hierarchy
SystemObject
  Accord.IOSerializer

Namespace:  Accord.IO
Assembly:  Accord (in Accord.dll) Version: 3.8.0
Syntax
public static class Serializer
Request Example View Source

The Serializer type exposes the following members.

Methods
  NameDescription
Public methodStatic memberDeepCloneT
Performs a deep copy of an object by serializing and deserializing it.
Public methodStatic memberGetValueT
Retrieves a value from the SerializationInfo store.
Public methodStatic memberLoadT(String)
Loads an object from a file.
Public methodStatic memberLoadT(Byte, SerializerCompression)
Loads an object from a stream, represented as an array of bytes.
Public methodStatic memberLoadT(Stream, SerializerCompression)
Loads an object from a stream.
Public methodStatic memberLoadT(String, SerializerCompression)
Loads an object from a file.
Public methodStatic memberLoadT(String, T)
Loads an object from a file.
Public methodStatic memberLoadT(Byte, T, SerializerCompression)
Loads an object from a stream, represented as an array of bytes.
Public methodStatic memberLoadT(Stream, BinaryFormatter, SerializerCompression)
Loads a model from a stream.
Public methodStatic memberLoadT(Stream, T, SerializerCompression)
Loads an object from a stream.
Public methodStatic memberLoadT(String, T, SerializerCompression)
Loads an object from a file.
Public methodStatic memberSaveT(T, SerializerCompression)
Saves an object to a stream, represented as an array of bytes.
Public methodStatic memberSaveT(T, String)
Saves an object to a stream.
Public methodStatic memberSaveT(T, Byte, SerializerCompression)
Saves an object to a stream.
Public methodStatic memberSaveT(T, Stream, SerializerCompression)
Saves an object to a stream.
Public methodStatic memberSaveT(T, String, SerializerCompression)
Saves an object to a stream.
Public methodStatic memberSaveT(T, BinaryFormatter, Stream, SerializerCompression)
Saves an object to a stream.
Top
Remarks
This class uses a binding mechanism to automatically convert files saved using older versions of the framework to the new format. If a deserialization doesn't work, please fill in a bug report at https://github.com/accord-net/framework/issues
Examples

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.
See Also