private void GenerateNetwork() { //create neurons var output = new FiringNeuron[Options.LayerStructure.Length, Options.MaxLayerDensity]; for (var layer = 1; layer < Options.LayerStructure.Length; layer++) { for (var neuron = 0; neuron < Options.LayerStructure[layer]; neuron++) { output[layer, neuron] = new FiringNeuron(new Coordinate(layer, neuron), Options.LayerStructure[layer - 1], new SigmoidActivationFunction()); } } NeuronLayers = output; _proposedNeurons = new IProposedNeuron[Options.LayerStructure.Length, Options.MaxLayerDensity]; }
public double GetActivationSlopeAt(FiringNeuron neuron) { var tanh = neuron.Output; return(1 - tanh * tanh); }
public override double GetActivationSlopeAt(FiringNeuron neuron) => neuron.Output * (1 - neuron.Output);
public override double GetActivationSlopeAt(FiringNeuron neuron) { double y = neuron.Output; return(y * (1 - y)); }
// This is the derivative after modifying the loss function. public override double GetActivationSlopeAt(FiringNeuron neuron) => 1;
public override double GetActivationSlopeAt(FiringNeuron neuron) => neuron.TotalInput > 0 ? 1 : .01;
public abstract double GetActivationSlopeAt(FiringNeuron neuron);
public static void Main() { int totalSamples = 1000; double learningRate = 0.02; #region Manufacture some data var random = new Random(); var data = ( from i in Enumerable.Range(0, totalSamples) let input1 = random.Next(2) let input2 = random.Next(2) select new { input1, input2, // A NAND gate should be 0 only when both inputs are 1 DesiredOutput = input1 == 1 && input2 == 1 ? 0 : 1 }).ToArray(); #endregion // Split the data into training and testing sets int trainingCount = totalSamples * 8 / 10; var trainingSet = data.Take(trainingCount); var testingSet = data.Skip(trainingCount); var neuron = new Neuron(); var firingNeuron = new FiringNeuron(neuron); // Train foreach (var sample in trainingSet) { firingNeuron.Learn(sample.input1, sample.input2, sample.DesiredOutput, learningRate); } // Test var testResults = ( from sample in testingSet let actualOutput = firingNeuron.Fire(sample.input1, sample.input2) let success = actualOutput >= .5 == (sample.DesiredOutput == 1) let error = actualOutput - sample.DesiredOutput group error by success).ToArray(); // Report results neuron.PrintDump(); testResults .Select(t => new { Successful = t.Key, Count = t.Count() }) .PrintDump(); testResults .SelectMany(t => t) .Average(t => Math.Abs(t)) .PrintDump(); testResults .SelectMany(t => t) .GroupBy(Loss => Math.Round(Loss, 2)) .Select(g => new { Error = g.Key, Count = g.Count() }) .OrderBy(g => g.Error) .PrintDump(); }