public double[] Train(double[] inputs, double trainSpeed = 0.05) { if (inputs.Length != InputNeurons.Count) { throw new ArgumentException(); } for (int i = 0; i < InputNeurons.Count; i++) { InputNeurons[i].Value = inputs[i]; } Parallel.ForEach(OutputNeurons, x => x.Calculate()); var winner = OutputNeurons.Select( x => new { Neuron = x, Value = x.Dendrites.Sum(item => Math.Abs(item.Neuron.Value - item.Weight)) * (Wins.ContainsKey(x) ? Wins[x] : 1) }) .OrderBy(x => x.Value) .First(); Wins[winner.Neuron] = Wins.ContainsKey(winner.Neuron) ? Wins[winner.Neuron] + 1 : 2; foreach (var dendrite in winner.Neuron.Dendrites) { dendrite.Weight = dendrite.Weight + trainSpeed * (dendrite.Neuron.Value - dendrite.Weight); } return(OutputNeurons.Select(x => x.Value).ToArray()); }
public void AddOutputNeuronAndMesh(WorkingNeuron neuron) { OutputNeurons.Add(neuron); foreach (WorkingNeuron wn in LastHiddenLayer) { neuron.AddNeuronConnection(new Connection(wn, 0)); } }
public Genome(Genome target, List <Gene> otherGenes = null) { SpeciesId = target.SpeciesId; Fitness = target.Fitness; CreateNetwork(target.InputCount, target.OutputCount, otherGenes ?? target.Genes); Assert.AreEqual(InputNeurons.Count(), target.InputCount); Assert.AreEqual(OutputNeurons.Count(), target.OutputCount); }
public IEnumerable <double> CalculateOutputs() { foreach (var cachedNeuron in AllNeurons.OfType <ACachedNeuron>()) { cachedNeuron.ResetCache(); } return(OutputNeurons.Select(o => o.Value)); }
/// <summary> /// Saves the neural network as a .ann file. /// </summary> /// <param name="name">The .ann file name.</param> /// <returns>The .ann file.</returns> public AnnFile Save(string name) { var map = ComputeNodeIdMap(); return(new AnnFile { Name = name, Activation = Activation, InputNeurons = InputNeurons.Select(neuron => (uint)map[neuron.ID]).ToList(), OutputNeurons = OutputNeurons.Select(neuron => (uint)map[neuron.ID]).ToList(), AdjacencyMatrix = AdjacencyMatrix }); }
public double[] Process(double[] inputs) { if (inputs.Length != InputNeurons.Count) { throw new ArgumentException(); } for (int i = 0; i < InputNeurons.Count; i++) { InputNeurons[i].Value = inputs[i]; } foreach (var outputNeuron in OutputNeurons) { outputNeuron.Calculate(); } return(OutputNeurons.Select(x => x.Value).ToArray()); }
public Genome(int inputCount, int outputCount, float geneWeightRandomVal = 0) { if (inputCount < 1 || outputCount < 1) { throw new System.ArgumentException("Invalid initialization", "inputCount or outputCount"); } SpeciesId = null; Fitness = 0; InitStartingNeurons(inputCount, outputCount); InitStartingGenes(geneWeightRandomVal); Assert.AreEqual(InputNeurons.Count(), inputCount); Assert.AreEqual(OutputNeurons.Count(), outputCount); Assert.AreEqual(Neurons.Count, inputCount + outputCount + 1); Assert.AreEqual(Genes.Count, inputCount * outputCount); }
/// <summary> /// Create a network from the given genes without any prerequesties. /// </summary> /// <param name="targetGenes"></param> private void CreateNetwork(int inputCount, int outputCount, IEnumerable <Gene> targetGenes) { Neurons = new List <Neuron>(); Genes = new List <Gene>(targetGenes.Count()); AddStartingNeurons(inputCount, outputCount); foreach (var gene in targetGenes) { Genes.Add(ForceConnection(gene)); } InputNeurons = Neurons.Where(x => x.Type == ENeuronType.Input); OutputNeurons = Neurons.Where(x => x.Type == ENeuronType.Output); Debug.Assert(Genes.Count == targetGenes.Count()); Debug.Assert(InputNeurons.Count() == inputCount); Debug.Assert(OutputNeurons.Count() == outputCount); }
/// <summary> /// Initialize the network with the starting genes. /// All inputs, but the bias, will be connected to output neurons. /// </summary> private void InitStartingGenes(float geneWeightRandomVal) { Genes = new List <Gene>(InputCount * OutputCount); var inputNeurons = InputNeurons.ToArray(); var outputNeurons = OutputNeurons.ToArray(); for (int i = 0; i < InputCount; i++) { for (int j = 0; j < OutputCount; j++) { Gene newGene = new Gene( inputNeurons[i], outputNeurons[j], Random.Range(-geneWeightRandomVal, geneWeightRandomVal) ); Genes.Add(newGene); inputNeurons[i].OutGenes.Add(newGene); outputNeurons[j].InGenes.Add(newGene); } } }
private void Initialize(int inputs, int outputs) { var random = new Random(); for (int i = 0; i < inputs; i++) { InputNeurons.Add(new Neuron()); } for (int i = 0; i < outputs; i++) { var dentrites = new List <Synapse>(); foreach (var input in InputNeurons) { dentrites.Add(new Synapse() { Neuron = input, Weight = (random.NextDouble() - 0.5) * 0.01 }); } OutputNeurons.Add(new Neuron() { Dendrites = dentrites }); } }
public double[] GetOutputValues(int cycle) { return(OutputNeurons.Select(x => x.Values[cycle] / Divisor).ToArray()); }
public void RemoveOutputNeuron(WorkingNeuron neuron) { OutputNeurons.Remove(neuron); }
public void AddOutputNeuron(Neuron neuron) { OutputNeurons.Add(neuron); }