Serialization

Module graphs can be serialized and deserialized to and from XML using the MMF Serialization.ModuleGraphXmlSerializer class. The SimpleML Program class provides a sample use cases for serialization...

// Following code gives an example of how to serialize a module graph... String graphFilePath = Path.Combine(logFilePath, "SimpleMLSampleGraph.xml"); using (FileStream serializedGraphStream = new FileStream(graphFilePath, FileMode.Create)) using (StreamWriter serializedGraphStreamWriter = new StreamWriter(serializedGraphStream)) { ModuleGraphXmlSerializer moduleGraphXmlSerializer = new ModuleGraphXmlSerializer(xmlDataSerializer); moduleGraphXmlSerializer.Serialize(BuildLinearRegressionModuleGraph(), serializedGraphStreamWriter); }

...and deserialization...

ModuleGraph moduleGraph; // Deserialize the module graph from the specified XML file String resourcesPath = Path.Combine(Environment.CurrentDirectory, @"..\..\", "Resources"); using (FileStream serializedGraphStream = new FileStream(Path.Combine(resourcesPath, graphFileName), FileMode.Open)) using (StreamReader serializedGraphStreamReader = new StreamReader(serializedGraphStream)) { ModuleGraphXmlSerializer moduleGraphXmlSerializer = new ModuleGraphXmlSerializer(xmlDataSerializer); moduleGraph = moduleGraphXmlSerializer.Deserialize(serializedGraphStreamReader); }

Serialization of Input Data

MMF allows InputSlots which have constant data values set, to have their data value serialized or deserialized as part of a module graph. Serialization of data values is handled by the MMF Serialization.XmlDataSerializer class. For custom data types, serialization and deserialization routines must be defined by calling the AddDataTypeSupport() method, and passing the data type and two routines. An example of adding support for SimpleML.Containers.Matrix objects is provided in the Program class...

xmlDataSerializer.AddDataTypeSupport ( typeof(Matrix), (Object inputObject, XmlWriter xmlWriter) => { containerObjectXmlSerializer.SerializeMatrix((Matrix)inputObject, xmlWriter); }, (XmlReader xmlReader) => { return containerObjectXmlSerializer.DeserializeMatrix(xmlReader); } );

A sample use case of input data serialization occurs with the input matrix to the 'Linear Regression Hypothesis Calculator' in the linear regression workflows. The input matrix is deserialized from the XML document representing the module graph...

<Module> <Id>10</Id> <Type>SimpleML.Samples.Modules.FeatureScaler</Type> <Assembly>SimpleML.Samples.Modules.dll</Assembly> <InputSlots> <InputSlot> <Name>InputMatrix</Name> <Data> <Type>SimpleML.Containers.Matrix</Type> <Assembly>SimpleML.Containers.dll</Assembly> <Value> <Dimensions> <MDimension>3</MDimension> <NDimension>2</NDimension> </Dimensions> <Rows> <Row>5103.27, 15323.14</Row> <Row>5411.61, 16385.89</Row> <Row>3510.4, 8235.87</Row> </Rows> </Value> </Data> (etc...)