コード例 #1
0
    public bool AddNodeToLayer(int layer, bool randomWeights, float weightVal = 0.0f, float biasVal = 0.0f)
    {
        if (!Mathc.ValueIsBetween(layer, -1, layerCount, true))
        {
            return(false);
        }

        Neuron newNode = new Neuron(nodeCount++, randomWeights ? Random.value * 2 - 1 : biasVal);

        nodeLayers[layer].Add(newNode);

        if (layer > 0)
        {
            foreach (var node in nodeLayers[layer - 1])
            {
                node.ConnectNode(newNode, randomWeights ? Random.value * 2 - 1 : weightVal);
            }
        }
        if (layer < layerCount - 1)
        {
            foreach (var node in nodeLayers[layer + 1])
            {
                newNode.ConnectNode(node, randomWeights ? Random.value * 2 - 1 : weightVal);
            }
        }
        return(true);
    }
コード例 #2
0
 public NodeController GetNodeFromIndex(int index)
 {
     if (Mathc.ValueIsBetween(index, -1, nodes.Count, true))
     {
         return(nodes[index]);
     }
     return(null);
 }
コード例 #3
0
    public bool RemoveLayer(int index, bool replaceConnections)
    {
        if (!Mathc.ValueIsBetween(index, 0, layerCount - 1, true))
        {
            return(false);
        }

        var currLayer = nodeLayers[index];
        var prevLayer = nodeLayers[index - 1];
        var nextLayer = nodeLayers[index + 1];

        if (prevLayer.Count != currLayer.Count)
        {
            replaceConnections = true;
        }

        if (!replaceConnections)
        {
            for (int i = 0; i < currLayer.Count; ++i)
            {
                var n0 = currLayer[i];
                var n1 = prevLayer[i];
                n1.RemoveAllConnections();
                foreach (var n2 in nextLayer)
                {
                    n0.ReplaceConnection(n2, n1, false);
                }
            }
        }
        else
        {
            foreach (var n0 in prevLayer)
            {
                n0.RemoveAllConnections();
                foreach (var n1 in nextLayer)
                {
                    n0.ConnectNode(n1, Random.value * 2 - 1);
                }
            }
        }

        nodeLayers.RemoveAt(index);

        return(true);
    }
コード例 #4
0
    public bool RemoveNodeFromLayer(int layerIndex)
    {
        if (!Mathc.ValueIsBetween(layerIndex, -1, layerCount, true))
        {
            return(false);
        }

        var layer  = nodeLayers[layerIndex];
        var neuron = layer[layer.Count - 1];

        if (layerIndex > 0)
        {
            foreach (var n in nodeLayers[layerIndex - 1])
            {
                n.RemoveConnection(neuron);
            }
        }
        neuron.RemoveAllConnections();

        layer.RemoveAt(layer.Count - 1);
        nodeCount--;

        return(true);
    }
コード例 #5
0
    /// <summary>  </summary>
    /// <param name="index"></param>
    /// <param name="numNodes"></param>
    /// <param name="replaceAxonWeights">
    /// If you do not want to change the inputs going into this layer, set to false.
    /// NOTE: If the layer does not contain the same number of nodes as the one before it, THIS OPTION WILL DO NOTHING!!
    /// </param>
    /// <param name="randomWeights"></param>
    /// <param name="weightVal"></param>
    /// <param name="biasVal"></param>
    /// <returns></returns>
    public bool AddLayer(int index, int numNodes, bool replaceAxonWeights, bool randomWeights, float weightVal = 1.0f, float biasVal = 0.0f)
    {
        if (!Mathc.ValueIsBetween(index, 0, layerCount, true))
        {
            return(false);
        }

        // If the added layer and previous layer do not have the same number of nodes, copying connections can't be done.
        //Debug.Log($"{numNodes} != {nodeLayers[index-1].Count}");
        if (numNodes != nodeLayers[index - 1].Count)
        {
            replaceAxonWeights = true;
        }

        NeuronListWrapper newLayer = new List <Neuron>(numNodes);

        // Create and insert newLayer
        for (int i = 0; i < numNodes; ++i)
        {
            newLayer.Add(new Neuron(nodeCount++, randomWeights ? Random.value * 2 - 1 : biasVal));
        }
        nodeLayers.Insert(index, newLayer);

        var prevLayer = nodeLayers[index - 1];
        var nextLayer = nodeLayers[index + 1];

        // If possible, copy prevLayer->nextLayer connections to corresponding newLayer->nextLayer connections
        if (!replaceAxonWeights)
        {
            //Debug.Log("Copying connections");
            for (int i = 0; i < prevLayer.Count; ++i)
            {
                var n0 = prevLayer[i];
                var n1 = newLayer[i];
                foreach (var n2 in nextLayer)
                {
                    n0.ReplaceConnection(n2, n1, false);
                }
            }
        }
        // If not copying, remove prevLayer's connections, and then connect newLayer to nextLayer.
        else
        {
            //Debug.Log("Replacing connections");
            foreach (var n0 in prevLayer)
            {
                n0.RemoveAllConnections();
            }
            foreach (var n0 in newLayer)
            {
                foreach (var n1 in nextLayer)
                {
                    n0.ConnectNode(n1, randomWeights ? Random.value * 2 - 1 : weightVal);
                }
            }
        }


        // Connect prevLayer to newLayer
        foreach (var n0 in prevLayer)
        {
            foreach (var n1 in newLayer)
            {
                n0.ConnectNode(n1, randomWeights ? Random.value * 2 - 1 : weightVal);
            }
        }

        return(true);
    }