public void Load(string fileName, TransferFunction transFunc) { int iWeight = 0; int iThresh = 0; StateParse curState = StateParse.Begin; StreamReader modelFile = new StreamReader(fileName); while (!modelFile.EndOfStream) { string line = modelFile.ReadLine(); if (StateParse.Begin == curState) { if (line.Equals("nStartNodes", StringComparison.OrdinalIgnoreCase)) { curState = StateParse.StartNodes; } } else if (StateParse.StartNodes == curState) { if (line.Equals("nEndNodes", StringComparison.OrdinalIgnoreCase)) { curState = StateParse.EndNodes; } else { nInputs = int.Parse(line); } } else if (StateParse.EndNodes == curState) { if (line.Equals("nWeights", StringComparison.OrdinalIgnoreCase)) { curState = StateParse.NWeights; } else { this.nOutputs = int.Parse(line); this.outputs = new float[nOutputs]; this.weights = new float[nOutputs][]; this.thresholds = new float[nOutputs]; for (int i = 0; i < nOutputs; i++) { weights[i] = new float[nInputs]; } } } else if (StateParse.NWeights == curState) { int.Parse(line); // == nOutputs * nInputs curState = StateParse.Weights; } else if (StateParse.Weights == curState) { if (line.Equals("thresholds", StringComparison.OrdinalIgnoreCase)) { curState = StateParse.Thresholds; } else { float w = float.Parse(line); int iOut = iWeight / nInputs; int iIn = iWeight % nInputs; weights[iOut][iIn] = w; iWeight++; } } else if (StateParse.Thresholds == curState) { float t = float.Parse(line); this.thresholds[iThresh++] = t; } } modelFile.Close(); this.transFunc = transFunc; }
//construct a node for multiclass (> 2) classification - there are cClass nodes for cClass classification model //where each node corresponding to each class; i.e., the (iClass) node sums all and only the regression trees for that (iClass) class public Node(int layerID, int nodeID, int cInputs, int cClass, int iClass) { this.layerID = layerID; this.nodeID = nodeID; this.Weights = new float[cInputs + 1]; this.Weights[0] = 0.0F; for (int i = 1; i < cInputs + 1; i++) { this.Weights[i] = 0.0F; if ((i-1) % cClass == iClass) //iClass is zero based but i is one based { this.Weights[i] = 1.0F; } } this.transFunc = TransferFunction.Linear; }
//construct a new node based on corresponding node in the sub-model to handle the additional boosted regression trees public Node(int layerID, int nodeID, int cInputs, Node curNode, bool fKeepCur, bool fKeepNew) { this.layerID = layerID; this.nodeID = nodeID; this.Weights = new float[cInputs+1]; for (int i = 0; i < cInputs+1; i++) { this.Weights[i] = 0.0F; if (i < curNode.Weights.Length) { if (fKeepCur) { this.Weights[i] = curNode.Weights[i]; } } else { if (fKeepNew) { this.Weights[i] = 1.0F; } } } if (fKeepCur) { this.transFunc = curNode.transFunc; } else { this.transFunc = TransferFunction.Linear; } }
private float[] Weights; //Weights[0] is the bias #endregion Fields #region Constructors //construct node from .ini configuration/description public Node(DataSection dataSec) { string[] fields = dataSec.Name.Split(':'); this.layerID = int.Parse(fields[1]); this.nodeID = int.Parse(fields[2]); List<float> weightList = new List<float>(); List<int> idxList = new List<int>(); for (int i = 0; i < dataSec.Data.Length; i++) { if (dataSec.Data[i].StartsWith("Weight:")) { fields = dataSec.Data[i].Split(':'); fields = fields[1].Split('='); idxList.Add(int.Parse(fields[0])); weightList.Add(float.Parse(fields[1])); } else if (dataSec.Data[i].StartsWith("Type=")) { fields = dataSec.Data[i].Split('='); if (string.Compare(fields[1], "Tanh", StringComparison.CurrentCultureIgnoreCase) == 0) { this.transFunc = TransferFunction.Tanh; } else if (string.Compare(fields[1], "Sigmoid", StringComparison.CurrentCultureIgnoreCase) == 0) { this.transFunc = TransferFunction.Sigmoid; } else if (string.Compare(fields[1], "Linear", StringComparison.CurrentCultureIgnoreCase) == 0) { this.transFunc = TransferFunction.Linear; } else if (string.Compare(fields[1], "Logistic", StringComparison.CurrentCultureIgnoreCase) == 0) { this.IsLogistic = true; } } } this.Weights = new float[weightList.Count]; for (int i = 0; i < this.Weights.Length; i++) { this.Weights[idxList[i]] = weightList[i]; } }