Class SerializationHelper<T>
Provides methods for serializing and deserializing AI model components to and from binary formats.
public static class SerializationHelper<T>
Type Parameters
TThe numeric type used in the AI models (e.g., double, float, int).
- Inheritance
-
SerializationHelper<T>
- Inherited Members
Remarks
For Beginners: Serialization is the process of converting complex data structures (like AI models) into a format that can be easily stored or transmitted. Think of it like saving your game progress so you can continue later. This helper makes it possible to save trained AI models to disk and load them back when needed.
Methods
DeserializeMatrix(byte[])
Deserializes a matrix from a byte array.
public static Matrix<T> DeserializeMatrix(byte[] data)
Parameters
databyte[]The byte array containing the serialized matrix.
Returns
- Matrix<T>
The deserialized matrix.
Remarks
For Beginners: This method converts a compact byte representation (which might have been stored in a database or received over a network) back into a usable matrix for AI calculations.
DeserializeMatrix(BinaryReader)
Deserializes a matrix from a binary reader without specifying expected dimensions.
public static Matrix<T> DeserializeMatrix(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read the serialized data from.
Returns
- Matrix<T>
The deserialized matrix with dimensions read from the binary data.
Remarks
For Beginners: This method loads a matrix (grid of numbers) from a file without needing to know its size in advance. The size information is stored in the file itself.
DeserializeMatrix(BinaryReader, int, int)
Deserializes a matrix from a binary format with expected dimensions.
public static Matrix<T> DeserializeMatrix(BinaryReader reader, int rows, int columns)
Parameters
readerBinaryReaderThe binary reader to read the serialized data from.
rowsintThe expected number of rows in the matrix.
columnsintThe expected number of columns in the matrix.
Returns
- Matrix<T>
The deserialized matrix.
Remarks
For Beginners: This method loads a previously saved matrix from a file, but checks that it has the size you expect. This is important because AI algorithms need matrices of specific sizes.
Exceptions
- InvalidOperationException
Thrown when the stored matrix dimensions do not match the expected dimensions.
DeserializeNode(BinaryReader)
Deserializes a decision tree node and its children from a binary format.
public static DecisionTreeNode<T>? DeserializeNode(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read the serialized data from.
Returns
- DecisionTreeNode<T>
The deserialized decision tree node.
Remarks
For Beginners: This method loads a previously saved decision tree from a file, reconstructing its structure so you can use it to make predictions without retraining.
DeserializeTensor(BinaryReader)
Deserializes a tensor from a binary stream.
public static Tensor<T> DeserializeTensor(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read from.
Returns
- Tensor<T>
The deserialized tensor.
Remarks
This helper method reads a tensor's shape and values from a binary stream. It first reads the rank (number of dimensions), then each dimension size, creates a tensor with that shape, and finally reads all the values.
For Beginners: This method loads a tensor's structure and values from a file.
When loading a tensor:
- First, it reads how many dimensions the tensor has
- Then, it reads the size of each dimension
- It creates a new tensor with that shape
- Finally, it reads all the values and fills the tensor
This process reverses the serialization process, reconstructing the tensor exactly as it was when saved.
DeserializeVector(byte[])
Deserializes a vector from a byte array.
public static Vector<T> DeserializeVector(byte[] data)
Parameters
databyte[]The byte array containing the serialized vector.
Returns
- Vector<T>
The deserialized vector.
Remarks
For Beginners: This method converts a compact byte representation (which might have been stored in a database or received over a network) back into a usable vector for AI calculations. Vectors are essential in many AI algorithms for representing features, weights, or predictions.
DeserializeVector(BinaryReader)
Deserializes a vector from a binary reader without specifying expected length.
public static Vector<T> DeserializeVector(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read the serialized data from.
Returns
- Vector<T>
The deserialized vector with length read from the binary data.
Remarks
For Beginners: This method loads a vector (list of numbers) from a file without needing to know its length in advance. The length information is stored in the file itself.
DeserializeVector(BinaryReader, int)
Deserializes a vector from a binary format with an expected length.
public static Vector<T> DeserializeVector(BinaryReader reader, int length)
Parameters
readerBinaryReaderThe binary reader to read the serialized data from.
lengthintThe expected length of the vector.
Returns
- Vector<T>
The deserialized vector.
Remarks
For Beginners: This method loads a previously saved vector (list of numbers) from a file, but checks that it has the length you expect. This is important because AI algorithms often need vectors of specific lengths to work correctly.
Exceptions
- InvalidOperationException
Thrown when the stored vector length does not match the expected length.
ReadValue(BinaryReader)
Reads a value of type T from a binary reader.
public static T ReadValue(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read from.
Returns
- T
The value read from the binary reader.
Remarks
For Beginners: This method reads different types of numbers from a file and converts them back to their original form so they can be used in calculations.
Exceptions
- NotSupportedException
Thrown when the type T is not supported for deserialization.
SerializeInterface<TInterface>(BinaryWriter, TInterface?)
Serializes an interface instance by writing its type name to a BinaryWriter.
public static void SerializeInterface<TInterface>(BinaryWriter writer, TInterface? instance) where TInterface : class
Parameters
writerBinaryWriterThe BinaryWriter to write the type name to.
instanceTInterfaceThe interface instance to serialize.
Type Parameters
TInterfaceThe interface type to serialize.
Remarks
This method writes the full name of the concrete type implementing the interface. If the instance is null, it writes an empty string.
For Beginners: This method saves information about a specific part of your network.
It writes:
- The name of the actual type used (if there is one)
- An empty string if no specific type is used
This allows you to recreate the exact same setup when you load the network later.
SerializeMatrix(Matrix<T>)
Serializes a matrix to a byte array.
public static byte[] SerializeMatrix(Matrix<T> matrix)
Parameters
matrixMatrix<T>The matrix to serialize.
Returns
- byte[]
A byte array containing the serialized matrix.
Remarks
For Beginners: This method converts a matrix (grid of numbers) into a compact format that can be easily stored in memory or in a database.
SerializeMatrix(BinaryWriter, Matrix<T>)
Serializes a matrix to a binary format.
public static void SerializeMatrix(BinaryWriter writer, Matrix<T> matrix)
Parameters
writerBinaryWriterThe binary writer to write the serialized data to.
matrixMatrix<T>The matrix to serialize.
Remarks
For Beginners: A matrix is a rectangular grid of numbers used in many AI algorithms. This method saves that grid to a file so you can use it later.
SerializeNode(DecisionTreeNode<T>?, BinaryWriter)
Serializes a decision tree node and its children to a binary format.
public static void SerializeNode(DecisionTreeNode<T>? node, BinaryWriter writer)
Parameters
nodeDecisionTreeNode<T>The decision tree node to serialize.
writerBinaryWriterThe binary writer to write the serialized data to.
Remarks
For Beginners: This method saves a decision tree (a flowchart-like model that makes decisions) to a file. Decision trees are used in AI for classification and regression tasks.
SerializeTensor(BinaryWriter, Tensor<T>)
Serializes a tensor to a binary stream.
public static void SerializeTensor(BinaryWriter writer, Tensor<T> tensor)
Parameters
writerBinaryWriterThe binary writer to write to.
tensorTensor<T>The tensor to serialize.
Remarks
This helper method writes a tensor's shape and values to a binary stream. It first writes the rank (number of dimensions), then each dimension size, and finally all the tensor values as doubles.
For Beginners: This method saves a tensor's structure and values to a file.
When saving a tensor:
- First, it saves how many dimensions the tensor has (its rank)
- Then, it saves the size of each dimension
- Finally, it saves all the actual values
This format ensures that when loading, the tensor can be reconstructed with exactly the same shape and values.
SerializeVector(Vector<T>)
Serializes a vector to a byte array.
public static byte[] SerializeVector(Vector<T> vector)
Parameters
vectorVector<T>The vector to serialize.
Returns
- byte[]
A byte array containing the serialized vector.
Remarks
For Beginners: This method converts a vector (list of numbers) into a compact format that can be easily stored in memory or in a database. This is useful when you want to save trained model parameters for later use without retraining.
SerializeVector(BinaryWriter, Vector<T>)
Serializes a vector to a binary format.
public static void SerializeVector(BinaryWriter writer, Vector<T> vector)
Parameters
writerBinaryWriterThe binary writer to write the serialized data to.
vectorVector<T>The vector to serialize.
Remarks
For Beginners: A vector is a one-dimensional array of numbers, like a single row or column of data. This method saves that list of numbers to a file so you can use it later. Vectors are commonly used in AI to represent features or weights.
WriteValue(BinaryWriter, T)
Writes a value of type T to a binary writer.
public static void WriteValue(BinaryWriter writer, T value)
Parameters
writerBinaryWriterThe binary writer to write to.
valueTThe value to write.
Remarks
For Beginners: This method handles writing different types of numbers (like integers or decimals) to a file in a way that preserves their exact values.
Exceptions
- NotSupportedException
Thrown when the type T is not supported for serialization.