public void Run() { // get iris file from resource stream Assembly assembly = Assembly.GetExecutingAssembly(); var f = assembly.GetManifestResourceStream("HillClimbing.Resources.iris.csv"); StreamReader sr = new StreamReader(f); // load all records into new dataset DataSet ds = DataSet.Load(sr); sr.Close(); Dictionary<string, int> species = ds.EncodeOneOfN(4); // encode specie names using one of n encoding // get a list of records with data and encoded class name List<Record> trainingData = ds.ExtractSupervised(0, 4, 4, 3); Network net = new Network(4, 4, 2); // new RBF network net.Randomize(); // initialize the networks state Scorer score = new Scorer(trainingData); // new scorer HillClimb hc = new HillClimb(net, score, 1.2, 1); // new hill climbing training algorithm Iterate(hc, 100, 0.01); // iterate through hill climbing algorithm QueryOneOfN(net, trainingData, species); // display ideal and actual specie names }
/// <summary> /// Determines a score for a RBF network. /// </summary> /// <param name="net">RBF network to determine a score for.</param> /// <returns>Double that represents the score.</returns> public double Score(Network net) { _errCalc.Clear(); // clear the error calculator foreach(var data in _trainingData) { double[] output = net.ComputeRegression(data.Input); // send input through RBFs in network _errCalc.UpdateError(output, data.Ideals); // updates error based on new outputs } return _errCalc.Calculate(); // calculates MSE error }
/// <summary> /// Goes through each record in the training set and displays the ideal output and the actual output. /// </summary> /// <param name="net">RBF network.</param> /// <param name="trainingData">Training data.</param> /// <param name="specieNames">Specie names.</param> private void QueryOneOfN(Network net, List<Record> trainingData, Dictionary<string, int> species) { // invert the specie list Dictionary<int, string> invSpecies = new Dictionary<int, string>(); foreach(string key in species.Keys) { int value = species[key]; invSpecies[value] = key; } // for each training record foreach(Record rec in trainingData) { double[] output = net.ComputeRegression(rec.Input); // run the input through the RBF network int idealIdx = GetMax(rec.Ideals); // find the index of the ideal name int actualIdx = GetMax(output); // find the index of the actual name Console.WriteLine("Guess: " + invSpecies[actualIdx] + " Actual: " + invSpecies[idealIdx]); } }