예제 #1
0
        /// <summary>
        /// Constructs a cyclic neural network.
        /// </summary>
        /// <param name="digraph">The directed graph that defines the neural network structure.</param>
        /// <param name="weightArr">An array of neural network connection weights.</param>
        /// <param name="activationFn">The neuron activation function to use at all neurons in the network.</param>
        /// <param name="cyclesPerActivation">The number of activation cycles to perform per overall activation of the cyclic network.</param>
        public NeuralNetCyclic(
            DirectedGraph digraph,
            double[] weightArr,
            VecFn2 <double> activationFn,
            int cyclesPerActivation)
        {
            Debug.Assert(digraph.ConnectionIdArrays._sourceIdArr.Length == weightArr.Length);

            // Store refs to network structure data.
            _srcIdArr  = digraph.ConnectionIdArrays._sourceIdArr;
            _tgtIdArr  = digraph.ConnectionIdArrays._targetIdArr;
            _weightArr = weightArr;

            // Store network activation function and parameters.
            _activationFn        = activationFn;
            _cyclesPerActivation = cyclesPerActivation;

            // Store input/output node counts.
            _inputCount  = digraph.InputCount;
            _outputCount = digraph.OutputCount;

            // Create node pre- and post-activation signal arrays.
            int nodeCount = digraph.TotalNodeCount;

            _preActivationArr  = new double[nodeCount];
            _postActivationArr = new double[nodeCount];

            // Wrap sub-ranges of the neuron signal arrays as input and output vectors.
            this.InputVector = new VectorSegment <double>(_postActivationArr, 0, _inputCount);

            // Note. Output neurons follow input neurons in the arrays.
            this.OutputVector = new VectorSegment <double>(_postActivationArr, _inputCount, _outputCount);
        }
예제 #2
0
    /// <summary>
    /// Constructs a cyclic neural network.
    /// </summary>
    /// <param name="digraph">The directed graph that defines the neural network structure.</param>
    /// <param name="weightArr">An array of neural network connection weights.</param>
    /// <param name="activationFn">The neuron activation function to use at all neurons in the network.</param>
    /// <param name="cyclesPerActivation">The number of activation cycles to perform per overall activation of the cyclic network.</param>
    public NeuralNetCyclic(
        DirectedGraph digraph,
        double[] weightArr,
        VecFn2 <double> activationFn,
        int cyclesPerActivation)
    {
        Debug.Assert(digraph.ConnectionIds.GetSourceIdSpan().Length == weightArr.Length);

        // Store refs to network structure data.
        _connIds   = digraph.ConnectionIds;
        _weightArr = weightArr;

        // Store network activation function and parameters.
        _activationFn        = activationFn;
        _cyclesPerActivation = cyclesPerActivation;

        // Store input/output node counts.
        _inputCount     = digraph.InputCount;
        _outputCount    = digraph.OutputCount;
        _totalNodeCount = digraph.TotalNodeCount;

        // Get a working array for both pre and post node activation signals, and map memory segments to pre
        // and post signal segments.
        // Rent an array that has length of at least _totalNodeCount * 2.
        _activationsArr = ArrayPool <double> .Shared.Rent(_totalNodeCount << 1);

        _preActivationMem  = _activationsArr.AsMemory(0, _totalNodeCount);
        _postActivationMem = _activationsArr.AsMemory(_totalNodeCount, _totalNodeCount);

        // Map the input and output vectors to the corresponding segments of _postActivationArr.
        this.Inputs = _postActivationMem.Slice(0, _inputCount);

        // Note. Output neurons follow input neurons in the arrays.
        this.Outputs = _postActivationMem.Slice(_inputCount, _outputCount);
    }
예제 #3
0
    /// <summary>
    /// Constructs a cyclic neural network.
    /// </summary>
    /// <param name="digraph">The directed graph that defines the neural network structure.</param>
    /// <param name="weightArr">An array of neural network connection weights.</param>
    /// <param name="activationFn">The neuron activation function to use at all neurons in the network.</param>
    /// <param name="cyclesPerActivation">The number of activation cycles to perform per overall activation of the cyclic network.</param>
    public NeuralNetCyclicSafe(
        DirectedGraph digraph,
        double[] weightArr,
        VecFn2 <double> activationFn,
        int cyclesPerActivation)
    {
        Debug.Assert(digraph.ConnectionIds.GetSourceIdSpan().Length == weightArr.Length);

        // Store refs to network structure data.
        _connIds   = digraph.ConnectionIds;
        _weightArr = weightArr;

        // Store network activation function and parameters.
        _activationFn        = activationFn;
        _cyclesPerActivation = cyclesPerActivation;

        // Store input/output node counts.
        _inputCount  = digraph.InputCount;
        _outputCount = digraph.OutputCount;

        // Create node pre- and post-activation signal arrays.
        int nodeCount = digraph.TotalNodeCount;

        _preActivationArr  = new double[nodeCount];
        _postActivationArr = new double[nodeCount];

        // Map the input and output vectors to the corresponding segments of _postActivationArr.
        this.Inputs = new Memory <double>(_postActivationArr, 0, _inputCount);

        // Note. Output neurons follow input neurons in the arrays.
        this.Outputs = new Memory <double>(_postActivationArr, _inputCount, _outputCount);
    }
        private void RunBenchmark(IActivationFunction <double> actFn)
        {
            VecFn2 <double> fn = actFn.Fn;

            for (int i = 0; i < __loops; i++)
            {
                fn(_x, _w);
            }
        }
예제 #5
0
 /// <summary>
 /// Constructs a cyclic neural network.
 /// </summary>
 /// <param name="digraph">The weighted directed graph that defines the neural network structure and connection weights.</param>
 /// <param name="activationFn">The neuron activation function to use at all neurons in the network.</param>
 /// <param name="cyclesPerActivation">The number of activation cycles to perform per overall activation of the cyclic network.</param>
 public NeuralNetCyclic(
     WeightedDirectedGraph <double> digraph,
     VecFn2 <double> activationFn,
     int cyclesPerActivation)
     : this(
         digraph,
         digraph.WeightArray,
         activationFn,
         cyclesPerActivation)
 {
 }