private GameObject CreateSynapseObject(AnnSynapse aNN_Synapse, int synapseID) { int inputLayerID = aNN_Synapse.InputNeuron.LayerID; int inputPositionID = aNN_Synapse.InputNeuron.PositionID; int outputLayerID = aNN_Synapse.OutputNeuron.LayerID; int outputPositionID = aNN_Synapse.OutputNeuron.PositionID; GameObject synapseGO = new GameObject { name = "Synapse_" + synapseID }; AddTextObject(synapseGO); Vector3 dir = (neuronObjects[outputLayerID][outputPositionID].transform.position - neuronObjects[inputLayerID][inputPositionID].transform.position).normalized; synapseGO.transform.position = neuronObjects[inputLayerID][inputPositionID].transform.position + dir * horizontalSpacing * weightPos; GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cylinder); go.name = "-2_" + synapseID; float diameter = 0.05f; float length = Vector3.Distance(neuronObjects[inputLayerID][inputPositionID].transform.position, neuronObjects[outputLayerID][outputPositionID].transform.position) / 2; go.transform.localScale = new Vector3(diameter, length, diameter); go.transform.position = neuronObjects[inputLayerID][inputPositionID].transform.position; go.transform.LookAt(neuronObjects[outputLayerID][outputPositionID].transform); go.transform.rotation *= Quaternion.Euler(90, 0, 0); go.transform.position = (neuronObjects[outputLayerID][outputPositionID].transform.position + neuronObjects[inputLayerID][inputPositionID].transform.position) / 2.0f; go.transform.SetParent(synapseGO.transform); MakeClickable(go); return(synapseGO); }
private string GetSynapseInfo(int positionID) { AnnSynapse synapse = Net.SynapsesAll[positionID]; string message = "Synapse" + "\n"; message += "ID: " + positionID + "\n"; message += "Input Neuron: " + synapse.InputNeuron.LayerID + " / " + synapse.InputNeuron.PositionID + "\n"; message += "Input Neuron [Output]: " + synapse.InputNeuron.Output + "\n"; message += "Output Neuron: " + synapse.OutputNeuron.LayerID + " / " + synapse.OutputNeuron.PositionID + "\n"; message += "Output Neuron [Total Input]: " + synapse.OutputNeuron.Input + "\n"; message += "Weight: " + synapse.Weight + "\n"; message += "Synapse output: " + synapse.Weight * synapse.InputNeuron.Output + "\n"; return(message); }
private double BackPropSingleDataSet(double[] sampleDataSet, double[] targetDataSet) { AnnFeedForward feedForwardModel = new AnnFeedForward(net); feedForwardModel.FeedForward(sampleDataSet); int outputLayerID = net.NetLayers.Length - 1; double[] errors = new double[net.NetLayers[outputLayerID].Length]; // calculate output deltas for (int outNeuronID = 0; outNeuronID < net.NetLayers[outputLayerID].Length; outNeuronID++) { AnnNeuron neuron = net.NetLayers[outputLayerID][outNeuronID]; double derivAFOutput = neuron.ActivationFunction.GetAFDerivValue(neuron.Input); double outputScalar = neuron.ActivationFunction.Scalar; double derivError = -(targetDataSet[outNeuronID] / outputScalar - neuron.Output / outputScalar); if (targetDataSet[outNeuronID] != 0) { errors[outNeuronID] += (targetDataSet[outNeuronID] - neuron.Output) / targetDataSet[outNeuronID]; } else { errors[outNeuronID] += 0; } neuron.BackPropDelta = derivAFOutput * derivError; } // calculate remaining deltas for (int layerID = outputLayerID - 1; layerID > 0; layerID--) { for (int neuronID = 0; neuronID < net.NetLayers[layerID].Length; neuronID++) { net.NetLayers[layerID][neuronID].BackPropDeltaUpdate(); } } // add delta * input neuron output to the synapse deltainput list for (int synapseID = 0; synapseID < net.SynapsesAll.Count; synapseID++) { AnnSynapse synapse = net.SynapsesAll[synapseID]; synapse.DeltaInputAdd(synapse.OutputNeuron.BackPropDelta * synapse.InputNeuron.Output); } return(MaxPercentageError(errors)); }