public override global::System.Data.DataSet Clone() { NeuralNetworkDataSet cln = ((NeuralNetworkDataSet)(base.Clone())); cln.InitVars(); cln.SchemaSerializationMode = this.SchemaSerializationMode; return(cln); }
public static global::System.Xml.Schema.XmlSchemaComplexType GetTypedDataSetSchema(global::System.Xml.Schema.XmlSchemaSet xs) { NeuralNetworkDataSet ds = new NeuralNetworkDataSet(); global::System.Xml.Schema.XmlSchemaComplexType type = new global::System.Xml.Schema.XmlSchemaComplexType(); global::System.Xml.Schema.XmlSchemaSequence sequence = new global::System.Xml.Schema.XmlSchemaSequence(); global::System.Xml.Schema.XmlSchemaAny any = new global::System.Xml.Schema.XmlSchemaAny(); any.Namespace = ds.Namespace; sequence.Items.Add(any); type.Particle = sequence; global::System.Xml.Schema.XmlSchema dsSchema = ds.GetSchemaSerializable(); if (xs.Contains(dsSchema.TargetNamespace)) { global::System.IO.MemoryStream s1 = new global::System.IO.MemoryStream(); global::System.IO.MemoryStream s2 = new global::System.IO.MemoryStream(); try { global::System.Xml.Schema.XmlSchema schema = null; dsSchema.Write(s1); for (global::System.Collections.IEnumerator schemas = xs.Schemas(dsSchema.TargetNamespace).GetEnumerator(); schemas.MoveNext();) { schema = ((global::System.Xml.Schema.XmlSchema)(schemas.Current)); s2.SetLength(0); schema.Write(s2); if ((s1.Length == s2.Length)) { s1.Position = 0; s2.Position = 0; for (; ((s1.Position != s1.Length) && (s1.ReadByte() == s2.ReadByte()));) { ; } if ((s1.Position == s1.Length)) { return(type); } } } } finally { if ((s1 != null)) { s1.Close(); } if ((s2 != null)) { s2.Close(); } } } xs.Add(dsSchema); return(type); }
public void Save(string fileName, bool includeWeights = false) { if (fileName.Contains("-gz")) { using (NeuralNetworkDataSet ds = new NeuralNetworkDataSet()) { NeuralNetworkDataSet.LossFunctionsRow lossFunctionRow; NeuralNetworkDataSet.LayerTypesRow layerTypeRow; NeuralNetworkDataSet.KernelTypesRow kernelTypeRow; ds.LossFunctions.BeginLoadData(); foreach (string lossFunctionName in Enum.GetNames(typeof(LossFunctions))) { ds.LossFunctions.AddLossFunctionsRow(lossFunctionName); } ds.LossFunctions.EndLoadData(); ds.LayerTypes.BeginLoadData(); foreach (string layerTypeName in Enum.GetNames(typeof(LayerTypes))) { ds.LayerTypes.AddLayerTypesRow(layerTypeName); } ds.LayerTypes.EndLoadData(); ds.KernelTypes.BeginLoadData(); foreach (string kernelTypeName in Enum.GetNames(typeof(KernelTypes))) { ds.KernelTypes.AddKernelTypesRow(kernelTypeName); } ds.KernelTypes.EndLoadData(); lossFunctionRow = ds.LossFunctions.FindByLossFunction((int)this.LossFunction); ds.NeuralNetworks.BeginLoadData(); NeuralNetworkDataSet.NeuralNetworksRow networkRow = ds.NeuralNetworks.AddNeuralNetworksRow(this.Id, this.Name, this.CreatedOn, this.TrainToValue, lossFunctionRow, this.dMicron); ds.NeuralNetworks.EndLoadData(); ds.Layers.BeginLoadData(); foreach (Layers layer in Layers) { layerTypeRow = ds.LayerTypes.FindByLayerType((int)layer.LayerType); kernelTypeRow = ds.KernelTypes.FindByKernelType((int)layer.KernelType); ds.Layers.AddLayersRow(networkRow, layer.LayerIndex, layerTypeRow, kernelTypeRow, layer.NeuronCount, layer.UseMapInfo, layer.MapCount, layer.MapWidth, layer.MapHeight, layer.IsFullyMapped, layer.ReceptiveFieldWidth, layer.ReceptiveFieldHeight, layer.LockedWeights); if (layer.Mappings != null) { ds.Mappings.BeginLoadData(); int previousMapIndex = 0; int currentMapIndex = 0; foreach (bool mapped in layer.Mappings.IsMapped) { ds.Mappings.AddMappingsRow(this.Id, layer.LayerIndex, previousMapIndex, currentMapIndex, mapped); currentMapIndex++; if (currentMapIndex >= layer.MapCount) { currentMapIndex = 0; previousMapIndex++; } } ds.Mappings.EndLoadData(); } if (includeWeights) { ds.Weights.BeginLoadData(); int weightIndex = 0; foreach (Weight weight in layer.Weights) { ds.Weights.AddWeightsRow(this.Id, layer.LayerIndex, weightIndex, weight.Value, weight.DiagonalHessian); weightIndex++; } ds.Weights.EndLoadData(); } } ds.Layers.EndLoadData(); using (FileStream outFile = File.Create(fileName)) { using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress)) { ds.WriteXml(Compress, System.Data.XmlWriteMode.WriteSchema); } } } } }
public static NeuralNetworks Load(string fileName, bool includeWeights = false) { NeuralNetworks network = null; if (fileName.Contains("-gz")) { using (NeuralNetworkDataSet ds = new NeuralNetworkDataSet()) { using (FileStream inFile = File.OpenRead(fileName)) { using (GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress)) { ds.ReadXml(Decompress, XmlReadMode.ReadSchema); } } if (ds.NeuralNetworks.Rows.Count == 1) { network = new NeuralNetworks(); NeuralNetworkDataSet.NeuralNetworksRow networkRow = ds.NeuralNetworks.First(); network.Id = networkRow.NetworkId; network.Name = networkRow.Name; network.TrainToValue = networkRow.TrainTo; network.LossFunction = (LossFunctions)networkRow.LossFunction; network.CreatedOn = networkRow.CreatedOn; network.dMicron = networkRow.DMicron; Layers layer; Layers previousLayer = null; foreach (NeuralNetworkDataSet.LayersRow layerRow in networkRow.GetLayersRows()) { List<bool> isMapped = new List<bool>(); foreach (NeuralNetworkDataSet.MappingsRow mappingRow in layerRow.GetMappingsRows()) { isMapped.Add(mappingRow.IsMapped); } Mappings mappings = null; if (isMapped.Count > 0) mappings = new Mappings(network, layerRow.LayerIndex, isMapped); layer = new Layers(network, layerRow.LayerIndex, (LayerTypes)layerRow.LayerType, (KernelTypes)layerRow.KernelType, layerRow.NeuronCount, layerRow.UseMapInfo, layerRow.MapCount, layerRow.MapWidth, layerRow.MapHeight, layerRow.IsFullyMapped, layerRow.ReceptiveFieldWidth, layerRow.ReceptiveFieldHeight, previousLayer, mappings, layerRow.LockedWeights); if ((includeWeights) && (layerRow.GetWeightsRows().Count() > 0)) { int i = 0; foreach (NeuralNetworkDataSet.WeightsRow weightRow in layerRow.GetWeightsRows()) { layer.Weights[i].Value = weightRow.Value; layer.Weights[i].DiagonalHessian = weightRow.DiagonalHessian; i++; } } network.Layers.Add(layer); previousLayer = layer; } if (!includeWeights) network.InitWeights(true); } else { InformationDialog.Show(null, "Invalid data format.", "Select an different file", "Information"); } } } return network; }