예제 #1
0
 public NeuralLayer(MathOperationManager mathManager, int neuronCount, int batchSize, NeuronActivationType activation, bool isInputLayer = false, bool isOutputLayer = false)
 {
     this.mathManager   = mathManager;
     this.IsInputLayer  = isInputLayer;
     this.IsOutputLayer = isOutputLayer;
     this.NeuralLink    = null;
     this.LayerIn       = null;
     this.LayerOut      = null;
     this.Activation    = activation;
     this.Data          = mathManager.CreateMatrix(neuronCount, batchSize);
     this.Bias          = mathManager.CreateMatrix(ArrayUtilities.GetArrayWithRandomValues(neuronCount), neuronCount);
     this.ErrorGradient = mathManager.CreateMatrix(neuronCount, batchSize);
 }
예제 #2
0
 public NeuralLayer(MathOperationManager mathManager, float[] biasData, int neuronCount, int batchSize, NeuronActivationType activation, bool isInputLayer = false, bool isOutputLayer = false)
 {
     this.mathManager   = mathManager;
     this.IsInputLayer  = isInputLayer;
     this.IsOutputLayer = isOutputLayer;
     this.NeuralLink    = null;
     this.LayerIn       = null;
     this.LayerOut      = null;
     this.Activation    = activation;
     this.Data          = mathManager.CreateMatrix(neuronCount, batchSize);
     this.Bias          = mathManager.CreateMatrix(biasData, neuronCount);
     this.ErrorGradient = mathManager.CreateMatrix(neuronCount, batchSize);
 }
예제 #3
0
        /// <summary>
        /// The neuron constructor
        /// </summary>
        /// <param name="_inputPorts">An array of input ports to the neuron</param>
        /// <param name="_numOutputIntBits">The number of integer bits to use on the output</param>
        /// <param name="_numOutputFracBits">The number of fractional bits to be used on the output</param>
        /// <param name="_numWeightIntBits">The number of integer bits used to represent the weights</param>
        /// <param name="_activationType">The thresholding function used to compute the output of the neuron</param>
        /// <param name="_name">The name of the neuron</param>
        public AsyncNeuron(Port[] _inputPorts, int _numOutputIntBits, int _numOutputFracBits, int _numWeightIntBits, NeuronActivationType _activationType, string _name)
        {
            /*Copy arguments*/
            this.neuronInputs      = _inputPorts;
            this.numOutputFracBits = _numOutputFracBits;
            this.numOutputIntBits  = _numOutputIntBits;
            this.activationType    = _activationType;
            this.name = _name;

            /*Initialize weight signals*/
            this.weightSignals = new Signal[this.neuronInputs.Length];
            int weightTopVal    = _numWeightIntBits; //NOT -1 => signed
            int weightBottomVal = this.neuronInputs[0].bottom;

            for (int i = 0; i < this.neuronInputs.Length; i++)
            {
                this.weightSignals[i] = new Signal(String.Format("w_{0}", i), Utilities.VHDLDataType.SIGNED_FIXED_POINT, null, weightTopVal, weightBottomVal);
            }

            /*Initialize product signals*/
            this.productSignals = new Signal[this.neuronInputs.Length];
            int productTopVal    = _numWeightIntBits + this.neuronInputs[0].top + 1;
            int productBottomVal = 2 * (weightBottomVal);

            for (int i = 0; i < this.neuronInputs.Length; i++)
            {
                this.productSignals[i] = new Signal(String.Format("p_{0}", i), Utilities.VHDLDataType.SIGNED_FIXED_POINT, null, productTopVal, productBottomVal);
            }

            /*Initialize sum signal*/
            int sumTopVal    = productTopVal + this.neuronInputs.Length;
            int sumBottomVal = productBottomVal;

            this.sum = new Signal("sum", Utilities.VHDLDataType.SIGNED_FIXED_POINT, null, sumTopVal, sumBottomVal);

            /*Initialize thresholded sum signal*/
            int threshTopVal    = this.numOutputIntBits; //NOT -1 => signed
            int threshBottomVal = this.numOutputFracBits;

            this.thresholdedSum = new Signal("thresh_sum", Utilities.VHDLDataType.SIGNED_FIXED_POINT, null, threshTopVal, -1 * threshBottomVal);

            /*Initialize loading signals*/
            int numOffsetBits = Utilities.getNumUnsignedBits(this.weightSignals.Length);

            this.loadClkPort    = new Port(Port.portDirection.IN, "loadClk", Utilities.VHDLDataType.STD_LOGIC, 0, 0);
            this.finalLoadPort  = new Port(Port.portDirection.OUT, "finalLoad", Utilities.VHDLDataType.STD_LOGIC, 0, 0);
            this.loadEnablePort = new Port(Port.portDirection.IN, "loadEnable", Utilities.VHDLDataType.STD_LOGIC, 0, 0);
            this.loadOffsetPort = new Port(Port.portDirection.IN, "loadOff", Utilities.VHDLDataType.UNSIGNED, numOffsetBits - 1, 0);
            this.loadValuePort  = new Port(Port.portDirection.IN, "loadVal", Utilities.VHDLDataType.SIGNED_FIXED_POINT, weightTopVal, weightBottomVal);

            /*Initialize output*/
            this.neuronOutput = new Port(Port.portDirection.OUT, "neuron_out", Utilities.VHDLDataType.SIGNED_FIXED_POINT, this.numOutputIntBits, -1 * this.numOutputFracBits); //NOT -1 => signed


            /*Initialize attached entities*/
            if (this.activationType == NeuronActivationType.SIGMOID_POLY_APPROX)
            {
                this.sigmoid_poly = new Sigmoid_PolyApprox(this.sum, Math.Abs(this.thresholdedSum.top) + 1, Math.Abs(this.thresholdedSum.bottom), String.Format("sigmoid_poly_{0}_{1}", this.numOutputIntBits, this.numOutputFracBits));
            }
            else
            {
                /*Linear node has no dependent entities!*/
            }

            return;
        }