/// <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> /// 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_TEXT, xmlout, obj, true); TextData text = (TextData)obj; xmlout.AddCDATA(text.Text); 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(); }
public void TestWrite() { var ms = new MemoryStream(); var write = new WriteXML(ms); write.BeginDocument(); write.AddAttribute("name", "value"); write.BeginTag("tag"); write.EndTag(); write.EndDocument(); ms.Close(); var enc = new ASCIIEncoding(); string str = enc.GetString(ms.ToArray()); Assert.AreEqual("<tag name=\"value\"></tag>", str); }
/// <summary> /// Save the layers to the specified XML writer. /// </summary> /// <param name="xmlOut">The XML writer.</param> private void SaveLayers(WriteXML xmlOut) { int current = 1; foreach (ILayer layer in this.currentNetwork.Structure.Layers) { xmlOut.AddAttribute(BasicNetworkPersistor.ATTRIBUTE_ID, "" + current); xmlOut.BeginTag(BasicNetworkPersistor.TAG_LAYER); IPersistor persistor = layer.CreatePersistor(); persistor.Save(layer, xmlOut); xmlOut.EndTag(); this.layer2index[layer] = current; current++; } }
/// <summary> /// Save the neural properties. /// </summary> /// <param name="xmlOut">The xml object.</param> private void SaveProperties(WriteXML xmlOut) { // save any properties xmlOut.BeginTag(BasicNetworkPersistor.TAG_PROPERTIES); foreach (String key in this.currentNetwork.Properties.Keys) { String value = this.currentNetwork.Properties[key]; xmlOut.AddAttribute(BasicNetworkPersistor.ATTRIBUTE_NAME, key); xmlOut.BeginTag(BasicNetworkPersistor.TAG_PROPERTY); xmlOut.AddText(value.ToString()); xmlOut.EndTag(); } xmlOut.EndTag(); }
/// <summary> /// Save the neural logic object. /// </summary> /// <param name="xmlOut">The XML object.</param> private void SaveLogic(WriteXML xmlOut) { xmlOut.BeginTag(BasicNetworkPersistor.TAG_LOGIC); INeuralLogic logic = this.currentNetwork.Logic; if (logic is FeedforwardLogic || logic is SimpleRecurrentLogic || logic is BoltzmannLogic || logic is ART1Logic || logic is BAMLogic || logic is HopfieldLogic) { xmlOut.AddText(logic.GetType().Name); } else xmlOut.AddText(logic.GetType().Name); xmlOut.EndTag(); }
/// <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 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 specified matrix. /// </summary> /// <param name="matrix">The matrix to save.</param> /// <param name="xmlOut">The XML writer.</param> public static void SaveMatrix(Matrix matrix, WriteXML xmlOut) { xmlOut.AddAttribute(PersistorUtil.ATTRIBUTE_MATRIX_ROWS, "" + matrix.Rows); xmlOut.AddAttribute(PersistorUtil.ATTRIBUTE_MATRIX_COLS, "" + matrix.Cols); xmlOut.BeginTag("Matrix"); CSVFormat format = CSVFormat.EG_FORMAT; for (int row = 0; row < matrix.Rows; row++) { StringBuilder builder = new StringBuilder(); for (int col = 0; col < matrix.Cols; col++) { if (col > 0) { builder.Append(','); } double d = matrix[row, col]; builder.Append(format.Format(d, 20)); } xmlOut.BeginTag(PersistorUtil.ROW); xmlOut.AddText(builder.ToString()); xmlOut.EndTag(); } xmlOut.EndTag(); }
/// <summary> /// Create a writer for the specified location. /// </summary> /// <param name="location">The location.</param> public PersistWriter(IPersistenceLocation location) { this.fileOutput = location.CreateStream(FileMode.OpenOrCreate); this.xmlOut = new WriteXML(this.fileOutput); }
/// <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; }
/// <summary> /// Save the activation function. /// </summary> /// <param name="activationFunction">The activation function.</param> /// <param name="xmlOut">The XML.</param> public static void SaveActivationFunction( IActivationFunction activationFunction, WriteXML xmlOut) { if (activationFunction != null) { xmlOut.BeginTag(BasicLayerPersistor.TAG_ACTIVATION); xmlOut.BeginTag(activationFunction.GetType().Name); String[] names = activationFunction.ParamNames; for (int i = 0; i < names.Length; i++) { String str = names[i]; double d = activationFunction.Params[i]; xmlOut.AddAttribute(str, "" + CSVFormat.EG_FORMAT.Format(d, 10)); } xmlOut.EndTag(); xmlOut.EndTag(); } }
/// <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> /// Copy an XML object, no need to know what it contains, just /// copy it. This way we will not damage unknown objects during /// a merge. /// </summary> /// <param name="xmlOut">The XML writer.</param> /// <param name="replace"></param> private void CopyXML(WriteXML xmlOut, IDictionary<String, String> replace) { StringBuilder text = new StringBuilder(); int depth = 0; int ch; CopyAttributes(xmlOut, replace); String contain = this.xmlIn.LastTag.Name; xmlOut.BeginTag(contain); while ((ch = this.xmlIn.Read()) != -1) { Tag.Type type = this.xmlIn.LastTag.TagType; if (ch == 0) { if (type == Tag.Type.BEGIN) { if (text.Length > 0) { xmlOut.AddText(text.ToString()); text.Length = 0; } CopyAttributes(xmlOut, null); xmlOut.BeginTag(this.xmlIn.LastTag.Name); depth++; } else if (type == Tag.Type.END) { if (text.Length > 0) { xmlOut.AddText(text.ToString()); text.Length = 0; } if (depth == 0) { break; } else { xmlOut.EndTag(xmlIn.LastTag.Name); } depth--; } } else { text.Append((char)ch); } } xmlOut.EndTag(contain); }
/// <summary> /// Modify the properties of this object. /// </summary> /// <param name="xmlOut">The XML writer.</param> /// <param name="targetName">The name of the object to change.</param> /// <param name="newName">The new name of this object.</param> /// <param name="newDesc">The new description of this object.</param> public void SaveModified(WriteXML xmlOut, String targetName, String newName, String newDesc) { AdvanceObjectsCollection(); while (this.xmlIn.ReadToTag()) { Tag.Type type = this.xmlIn.LastTag.TagType; if (type == Tag.Type.BEGIN) { String name = this.xmlIn.LastTag.GetAttributeValue( PersistReader.ATTRIBUTE_NAME); if (name.Equals(targetName)) { IDictionary<String, String> replace = new Dictionary<String, String>(); replace.Add("name", newName); replace.Add("description", newDesc); CopyXML(xmlOut, replace); } else { CopyXML(xmlOut, null); } } } }
private void SaveRBF(WriteXML xmlout, RadialBasisFunctionLayer layer) { xmlout.BeginTag(RadialBasisFunctionLayerPersistor.PROPERTY_RBF); foreach (IRadialBasisFunction rbf in layer.RadialBasisFunction) { xmlout.BeginTag(rbf.GetType().Name); xmlout.AddProperty(PROPERTY_CENTERS, rbf.Centers, rbf.Centers.Length); xmlout.AddProperty(PROPERTY_PEAK, rbf.Peak); xmlout.AddProperty(PROPERTY_WIDTH, rbf.Width); xmlout.EndTag(); } xmlout.EndTag(); }
/// <summary> /// Save the synapses to the specified XML writer. /// </summary> /// <param name="xmlOut">The XML writer.</param> private void SaveSynapses(WriteXML xmlOut) { foreach (ISynapse synapse in this.currentNetwork.Structure.Synapses) { xmlOut.AddAttribute(BasicNetworkPersistor.ATTRIBUTE_FROM, "" + this.layer2index[synapse.FromLayer]); xmlOut.AddAttribute(BasicNetworkPersistor.ATTRIBUTE_TO, "" + this.layer2index[synapse.ToLayer]); xmlOut.BeginTag(BasicNetworkPersistor.TAG_SYNAPSE); IPersistor persistor = synapse.CreatePersistor(); persistor.Save(synapse, xmlOut); xmlOut.EndTag(); } }
private void SaveTags(WriteXML outXML) { // save any properties outXML.BeginTag(BasicNetworkPersistor.TAG_TAGS); foreach (String key in this.currentNetwork.LayerTags.Keys) { ILayer value = this.currentNetwork.LayerTags[key]; outXML.AddAttribute(BasicNetworkPersistor.ATTRIBUTE_NAME, key); outXML.AddAttribute(BasicNetworkPersistor.ATTRIBUTE_LAYER, "" + layer2index[value]); outXML.BeginTag(BasicNetworkPersistor.TAG_TAG); outXML.EndTag(); } outXML.EndTag(); }
/// <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) { 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> /// 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> /// Copy all of the attributes to the writer. /// </summary> /// <param name="xmlOut">The XML writer.</param> /// <param name="replace">A map of attributes to replace. This allows /// new values to be specified for select attributes.</param> private void CopyAttributes(WriteXML xmlOut, IDictionary<String, String> replace) { foreach (String key in this.xmlIn.LastTag.Attributes.Keys) { String value = this.xmlIn.LastTag.GetAttributeValue(key); if ((replace != null) && replace.ContainsKey(key)) { value = replace[key]; } xmlOut.AddAttribute(key, value); } }
/// <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> /// /// </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 all objects to the specified steam, skip the one specified by the /// skip parameter. Do not attempt to understand the structure, just copy. /// </summary> /// <param name="xmlOut">The XML writer to save the objects to.</param> /// <param name="skip">The object to skip.</param> public void SaveTo(WriteXML xmlOut, String skip) { AdvanceObjectsCollection(); while (this.xmlIn.ReadToTag()) { Tag.Type type = this.xmlIn.LastTag.TagType; if (type == Tag.Type.BEGIN) { String name = this.xmlIn.LastTag.GetAttributeValue( PersistReader.ATTRIBUTE_NAME); if (name.Equals(skip)) { SkipObject(); } else { CopyXML(xmlOut, null); } } } }
/// <summary> /// Save items. /// </summary> /// <param name="xmlout">The XML output object.</param> public void SaveItems(WriteXML xmlout) { foreach (String key in this.current.Contents.Keys) { xmlout.AddAttribute(TrainingContinuationPersistor.ATTRIBUTE_NAME, key); xmlout.BeginTag(TrainingContinuationPersistor.TAG_ITEM); double[] value = (double[])this.current[key]; StringBuilder result = new StringBuilder(); NumberList.ToList(CSVFormat.EG_FORMAT, result, value); xmlout.AddText(result.ToString()); xmlout.EndTag(); } }
/// <summary> /// Save a model. /// </summary> /// <param name="xmlout">Where to save a model to.</param> /// <param name="model">The model to save to.</param> public static void SaveModel(WriteXML xmlout, svm_model model) { if (model != null) { xmlout.BeginTag(SVMNetworkPersistor.TAG_MODEL); svm_parameter param = model.param; xmlout.AddProperty(SVMNetworkPersistor.TAG_TYPE_SVM, svm_type_table[param.svm_type]); xmlout.AddProperty(SVMNetworkPersistor.TAG_TYPE_KERNEL, kernel_type_table[param.kernel_type]); if (param.kernel_type == svm_parameter.POLY) { xmlout.AddProperty(SVMNetworkPersistor.TAG_DEGREE, param.degree); } if (param.kernel_type == svm_parameter.POLY || param.kernel_type == svm_parameter.RBF || param.kernel_type == svm_parameter.SIGMOID) { xmlout.AddProperty(SVMNetworkPersistor.TAG_GAMMA, param.gamma); } if (param.kernel_type == svm_parameter.POLY || param.kernel_type == svm_parameter.SIGMOID) { xmlout.AddProperty(SVMNetworkPersistor.TAG_COEF0, param.coef0); } int nr_class = model.nr_class; int l = model.l; xmlout.AddProperty(SVMNetworkPersistor.TAG_NUMCLASS, nr_class); xmlout.AddProperty(SVMNetworkPersistor.TAG_TOTALSV, l); xmlout.AddProperty(SVMNetworkPersistor.TAG_RHO, model.rho, nr_class * (nr_class - 1) / 2); xmlout.AddProperty(SVMNetworkPersistor.TAG_LABEL, model.label, nr_class); xmlout.AddProperty(SVMNetworkPersistor.TAG_PROB_A, model.probA, nr_class * (nr_class - 1) / 2); xmlout.AddProperty(SVMNetworkPersistor.TAG_PROB_B, model.probB, nr_class * (nr_class - 1) / 2); xmlout.AddProperty(SVMNetworkPersistor.TAG_NSV, model.nSV, nr_class); xmlout.BeginTag(SVMNetworkPersistor.TAG_DATA); double[][] sv_coef = model.sv_coef; svm_node[][] SV = model.SV; StringBuilder line = new StringBuilder(); for (int i = 0; i < l; i++) { line.Length = 0; for (int j = 0; j < nr_class - 1; j++) line.Append(sv_coef[j][i] + " "); svm_node[] p = SV[i]; //if (param.kernel_type == svm_parameter.PRECOMPUTED) //{ // line.Append("0:" + (int) (p[0].value)); //} //else for (int j = 0; j < p.Length; j++) line.Append(p[j].index + ":" + p[j].value_Renamed + " "); xmlout.AddProperty(SVMNetworkPersistor.TAG_ROW, line.ToString()); } xmlout.EndTag(); xmlout.EndTag(); } }