/// <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> /// Load the specified Encog object from an XML reader. /// </summary> /// <param name="xmlIn">The XML reader to use.</param> /// <returns>The loaded object.</returns> public IEncogPersistedObject Load(ReadXML xmlIn) { int neuronCount = 0; int x = 0; int y = 0; double biasActivation = 1; String threshold = null; IActivationFunction activation = null; String end = xmlIn.LastTag.Name; String context = null; while (xmlIn.ReadToTag()) { if (xmlIn.IsIt(BasicLayerPersistor.TAG_ACTIVATION, true)) { xmlIn.ReadToTag(); String type = xmlIn.LastTag.Name; activation = BasicLayerPersistor.LoadActivation(type, xmlIn); } else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_NEURONS, true)) { neuronCount = xmlIn.ReadIntToTag(); } else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_X, true)) { x = xmlIn.ReadIntToTag(); } else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_Y, true)) { y = xmlIn.ReadIntToTag(); } else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_THRESHOLD, true)) { threshold = xmlIn.ReadTextToTag(); } else if (xmlIn.IsIt(PROPERTY_CONTEXT, true)) { context = xmlIn.ReadTextToTag(); } else if (xmlIn.IsIt(BasicLayerPersistor.PROPERTY_BIAS_ACTIVATION, true)) { biasActivation = double.Parse(xmlIn.ReadTextToTag()); } else if (xmlIn.IsIt(end, false)) { break; } } if (neuronCount > 0) { ContextLayer layer; if (threshold == null) { layer = new ContextLayer(activation, false, neuronCount); } else { double[] t = NumberList.FromList(CSVFormat.EG_FORMAT, threshold); layer = new ContextLayer(activation, true, neuronCount); for (int i = 0; i < t.Length; i++) { layer.BiasWeights[i] = t[i]; } } if (context != null) { double[] c = NumberList.FromList(CSVFormat.EG_FORMAT, context); for (int i = 0; i < c.Length; i++) { layer.Context[i] = c[i]; } } layer.X = x; layer.Y = y; layer.BiasActivation = biasActivation; return(layer); } return(null); }