/// <summary> /// Handle an item. /// </summary> /// <param name="xmlin">The XML input object.</param> public void HandleItem(ReadXML xmlin) { String name = xmlin.LastTag.GetAttributeValue( TrainingContinuationPersistor.ATTRIBUTE_NAME); String str = xmlin.ReadTextToTag(); double[] list = NumberList.FromList(CSVFormat.EG_FORMAT, str); this.current[name] = list; }
/// <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; }
/// <summary> /// Load the properties. /// </summary> /// <param name="xmlIn">The XML object.</param> private void HandleProperties(ReadXML xmlIn) { String end = xmlIn.LastTag.Name; while (xmlIn.ReadToTag()) { if (xmlIn.IsIt(BasicNetworkPersistor.TAG_PROPERTY, true)) { String name = xmlIn.LastTag.GetAttributeValue( BasicNetworkPersistor.ATTRIBUTE_NAME); String value = xmlIn.ReadTextToTag(); this.currentNetwork.SetProperty(name, value); } if (xmlIn.IsIt(end, false)) { break; } } }
/// <summary> /// Load a matrix from the reader. /// </summary> /// <param name="xmlIn">The XML reader.</param> /// <returns>The loaded matrix.</returns> public static Matrix LoadMatrix(ReadXML xmlIn) { int rows = xmlIn.LastTag.GetAttributeInt( PersistorUtil.ATTRIBUTE_MATRIX_ROWS); int cols = xmlIn.LastTag.GetAttributeInt( PersistorUtil.ATTRIBUTE_MATRIX_COLS); Matrix matrix = new Matrix(rows, cols); int row = 0; String end = xmlIn.LastTag.Name; while (xmlIn.ReadToTag()) { if (xmlIn.IsIt(end, false)) { break; } if (xmlIn.IsIt(PersistorUtil.ROW, true)) { String str = xmlIn.ReadTextToTag(); double[] d = NumberList.FromList(CSVFormat.EG_FORMAT, str); for (int col = 0; col < d.Length; col++) { matrix[row, col] = d[col]; } row++; } } return matrix; }
/// <summary> /// Load the neural logic object. /// </summary> /// <param name="xmlIn">The XML object.</param> private void HandleLogic(ReadXML xmlIn) { String value = xmlIn.ReadTextToTag(); if (value.Equals("ART1Logic")) { this.currentNetwork.Logic = new ART1Logic(); } else if (value.Equals("BAMLogic")) { this.currentNetwork.Logic = new BAMLogic(); } else if (value.Equals("BoltzmannLogic")) { this.currentNetwork.Logic = new BoltzmannLogic(); } else if (value.Equals("FeedforwardLogic")) { this.currentNetwork.Logic = new FeedforwardLogic(); } else if (value.Equals("HopfieldLogic")) { this.currentNetwork.Logic = new HopfieldLogic(); } else if (value.Equals("SimpleRecurrentLogic")) { this.currentNetwork.Logic = new SimpleRecurrentLogic(); } else { INeuralLogic logic = (INeuralLogic)Assembly.GetExecutingAssembly().CreateInstance(value); this.currentNetwork.Logic = logic; } }
/// <summary> /// Load the data from a model. /// </summary> /// <param name="xmlin">Where to read the data from.</param> /// <param name="model">The model to load data into.</param> private void HandleData(ReadXML xmlin, svm_model model) { int i = 0; int m = model.nr_class - 1; int l = model.l; model.sv_coef = EngineArray.AllocateDouble2D(m, l); model.SV = new svm_node[l][]; while (xmlin.ReadToTag()) { if (xmlin.IsIt(SVMNetworkPersistor.TAG_ROW, true)) { String line = xmlin.ReadTextToTag(); String[] st = xmlin.ReadTextToTag().Split(','); for (int k = 0; k < m; k++) model.sv_coef[k][i] = Double.Parse(st[i]); int n = st.Length / 2; model.SV[i] = new svm_node[n]; int idx = 0; for (int j = 0; j < n; j++) { model.SV[i][j] = new svm_node(); model.SV[i][j].index = int.Parse(st[idx++]); model.SV[i][j].value_Renamed = Double.Parse(st[idx++]); } i++; } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_DATA, false)) { break; } } }
/// <summary> /// Handle a model. /// </summary> /// <param name="xmlin">Where to read the model from.</param> /// <param name="model">Where to load the model into.</param> private void HandleModel(ReadXML xmlin, svm_model model) { while (xmlin.ReadToTag()) { if (xmlin.IsIt(SVMNetworkPersistor.TAG_TYPE_SVM, true)) { int i = EngineArray.FindStringInArray( SVMNetworkPersistor.svm_type_table, xmlin.ReadTextToTag()); model.param.svm_type = i; } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_DEGREE, true)) { model.param.degree = int.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_GAMMA, true)) { model.param.gamma = double.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_COEF0, true)) { model.param.coef0 = double.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_NUMCLASS, true)) { model.nr_class = int.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_TOTALSV, true)) { model.l = int.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_RHO, true)) { int n = model.nr_class * (model.nr_class - 1) / 2; model.rho = new double[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) model.rho[i] = double.Parse(st[i]); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_LABEL, true)) { int n = model.nr_class; model.label = new int[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) model.label[i] = int.Parse(st[i]); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_PROB_A, true)) { int n = model.nr_class * (model.nr_class - 1) / 2; model.probA = new double[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) model.probA[i] = Double.Parse(st[i]); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_PROB_B, true)) { int n = model.nr_class * (model.nr_class - 1) / 2; model.probB = new double[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) model.probB[i] = Double.Parse(st[i]); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_NSV, true)) { int n = model.nr_class; model.nSV = new int[n]; String[] st = xmlin.ReadTextToTag().Split(','); for (int i = 0; i < n; i++) model.nSV[i] = int.Parse(st[i]); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_TYPE_KERNEL, true)) { int i = EngineArray.FindStringInArray( SVMNetworkPersistor.kernel_type_table, xmlin .ReadTextToTag()); model.param.kernel_type = i; } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_DATA, true)) { HandleData(xmlin, model); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_MODEL, false)) { break; } } }
/// <summary> /// Load the SVM network. /// </summary> /// <param name="xmlin">Where to read it from.</param> /// <returns>The loaded object.</returns> public IEncogPersistedObject Load(ReadXML xmlin) { SVMNetwork result = null; int input = -1, output = -1; String name = xmlin.LastTag.Attributes[ EncogPersistedCollection.ATTRIBUTE_NAME]; String description = xmlin.LastTag.Attributes[ EncogPersistedCollection.ATTRIBUTE_DESCRIPTION]; while (xmlin.ReadToTag()) { if (xmlin.IsIt(SVMNetworkPersistor.TAG_INPUT, true)) { input = int.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_OUTPUT, true)) { output = int.Parse(xmlin.ReadTextToTag()); } else if (xmlin.IsIt(SVMNetworkPersistor.TAG_MODELS, true)) { result = new SVMNetwork(input, output, false); HandleModels(xmlin, result); } else if (xmlin.IsIt(EncogPersistedCollection.TYPE_SVM, false)) { break; } } result.Name = name; result.Description = description; return result; }
private IRadialBasisFunction LoadRBF(String name, ReadXML xmlin) { String clazz = ReflectionUtil.ResolveEncogClass(name); if (clazz == null) { throw new NeuralNetworkError("Unknown RBF function type: " + name); } IRadialBasisFunction result = (IRadialBasisFunction)ReflectionUtil.LoadObject(clazz); while (xmlin.ReadToTag()) { if (xmlin.IsIt(name, false)) { break; } else if (xmlin.IsIt(PROPERTY_CENTERS, true)) { String str = xmlin.ReadTextToTag(); double[] centers = NumberList.FromList(CSVFormat.EG_FORMAT, str); result.Centers = centers; } else if (xmlin.IsIt(PROPERTY_PEAK, true)) { String str = xmlin.ReadTextToTag(); double d = Double.Parse(str); result.Peak = d; } else if (xmlin.IsIt(PROPERTY_WIDTH, true)) { String str = xmlin.ReadTextToTag(); double d = Double.Parse(str); result.Width = d; } } return result; }