public void DisplayConnections(int neuronIndex, NeuralLayer currentLayer, UINeuralNetworkLayerPanel nextLayer) { Image dummyConnection = Connections[0]; dummyConnection.gameObject.SetActive(true); for (int i = Connections.Count; i < currentLayer.OutputCount; i++) { Image newConnection = Instantiate(dummyConnection); newConnection.transform.SetParent(this.transform, false); Connections.Add(newConnection); } for (int i = this.Connections.Count - 1; i >= currentLayer.OutputCount; i++) { Image toBeDestroyed = Connections[i]; Connections.RemoveAt(i); Destroy(toBeDestroyed); } for (int i = 0; i < Connections.Count; i++) { PositionConnection(Connections[i], nextLayer.Nodes[i], neuronIndex, i, currentLayer.Weights); } }
/// <summary> /// Copies this NeuralLayer including its weights. /// </summary> /// <returns>A deep copy of this NeuralLayer</returns> public NeuralLayer DeepCopy() { //Copy weights double[,] copiedWeights = new double[this.Weights.GetLength(0), this.Weights.GetLength(1)]; for (int x = 0; x < this.Weights.GetLength(0); x++) { for (int y = 0; y < this.Weights.GetLength(1); y++) { copiedWeights[x, y] = this.Weights[x, y]; } } //Create copy NeuralLayer newLayer = new NeuralLayer(this.NeuronCount, this.OutputCount, rnnLayer); newLayer.Weights = copiedWeights; newLayer.NeuronActivationFunction = this.NeuronActivationFunction; if (rnnLayer) { newLayer.recurrents = recurrents; } return(newLayer); }
public static NeuralNetwork SimpleLinearNetworkWithDropout(double drate, double dseed) { var net = new NeuralNetwork(1, activation: new Mocks.LinearActivation()); net.IsTraining = true; var layer1 = new NeuralLayer(1); net.AddLayer(layer1); var layer2 = new NeuralLayer(1); layer2.DropoutRate = drate; layer2.DropoutSeed = 1; layer2[0].LastRetained = true; net.AddLayer(layer2); var layer3 = new NeuralLayer(1); net.AddLayer(layer3); net.Build(); layer1[0].Bias = 1; layer1[0][0] = 3; layer2[0].Bias = -1; layer2[0][0] = 1; layer3[0].Bias = 2; layer3[0][0] = -1; return(net); }
/// <summary> /// Displays the connections of a neuron of one layer to all neurons of the next layer. /// </summary> /// <param name="neuronIndex">The neuron to display the connections of.</param> /// <param name="currentLayer">The layer of the neuron to be displayed.</param> /// <param name="nextLayer">The layer the neuron to be displayed is connected to.</param> public void DisplayConnections(int neuronIndex, NeuralLayer currentLayer, UINeuralNetworkLayerPanel nextLayer) { // Set dummyConnection to active again, just in case it was hidden at some point. Image dummyConnection = Connections[0]; dummyConnection.gameObject.SetActive(true); // Duplicate dummyConnection for (int i = Connections.Count; i < currentLayer.OutputCount; i++) { Image newConnection = Instantiate(dummyConnection); newConnection.transform.SetParent(this.transform, false); Connections.Add(newConnection); } // Destory all unnecessary connections for (int i = this.Connections.Count - 1; i >= currentLayer.OutputCount; i++) { Image toBeDestroyed = Connections[i]; Connections.RemoveAt(i); Destroy(toBeDestroyed); } // Position connections for (int i = 0; i < Connections.Count; i++) { PositionConnection(Connections[i], nextLayer.Nodes[i], neuronIndex, i, currentLayer.Weights); } }
public static NeuralNetwork SimpleLinearNetwork() { var net = new NeuralNetwork(1, activation: new Mocks.LinearActivation()); net.IsTraining = true; var layer1 = new NeuralLayer(1); net.AddLayer(layer1); var layer2 = new NeuralLayer(1); net.AddLayer(layer2); var layer3 = new NeuralLayer(1); net.AddLayer(layer3); net.Build(); layer1[0].Bias = 1; layer1[0][0] = 3; layer2[0].Bias = -1; layer2[0][0] = 1; layer3[0].Bias = 2; layer3[0][0] = -1; return(net); }
public void FeedNextLayer(NeuralLayer layer) { foreach (NeuralNode node in nodes) { node.FeedNextLayer(layer); } }
/// <summary> /// Displays all connections from between the given two layers. /// </summary> /// <param name="currentLayer">The layer that is connected to the other layer.</param> /// <param name="nextLayer">The layer that the other layer is connected to.</param> public void DisplayConnections(NeuralLayer currentLayer, UINeuralNetworkLayerPanel nextLayer) { for (int i = 0; i < Nodes.Count; i++) { Nodes[i].DisplayConnections(i, currentLayer, nextLayer); } }
//Kopiuje warstwe public NeuralLayer CopyLayer() { NeuralLayer copy = new NeuralLayer(NeuronNumber, Connections); copy.Weights = Weights; return(copy); }
public NeuralLayer(NeuralLayer neuralLayer) { if (neuralLayer == null) { throw new ArgumentNullException("Neural layer has not been provided"); } CloneNeuralLayer(neuralLayer); }
public void AddLayer(NeuralLayer newLayer) { if (_layers.Any()) { var lastLayer = _layers.Last(); newLayer.ConnectLayers(lastLayer); } _layers.Add(newLayer); }
//Connect all of the layers in the neural network, the netwok must have layers for this to work public void BuildNetwork() { //Break if only 1 or 0 layers if (NeuralLayers.Count < 2) { Console.WriteLine("Not enough layers."); return; } //Conects each layer to the next layer foreach (NeuralLayer neuralLayer in NeuralLayers) { //The last layer doesn't connect to anything, break if (neuralLayer == NeuralLayers.Last()) { break; } //Fetch the next layer to connect to NeuralLayer nextLayer = NeuralLayers[neuralLayer.LayerNumber + 1]; Random rand = new Random(); //Loop through all nodes from both layers and try to conect them foreach (Neuron fromNeuron in neuralLayer.Neurons) { foreach (Neuron toNeuron in nextLayer.Neurons) { //Neurons with the same number will allways conect if (fromNeuron.NeuronNumber == toNeuron.NeuronNumber) { toNeuron.Conections.Add(new Conection() { ConectedFrom = fromNeuron, weight = rand.NextDouble() + 0.1 }); } //Else, randomly assign conections, if the random number is zero, no conection else if (rand.Next(0, Globals.XmlData.Inputs) != 0) { toNeuron.Conections.Add(new Conection() { ConectedFrom = fromNeuron, weight = rand.NextDouble() + 0.1 }); } //We need to worry about neurons not having a conection if next layer has more nodes than current layer else if (toNeuron.NeuronNumber >= neuralLayer.Neurons.Count && fromNeuron == neuralLayer.Neurons.Last() && toNeuron.Conections.Count < 1) { toNeuron.Conections.Add(new Conection() { ConectedFrom = fromNeuron, weight = rand.NextDouble() + 0.1 }); } } } } }
private void ComputeGradient(Gradients gradients, List <double> inputs, List <double> requiredOutputs) { Activities(inputs); for (int i = NumberOfLayers() - 1; i >= 1; i--) { NeuralLayer currentLayer = GetLayer(i); if (currentLayer.IsLayerTop()) { for (int j = 0; j < currentLayer.NumberOfNeurons(); j++) { Neuron currentNeuron = currentLayer.GetNeuron(j); gradients.SetThreshold(i, j, currentNeuron.Output * (1 - currentNeuron.Output) * (currentNeuron.Output - requiredOutputs[j])); } for (int j = 0; j < currentLayer.NumberOfNeurons(); j++) { Neuron currentNeuron = currentLayer.GetNeuron(j); for (int k = 0; k < currentNeuron.NumberOfInputs(); k++) { NeuralInput currentInput = currentNeuron.GetInput(k); gradients.SetWeight(i, j, k, gradients.GetThreshold(i, j) * currentLayer.LowerLayer().GetNeuron(k).Output); } } } else { for (int j = 0; j < currentLayer.NumberOfNeurons(); j++) { double aux = 0; for (int ia = 0; ia < currentLayer.UpperLayer().NumberOfNeurons(); ia++) { aux += gradients.GetThreshold(i + 1, ia) * currentLayer.UpperLayer().GetNeuron(ia).GetInput(j).Weight; } gradients.SetThreshold(i, j, currentLayer.GetNeuron(j).Output *(1 - currentLayer.GetNeuron(j).Output) * aux); } for (int j = 0; j < currentLayer.NumberOfNeurons(); j++) { Neuron currentNeuron = currentLayer.GetNeuron(j) ; for (int k = 0; k < currentNeuron.NumberOfInputs(); k++) { NeuralInput currentInput = currentNeuron.GetInput(k); gradients.SetWeight(i, j, k, gradients.GetThreshold(i, j) * currentLayer.LowerLayer().GetNeuron(k).Output); } } } } }
protected MutationData MutateLayer(NeuralLayer targetLayer, NeuralLayer ancestorLayer) { MutationData data = new MutationData(); for (int n = 0; n < targetLayer.nodes.Count; n++) { data += MutateNode(targetLayer.nodes[n], ancestorLayer.nodes[n]); } return(data); }
public Neuron(int numberOfInputs, double threshold, NeuralLayer neuralLayer) { Threshold = threshold; NeuralLayer = neuralLayer; Index = NeuralLayer.NumberOfNeurons(); for (int i = 0; i < numberOfInputs; i++) { listInputs.Add(new NeuralInput(1.0, this)); } }
public NeuralNetwork() { inputsNumber = 0; outputsNumber = 0; inputLayer = new NeuralLayer(); outputLayer = new NeuralLayer(); nnInputs = new List <float>(); nnOutputs = new List <float>(); hiddenLayers = new List <NeuralLayer>(); }
private void LoadFromXml(string filePath) { Console.WriteLine("NeuralNetwork : loading network topology from file " + filePath); // Read XML to XmlDocument XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(filePath); string rootNodeName = xmlDocument.Name; if (rootNodeName != "neuralNetwork") { throw new InvalidOperationException( "[Error] NN-Load: Parse error in XML file, neural network couldn't be loaded."); } var nodeList = xmlDocument.ChildNodes; for (int i = 0; i < nodeList.Count; i++) { var nodeStructure = nodeList.Item(i); if (nodeStructure != null && nodeStructure.Name == "structure") { var nodeStructureContent = nodeStructure.ChildNodes; for (int j = 0; j < nodeStructureContent.Count; j++) { var nodeLayer = nodeStructureContent.Item(j); if (nodeLayer != null && nodeLayer.Name == "layer") { NeuralLayer neuralLayer = new NeuralLayer(this); listLayers.Add(neuralLayer); var nodeLayerContent = nodeLayer.ChildNodes; for (int k = 0; k < nodeLayerContent.Count; k++) { var nodeNeuron = nodeLayerContent.Item(k); if (nodeNeuron != null && nodeNeuron.Name == "neuron") { Neuron neuron = new Neuron(double.Parse(nodeNeuron.Attributes["threshold"].ToString()), neuralLayer); neuralLayer.listNeurons.Add(neuron); var nodeNeuronContent = nodeNeuron.ChildNodes; for (int l = 0; l < nodeNeuronContent.Count; l++) { var nodeNeuralInput = nodeNeuronContent.Item(l); if (nodeNeuralInput != null && nodeNeuralInput.Name == "input") { var neuralInput = new NeuralInput(double.Parse(nodeNeuralInput.Attributes["weight"].ToString()), neuron); neuron.listInputs.Add(neuralInput); } } } } } } } } }
/// <summary> /// Add layer to the neural network. /// Layer will automatically be added as the output layer to the last layer in the neural network. /// </summary> public void AddLayer(NeuralLayer newLayer) { if (_layers.Any()) { var lastLayer = _layers.Last(); newLayer.ConnectLayers(lastLayer); } _layers.Add(newLayer); _neuronErrors.Add(_layers.Count - 1, new double[newLayer.Neurons.Count]); }
void SetupNetworkArchitecture() { int nextLayerSize = networkComposition.hiddenLayers[0].nodes; inputLayer = new NeuralLayer(networkComposition.inputLayer.nodes, LayerType.INPUT_LAYER, nextLayerSize); SetupHiddenLayers(); outputLayer = new NeuralLayer(networkComposition.outputLayer.nodes, LayerType.OUTPUT_LAYER, -1); }
/// <summary> /// calculate overall net error (RMS(Root Means Square) errors formula) /// </summary> /// <param name="targetValues"></param> void BackPropogation(List <double> targetValues) { NeuralLayer outputLayer = m_layers[m_layers.Count - 1]; m_error = 0.0; // calculate rms error for the last (output) layer // rms = average of (sum of (target - actualvalue squared)) // rms = sqrt(1 / (target - output value)squared) for (int i = 0; i < outputLayer.m_neurons.Count; i++) { double errorDelta = targetValues[i] - outputLayer.m_neurons[i].GetOutputValue(); m_error += errorDelta * errorDelta; } m_error /= outputLayer.m_neurons.Count - 1; m_error = Math.Sqrt(m_error); // gives output of the current error avg // allows us to check how close/far the network is to completion m_recentAverageError = ((m_recentAverageError * m_recentAverageSmoothingFactor) + m_error) / (m_recentAverageSmoothingFactor + 1.0); // calculate output layer gradients for (int i = 0; i < outputLayer.m_neurons.Count; i++) { outputLayer.m_neurons[i].CalculateOutputGradients(targetValues[i]); } // calculate gradients on hidden (a layer between first and last layers) layers // starting from outputlayerIndex - 1 // and moving back to the initiallayerIndex + 1 for (int i = m_layers.Count - 2; i > 0; i--) { NeuralLayer curHiddenLayer = m_layers[i]; NeuralLayer nextLayer = m_layers[i + 1]; for (int j = 0; j != curHiddenLayer.m_neurons.Count; j++) { curHiddenLayer.m_neurons[j].CalculateHiddenGradients(nextLayer); } } // for all layers from outputs to first hidden layer // update connection weights for (int i = m_layers.Count - 1; i > 0; i--) { NeuralLayer curLayer = m_layers[i]; NeuralLayer prevLayer = m_layers[i - 1]; for (int j = 0; j != curLayer.m_neurons.Count; j++) { curLayer.m_neurons[j].UpdateInputWeights(prevLayer); } } }
public NeuralLayer CreateNeuralLayer(int numberOfNeurons, IActivationFunction activationFunction) { var layer = new NeuralLayer(); for (int i = 0; i < numberOfNeurons; i++) { var neuron = new Neuron(activationFunction); layer.Neurons.Add(neuron); } return(layer); }
public NeuralLayer CreateNeuralLayer(int numberOfNeurons, double[] weights, IActivationFunction activationFunction, IInputFunction inputFunction) { var layer = new NeuralLayer(weights); for (int i = 0; i < numberOfNeurons; i++) { var neuron = new Neuron(activationFunction, inputFunction); layer.Neurons.Add(neuron); } return(layer); }
public NeuralNetwork(int inputs, int outputs, int hiddenLayers, int hiddenNeurons) { inputAmount = inputs; outputAmount = outputs; outputLayer = new NeuralLayer(outputs, hiddenNeurons); for (int i = 0; i < hiddenLayers; i++) { NeuralLayer layer = new NeuralLayer(hiddenNeurons, inputs); this.hiddenLayers.Add(layer); } outputLayer = new NeuralLayer(outputs, hiddenNeurons); }
void Start() { layers = new NeuralLayer[nbHiddenLayers + 1]; float[] currentInputs = inputs; for (int i = 0; i < nbHiddenLayers; ++i ) { NeuralLayer layer = new NeuralLayer(currentInputs, nbNeuronsPerHiddenLayer); layers[i] = layer; currentInputs = layer.Outputs; } layers[layers.Length-1] = new NeuralLayer(currentInputs, nbOutputs); }
public void CreateAgents(out List <Agent> agents, IEnumerable <Genotype> population) { //Create new agents from currentPopulation agents = new List <Agent>(); NeuralLayer.ActivationFunction af = NeuralLayer.GetActivitionFunction(activationFunctionType); foreach (Genotype genotype in population) { agents.Add(new Agent(genotype, af, useRNN, NNTopology)); } isNewAgents = true; }
public double SumDOW(NeuralLayer nextLayer) { double sum = 0.0; // sum all contributions of errors for next layer for (int i = 0; i < nextLayer.m_neurons.Count - 1; i++) { sum += m_outputWeights[i] * nextLayer.m_neurons[i].m_gradient; } return(sum); }
public NeuralNetwork(int[] layerShape) { this.layerShape = layerShape; neuralLayers = new List <NeuralLayer>(); for (int i = 0; i < layerShape.Length; i++) { int weightNum = i == 0 ? 0 : layerShape[i - 1] + 1; NeuralLayer layer = new NeuralLayer(layerShape[i], weightNum, i == layerShape.Length - 1); neuralLayers.Add(layer); } GetSplitPoints(); }
public NeuralNetwork() { _inputAmount = 0; _outputAmount = 0; _inputlayer = new NeuralLayer(); _outputLayer = new NeuralLayer(); _inputs = new List <float>(); _outputs = new List <float>(); _hiddenLayers = new List <NeuralLayer>(); }
private void CreateNetwork(NeuralLayer connectingFrom, NeuralLayer connectingTo) { foreach (var to in connectingTo.Neurons) { foreach (var from in connectingFrom.Neurons) { to.Nerves.Add(new Nerve() { InPulse = to.OutPulse, Weight = connectingTo.Weight }); } } }
public void IntializeNetwork() { Inputs = HeavyAlienBrainCreator.instance.inputs; Outputs = HeavyAlienBrainCreator.instance.outputs; NeuronsInLayer = HeavyAlienBrainCreator.instance.neuronsPerHiddenLayer; HiddenLayers = HeavyAlienBrainCreator.instance.hiddenLayersPerNetwork; hiddenLayers = new List<NeuralLayer>(); firstHiddenLayer = new NeuralLayer(Inputs, NeuronsInLayer); // Debug.Log("[network] neurons in layer" + NeuronsInLayer); outputLayer = new NeuralLayer(NeuronsInLayer, Outputs); for (int i = 0; i < HiddenLayers - 1; ++i) hiddenLayers.Add(new NeuralLayer(NeuronsInLayer, NeuronsInLayer)); }
/// <summary> /// Connecting two layers. /// </summary> public void ConnectLayers(NeuralLayer inputLayer) { var combos = Neurons.SelectMany(neuron => inputLayer.Neurons, (neuron, input) => new { neuron, input }); var combosList = combos.ToList(); for (int i = 0; i < combos.Count(); i++) { combosList[i].neuron.AddInputNeuron(combosList[i].input, _weights[i]); } //combos.ToList().ForEach(x => x.neuron.AddInputNeuron(x.input)); }
protected virtual INeuralLayer <double> BuildLayer(int neuronsInLayer) { IList <INeuron <double> > neurons = new List <INeuron <double> >(); for (int i = 0; i < neuronsInLayer; ++i) { INeuron <double> neuron = this.GetSpecificNeuron(); neurons.Add(neuron); } INeuralLayer <double> layer = new NeuralLayer(neurons); return(layer); }
/// <summary> /// main calculation functions /// takes in output from previous layer /// sums it /// outputs a new modified value /// </summary> /// <param name="previousLayer"></param> public void FeedForward(NeuralLayer previousLayer) { double sum = 0.0; for (int i = 0; i != previousLayer.m_neurons.Count; i++) { // multiple the previous layers output value (this current neurons input) by previous layers outputweight to me // and add to the sum //sum += previousLayer.m_neurons[i].GetOutputValue() * previousLayer.m_neurons[i].m_outputWeights[m_myIndex]; sum += previousLayer.m_neurons[i].GetOutputValue() * previousLayer.m_neurons[i].m_outputWeights[m_myIndex]; } m_output = Transfer(sum); }
public void Create(int[] networkStructure) { inputsNumber = networkStructure[0]; outputsNumber = networkStructure[networkStructure.Length - 1]; int hiddenLayers = networkStructure.Length - 2; for (int i = 0; i < hiddenLayers; i++) { NeuralLayer layer = new NeuralLayer(); layer.PopulateLayer(networkStructure[i + 1], networkStructure[i]); this.hiddenLayers.Add(layer); } outputLayer.PopulateLayer(networkStructure[networkStructure.Length - 1], networkStructure[networkStructure.Length - 2]); }
/// <summary> /// Initializes a new instance of the <see cref="Vulcan" /> class with X number of hidden layers /// </summary> /// <param name="hidden">Number of hidden layer(s)</param> public Vulcan(int hidden) { this.Error = 0.0; var input = new NeuralLayer("Input"); this.AddLayer(input); for (var i = 0; i < hidden; ++i) { var layer = new NeuralLayer($"Hidden{i + 1}"); this.AddLayer(layer); } var output = new NeuralLayer("Output"); this.AddLayer(output); }
private void _compute(NeuralOutputHolder holder, NeuralLayer layer, NVector input, Synapse source) { PreProcessLayer(layer, input, source); foreach (var synapse in layer.OutputSynapses) { if (!holder.Results.ContainsKey(synapse)) { var nextLayer = synapse.OutputLayer; var pattern = synapse.Compute(input); pattern = synapse.OutputLayer.Compute(pattern); synapse.OutputLayer.Process(pattern); holder.Results[synapse] = input; _compute(holder, synapse.OutputLayer, pattern, synapse); if (nextLayer == Network.OutputLayer) { holder.Output = pattern; } } } }
public void FromGenome(Genome genome, int inputs, int outputs, int hiddenNeurons) { Release(); outputAmount = outputs; inputAmount = inputs; //int weightsForHidden = inputs * hiddenNeurons; List<Neuron> neurons = new List<Neuron>(); for (int i = 0; i < hiddenNeurons; i++) { List<float> weights = new List<float>(); for (int j = 0; j < inputs + 1; j++) { weights.Add(0.0f); weights[j] = genome.GetGen(i * hiddenNeurons + j); } neurons.Add(new Neuron(weights, inputs)); } NeuralLayer hidden = new NeuralLayer(neurons); //Debug.Log ("fromgenome, hiddenlayer neruons#: " + neurons.Count); //Debug.Log ("fromgenome, hiddenlayer numInput: " + neurons[0].inputs); this.hiddenLayers.Add(hidden); //int weightsForOutput = hiddenNeurons * outputs; List<Neuron> outneurons = new List<Neuron>(); for (int i = 0; i < outputs; i++) { List<float> weights = new List<float>(); for (int j = 0; j < hiddenNeurons + 1; j++) { weights.Add(0.0f); weights[j] = genome.GetGen(i * hiddenNeurons + j); } outneurons.Add(new Neuron(weights, hiddenNeurons)); } this.outputLayer = new NeuralLayer(outneurons); //Debug.Log ("fromgenome, outputlayer neruons#: " + outneurons.Count); //Debug.Log ("fromgenome, outputlayer numInput: " + outneurons[0].inputs); }
public void Release() { if (inputLayer != null) { inputLayer = new NeuralLayer(); } if (outputLayer != null) { outputLayer = new NeuralLayer(); } for (int i = 0; i < hiddenLayers.Count; i++) { if (hiddenLayers[i] != null) { hiddenLayers[i] = null; } } hiddenLayers.Clear(); }
/// <summary>Can be overridden by subclasses. Usually used to implement recurrent layers.</summary> public virtual void PreProcessLayer(NeuralLayer layer, NVector input, Synapse source) { }
/// <summary> /// Removes a layer from the network /// </summary> /// <param name="layer">The layer to be removed</param> /// <returns>True if succeeds</returns> public bool RemoveLayer(NeuralLayer layer) { return this.layers.Remove(layer); }
/// <summary> /// Adds a neural layer to the network /// </summary> /// <param name="layer">The layer to be added</param> public void AddLayer(NeuralLayer layer) { this.layers.Add(layer); }
/// <summary>Connects this synapse to the given input / output layers.</summary> internal void Connect(NeuralLayer inputLayer, NeuralLayer outputLayer) { InputLayer = inputLayer; OutputLayer = outputLayer; }