Exemplo n.º 1
0
        /// <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();
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
 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 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();
 }
 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>
        ///
        /// </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 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();
     }
 }
Exemplo n.º 8
0
        /// <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 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 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 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();
     }
 }
        /// <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>
        /// 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>
        /// 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++;
            }
        }
Exemplo n.º 15
0
        /// <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>
        /// 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();
        }
Exemplo n.º 17
0
        /// <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();
        }
Exemplo n.º 18
0
        /// <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);
        }
Exemplo n.º 19
0
        /// <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();
            }
        }