/// <summary> /// Save the specified Encog object to an XML writer. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlOut">The XML writer to save to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlOut) { PersistorUtil.BeginEncogObject( EncogPersistedCollection.TYPE_WEIGHTLESS_SYNAPSE, xmlOut, obj, false); xmlOut.EndTag(); }
/// <summary> /// Save the specified Encog object to an XML writer. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlOut">The XML writer to save to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlOut) { PersistorUtil.BeginEncogObject( EncogPersistedCollection.TYPE_CONTEXT_LAYER, xmlOut, obj, false); ContextLayer layer = (ContextLayer)obj; xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_NEURONS, layer .NeuronCount); xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_X, layer.X); xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_Y, layer.Y); if (layer.HasBias) { StringBuilder result = new StringBuilder(); NumberList.ToList(CSVFormat.EG_FORMAT, result, layer.BiasWeights); xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_THRESHOLD, result .ToString()); } StringBuilder ctx = new StringBuilder(); NumberList.ToList(CSVFormat.EG_FORMAT, ctx, layer.Context.Data); xmlOut.AddProperty(PROPERTY_CONTEXT, ctx.ToString()); xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_BIAS_ACTIVATION, layer.BiasActivation); BasicLayerPersistor.SaveActivationFunction(layer.ActivationFunction, xmlOut); xmlOut.EndTag(); }
/// <summary> /// Save the object to XML. /// </summary> /// <param name="encogObject">The object to save.</param> /// <param name="xmlOut">The XML writer.</param> public void Save(IEncogPersistedObject encogObject, WriteXML xmlOut) { this.xmlOut = xmlOut; PersistorUtil.BeginEncogObject(encogObject.GetType().Name , xmlOut, encogObject, true); this.tagger.Analyze(encogObject); foreach (FieldInfo childField in ReflectionUtil .GetAllFields(encogObject.GetType())) { if (ReflectionUtil.ShouldAccessField(childField, true)) { Object childValue = childField.GetValue(encogObject); xmlOut.BeginTag(childField.Name); SaveField(childValue); xmlOut.EndTag(); } } xmlOut.EndTag(); }
/// <summary> /// Write the beginning XML for an Encog object. /// </summary> /// <param name="objectType">The object type to persist.</param> /// <param name="xmlOut">The object that is being persisted.</param> /// <param name="obj">The XML writer.</param> /// <param name="top">Is this a top-level object, that needs a name /// and description?</param> public static void BeginEncogObject(String objectType, WriteXML xmlOut, IEncogPersistedObject obj, bool top) { if (top) { if (obj.Name != null) { xmlOut.AddAttribute("name", obj.Name); } if (obj.Description != null) { xmlOut.AddAttribute("description", obj.Description); } else { xmlOut.AddAttribute("description", ""); } } xmlOut.AddAttribute("native", obj.GetType().Name); xmlOut.AddAttribute("id", "1"); xmlOut.BeginTag(objectType); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="xmlOut"></param> public void Save(IEncogPersistedObject obj, WriteXML xmlOut) { PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_TRAINING, xmlOut, obj, true); INeuralDataSet set = (INeuralDataSet)obj; StringBuilder builder = new StringBuilder(); foreach (INeuralDataPair pair in set) { xmlOut.BeginTag(BasicNeuralDataSetPersistor.TAG_ITEM); NumberList.ToList(CSVFormat.EG_FORMAT, builder, pair.Input.Data); xmlOut.AddProperty(BasicNeuralDataSetPersistor.TAG_INPUT, builder .ToString()); if (pair.Ideal != null) { NumberList.ToList(CSVFormat.EG_FORMAT, builder, pair.Ideal.Data); xmlOut.AddProperty(BasicNeuralDataSetPersistor.TAG_IDEAL, builder .ToString()); } xmlOut.EndTag(); } xmlOut.EndTag(); }
/// <summary> /// Save the specified Encog object to an XML writer. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlout">The XML writer to save to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlout) { PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_TEXT, xmlout, obj, true); TextData text = (TextData)obj; xmlout.AddCDATA(text.Text); xmlout.EndTag(); }
/// <summary> /// Called to search all Encog objects in this collection for one with a name /// that passes what was passed in. /// </summary> /// <param name="name">The name we are searching for.</param> /// <returns>The Encog object with the correct name.</returns> public IEncogPersistedObject Find(String name) { PersistReader reader = new PersistReader(this.filePrimary); IEncogPersistedObject result = reader.ReadObject(name); reader.Close(); return(result); }
/// <summary> /// Load an object from XML. /// </summary> /// <param name="xmlIn">The XML reader.</param> /// <param name="target">The object to load.</param> public void Load(ReadXML xmlIn, IEncogPersistedObject target) { this.xmlIn = xmlIn; this.mapper.Clear(); target.Name = xmlIn.LastTag.GetAttributeValue("name"); target.Description = xmlIn.LastTag.GetAttributeValue("description"); LoadActualObject(null, target); this.mapper.Resolve(); }
/// <inheritdoc/> public void UpdateProperties(string name, string newName, string desc) { IEncogPersistedObject obj = this.Contents[name]; obj.Name = newName; obj.Description = desc; this.Contents.Remove(name); this.Contents[newName] = obj; BuildDirectory(); }
/// <summary> /// Construct a directory entry for the specified object. /// </summary> /// <param name="obj">The Encog object.</param> public DirectoryEntry(IEncogPersistedObject obj) { String type = obj.GetType().Name; if (type.Equals("BasicNeuralDataSet")) type = EncogPersistedCollection.TYPE_TRAINING; this.type = type; this.description = obj.Description; this.name = obj.Name; }
/// <summary> /// Construct a directory entry for the specified object. /// </summary> /// <param name="obj">The Encog object.</param> public DirectoryEntry(IEncogPersistedObject obj) { String type = obj.GetType().Name; if (type.Equals("BasicNeuralDataSet")) { type = EncogPersistedCollection.TYPE_TRAINING; } this.type = type; this.description = obj.Description; this.name = obj.Name; }
/// <summary> /// Save the object. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlout">The XML output object.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlout) { PersistorUtil.BeginEncogObject( EncogPersistedCollection.TYPE_TRAINING_CONTINUATION, xmlout, obj, true); this.current = (TrainingContinuation)obj; xmlout.BeginTag(TrainingContinuationPersistor.TAG_ITEMS); SaveItems(xmlout); xmlout.EndTag(); xmlout.EndTag(); }
/// <summary> /// Save the specified Encog object to an XML writer. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlOut">The XML writer to save to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlOut) { PersistorUtil .BeginEncogObject( EncogPersistedCollection.TYPE_WEIGHTED_SYNAPSE, xmlOut, obj, false); WeightedSynapse synapse = (WeightedSynapse)obj; xmlOut.BeginTag(WeightedSynapsePersistor.TAG_WEIGHTS); PersistorUtil.SaveMatrix(synapse.WeightMatrix, xmlOut); xmlOut.EndTag(); xmlOut.EndTag(); }
/// <summary> /// Load the contents of a location. /// </summary> /// <param name="location">The location to load from.</param> public void Load(IPersistenceLocation location) { PersistReader reader = null; this.location = location; try { reader = new PersistReader(location); IDictionary <String, String> header = reader.ReadHeader(); if (header != null) { this.FileVersion = int.Parse(header["fileVersion"]); this.EncogVersion = header["encogVersion"]; this.Platform = header["platform"]; } reader.AdvanceObjectsCollection(); ReadXML xmlIn = reader.XMLInput; this.Contents.Clear(); while (xmlIn.ReadToTag()) { if (xmlIn.IsIt(PersistReader.TAG_OBJECTS, false)) { break; } String type = xmlIn.LastTag.Name; String name = xmlIn.LastTag.Attributes["name"]; IPersistor persistor = PersistorUtil .CreatePersistor(type); if (persistor == null) { throw new PersistError("Do not know how to load: " + type); } IEncogPersistedObject obj = persistor.Load(xmlIn); this.Contents[name] = obj; } } finally { if (reader != null) { reader.Close(); } BuildDirectory(); } }
/// <summary> /// Analyze the specified object and build a reference map. /// </summary> /// <param name="encogObject">The object to analyze.</param> public void Analyze(IEncogPersistedObject encogObject) { this.depth = 0; AssignObjectTag(encogObject); foreach (FieldInfo childField in ReflectionUtil .GetAllFields(encogObject.GetType())) { if (ReflectionUtil.ShouldAccessField(childField, true)) { Object childValue = childField.GetValue(encogObject); TagField(childValue); } } }
/// <summary> /// Save a RBF layer. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlout">XML stream to write to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlout) { PersistorUtil.BeginEncogObject( EncogPersistedCollection.TYPE_RADIAL_BASIS_LAYER, xmlout, obj, false); RadialBasisFunctionLayer layer = (RadialBasisFunctionLayer)obj; xmlout.AddProperty(BasicLayerPersistor.PROPERTY_NEURONS, layer.NeuronCount); xmlout.AddProperty(BasicLayerPersistor.PROPERTY_X, layer.X); xmlout.AddProperty(BasicLayerPersistor.PROPERTY_Y, layer.Y); SaveRBF(xmlout, layer); xmlout.EndTag(); }
/// <summary> /// Save a SVMNetwork. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlout">Where to save it to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlout) { PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_SVM, xmlout, obj, true); SVMNetwork net = (SVMNetwork)obj; xmlout.AddProperty(SVMNetworkPersistor.TAG_INPUT, net.InputCount); xmlout.AddProperty(SVMNetworkPersistor.TAG_OUTPUT, net.OutputCount); xmlout.BeginTag(SVMNetworkPersistor.TAG_MODELS); for (int i = 0; i < net.Models.Length; i++) { SaveModel(xmlout, net.Models[i]); } xmlout.EndTag(); xmlout.EndTag(); }
/// <summary> /// Write an object. /// </summary> /// <param name="obj">The object to write.</param> public void WriteObject(IEncogPersistedObject obj) { IPersistor persistor = obj.CreatePersistor(); if (persistor == null) { String str = "Can't find a persistor for object of type " + obj.GetType().Name; #if logging if (this.logger.IsErrorEnabled) { this.logger.Error(str); } #endif throw new PersistError(str); } persistor.Save(obj, this.xmlOut); }
/// <summary> /// Save the specified Encog object to an XML writer. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlout">The XML writer to save to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlout) { PropertyData pData = (PropertyData)obj; PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_PROPERTY, xmlout, obj, true); xmlout.BeginTag(PropertyDataPersistor.TAG_PROPERTIES); foreach (String key in pData.Data.Keys) { xmlout.AddAttribute(PropertyDataPersistor.ATTRIBUTE_NAME, key); xmlout.AddAttribute(PropertyDataPersistor.ATTRIBUTE_VALUE, pData [key]); xmlout.BeginTag(PropertyDataPersistor.TAG_PROPERTY); xmlout.EndTag(); } xmlout.EndTag(); xmlout.EndTag(); }
/// <summary> /// Add an EncogPersistedObject to the collection. /// </summary> /// <param name="name">The name of the object to load.</param> /// <param name="obj">The object to add.</param> public void Add(String name, IEncogPersistedObject obj) { if (obj is BasicNetwork) { ((BasicNetwork)obj).Structure.UpdateFlatNetwork(); } obj.Name = name; PersistWriter writer = new PersistWriter(this.fileTemp); writer.Begin(); writer.WriteHeader(); writer.BeginObjects(); writer.WriteObject(obj); writer.MergeObjects(this.filePrimary, name); writer.EndObjects(); writer.End(); writer.Close(); MergeTemp(); BuildDirectory(); }
/// <summary> /// Create a persistor object. These objects know how to persist /// certain types of classes. /// </summary> /// <param name="className">The name of the class to create a persistor for.</param> /// <returns>The persistor for the specified class.</returns> public static IPersistor CreatePersistor(String className) { // handle any hard coded ones if (className.Equals("TrainingData")) { return(new BasicNeuralDataSetPersistor()); } String name = "Encog.Persist.Persistors." + className + "Persistor"; IPersistor persistor = (IPersistor)Assembly.GetExecutingAssembly().CreateInstance(name); // try another way if (persistor == null) { String type = ReflectionUtil.ResolveEncogClass(className); IEncogPersistedObject temp = (IEncogPersistedObject)ReflectionUtil.LoadObject(type); persistor = temp.CreatePersistor(); } return(persistor); }
/// <summary> /// Save the specified Encog object to an XML writer. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlOut">The XML writer to save to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlOut) { PersistorUtil.BeginEncogObject(EncogPersistedCollection.TYPE_BASIC_NET, xmlOut, obj, true); this.currentNetwork = (BasicNetwork)obj; this.currentNetwork.Structure.FinalizeStructure(); // save the layers xmlOut.BeginTag(BasicNetworkPersistor.TAG_LAYERS); SaveLayers(xmlOut); xmlOut.EndTag(); // save the structure of these layers xmlOut.BeginTag(BasicNetworkPersistor.TAG_SYNAPSES); SaveSynapses(xmlOut); xmlOut.EndTag(); SaveProperties(xmlOut); SaveTags(xmlOut); SaveLogic(xmlOut); xmlOut.EndTag(); }
/// <summary> /// Perform a deep copy. /// Silverlight version. /// </summary> /// <param name="oldObj">The old object.</param> /// <returns>The new object.</returns> static public IEncogPersistedObject DeepCopy(IEncogPersistedObject oldObj) { bool replacedName = false; // encog objects won't save without a name if (oldObj.Name == null) { replacedName = true; oldObj.Name = "temp"; } // now make the copy MemoryStream mstream = new MemoryStream(); WriteXML xmlOut = new WriteXML(mstream); IPersistor persistor = oldObj.CreatePersistor(); xmlOut.BeginDocument(); persistor.Save(oldObj, xmlOut); xmlOut.EndDocument(); // now read it back mstream.Position = 0; ReadXML xmlIn = new ReadXML(mstream); xmlIn.ReadToTag(); IEncogPersistedObject result = persistor.Load(xmlIn); mstream.Close(); // put the name back to null if we changed it if (replacedName) { oldObj.Name = null; result.Name = null; } return(result); }
/// <inheritdoc/> public void Add(string name, IEncogPersistedObject obj) { this.Contents[name] = obj; BuildDirectory(); }
/// <summary> /// Save the specified object. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlOut">The XML object.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlOut) { Object2XML conv = new Object2XML(); conv.Save(obj, xmlOut); }
/// <summary> /// Delete the specified object. /// </summary> /// <param name="obj">The object to delete.</param> public void Delete(IEncogPersistedObject obj) { Delete(obj.Name); }
/// <summary> /// Save the specified Encog object to an XML writer. /// </summary> /// <param name="obj">The object to save.</param> /// <param name="xmlOut">The XML writer to save to.</param> public void Save(IEncogPersistedObject obj, WriteXML xmlOut) { PersistorUtil.BeginEncogObject( EncogPersistedCollection.TYPE_CONTEXT_LAYER, xmlOut, obj, false); ContextLayer layer = (ContextLayer)obj; xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_NEURONS, layer .NeuronCount); xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_X, layer.X); xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_Y, layer.Y); if (layer.HasBias) { StringBuilder result = new StringBuilder(); NumberList.ToList(CSVFormat.EG_FORMAT, result, layer.BiasWeights); xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_THRESHOLD, result .ToString()); } StringBuilder ctx = new StringBuilder(); NumberList.ToList(CSVFormat.EG_FORMAT, ctx, layer.Context.Data); xmlOut.AddProperty(PROPERTY_CONTEXT, ctx.ToString()); xmlOut.AddProperty(BasicLayerPersistor.PROPERTY_BIAS_ACTIVATION, layer.BiasActivation ); BasicLayerPersistor.SaveActivationFunction(layer.ActivationFunction, xmlOut); xmlOut.EndTag(); }
/// <summary> /// Perform a deep copy. /// Silverlight version. /// </summary> /// <param name="oldObj">The old object.</param> /// <returns>The new object.</returns> static public IEncogPersistedObject DeepCopy(IEncogPersistedObject oldObj) { bool replacedName = false; // encog objects won't save without a name if (oldObj.Name == null) { replacedName = true; oldObj.Name = "temp"; } // now make the copy MemoryStream mstream = new MemoryStream(); WriteXML xmlOut = new WriteXML(mstream); IPersistor persistor = oldObj.CreatePersistor(); xmlOut.BeginDocument(); persistor.Save(oldObj, xmlOut); xmlOut.EndDocument(); // now read it back mstream.Position = 0; ReadXML xmlIn = new ReadXML(mstream); xmlIn.ReadToTag(); IEncogPersistedObject result = persistor.Load(xmlIn); mstream.Close(); // put the name back to null if we changed it if (replacedName) { oldObj.Name = null; result.Name = null; } return result; }