public void LearnOneExample(Example example) { if (savedExamples.Count >= n) { //delete the oldest example savedExamples.RemoveAt(1); } savedExamples.Add(example); }
public void PropogateInput(Example example) { List<BestExample> best = new List<BestExample>(); double maxBest = 0.0; foreach (Example e in savedExamples) { double d = distance_sq(e, example); if (best.Count < k) { best.Add(new BestExample(e, d)); if (d > maxBest) { maxBest = d; } } else if (d < maxBest) { // at this point, the Best list contains k elements best.Sort(); maxBest = best[k - 1].distance; best[k - 1] = new BestExample(e, d); } } if (best.Count < 1) throw new LearnerNotReady(); // perform a weighted average List<Double> result = new List<Double>(); for (int i = 0; i < best[0].example.Labels.Count; i++) { result.Add(0.0); } double sumWeight = 0.0; foreach (BestExample be in best) { double thisWeight = 1 / (1 + be.distance); for (int i = 0; i < result.Count; i++) { result[i] += be.example.Labels[i] * thisWeight; } sumWeight += thisWeight; } for (int i = 0; i < result.Count; i++) { result[i] /= sumWeight; } example.Predictions = result; }
public override void LearnOneExample(Example example) { Network.LearnOneExample(example); }
public abstract void LearnOneExample(Example example);
public override void LearnOneExample(Example example) { learner.LearnOneExample(example); }
private void PropogateErrors(Example example) { AssertValidLabels(example); Neuron.UnfeedAll(Neurons); int label = 0; foreach (Neuron neuron in Outputs) { neuron.UpdateErrorTerm(example.Labels[label++]); neuron.Fed = true; } foreach (Neuron neuron in Neurons) recur_BackPropogation(neuron); }
private void AssertValidLabels(Example example) { if (example.Labels.Count != Outputs.Count) throw new Exception("The number of labels must match the number of Output Neurons.\r\n" + "Labels: " + example.Features.Count + " Outputs: " + Inputs.Count); }
private void AssertValidForTraining(Example example) { AssertValidFeatures(example); AssertValidLabels(example); }
private void AssertValidFeatures(Example example) { if (example.Features.Count != Inputs.Count) throw new Exception("The number of features must match the number of Input Neurons.\r\n" + "Features: " + example.Features.Count + " Inputs: " + Inputs.Count); }
/// <summary> /// Propogates input through network (FeedForward). Updates example.predictions. /// </summary> public void PropogateInput(Example example) { AssertValidFeatures(example); Neuron.UnfeedAll(Neurons); int feature = 0; foreach (Neuron neuron in Inputs) { neuron.Value = example.Features[feature++]; neuron.Fed = true; } foreach (Neuron neuron in Constants) { neuron.Value = 1; neuron.Fed = true; } foreach (Neuron neuron in Neurons) recur_FeedForward(neuron); example.Predictions = Outputs.Select(o => o.Value).ToList(); }
public void LearnOneExample(Example example) { PropogateInput(example); PropogateErrors(example); UpdateWeights(); }
private double distance_sq(Example e1, Example e2) { double sum = 0.0; Debug.Assert(e1.Features.Count == e2.Features.Count); for (int i = 0; i < e1.Features.Count; i++) { double x1 = e1.Features[i]; double x2 = e2.Features[i]; sum += Math.Pow(x1 - x2, 2.0); } return sum; }
public BestExample(Example e, double d) { example = e; distance = d; }