public override AbstractLayer Compile(AbstractLayer previousLayer)
        {
            if (previousLayer == null && inputDimension == null)
            {
                string msg = "Dimension of first layer of network not set!";
                Utils.ThrowException(msg);
            }
            else if (previousLayer != null)
            {
                inputDimension = previousLayer.outputDimension;
                index          = previousLayer.Index + 1;
            }

            TestHyperParametersAndSetOutputSize(this.inputDimension.width,
                                                this.inputDimension.height, this.inputDimension.depth);

            outputDimension = new Dimension(this.inputDimension.imageCount, filterCount,
                                            1, outputSize);

            #region Init initializers
            if (biasInitializer == null)
            {
                biasInitializer = new ConstantBiasInitializer(0d);
            }
            biases = biasInitializer.Initialize(filterCount);

            // https://stats.stackexchange.com/questions/198840/cnn-xavier-weight-initialization
            // https://github.com/keras-team/keras/blob/998efc04eefa0c14057c1fa87cab71df5b24bf7e/keras/initializations.py
            int fanIn  = filterSize * filterSize * inputDimension.depth;
            int fanOut = filterCount;
            if (weightInitializer == null)
            {
                weightInitializer = new XavierWeightInitializer(fanIn, fanOut);
            }
            #endregion Init initializers

            filters = weightInitializer.Initialize(new Dimension(filterCount,
                                                                 inputDimension.depth, inputDimension.width, filterSize));

            // activations will be init
            activations = Utils.Init4dArr(inputDimension.imageCount,
                                          filterCount, inputDimension.width, outputSize);

            compiled = true;

            return(this);
        }
Esempio n. 2
0
        public override AbstractLayer Compile(AbstractLayer previousLayer)
        {
            if (previousLayer == null)
            {
                string msg = "Dimension of first layer of network not set!";
                Utils.ThrowException(msg);
            }
            else if (previousLayer != null)
            {
                inputDimension = previousLayer.outputDimension;
                index          = previousLayer.Index + 1;
            }

            inputDimension = previousLayer.outputDimension;

            outputDimension = new Dimension(inputDimension.imageCount, 1, 1, numNeurons);

            activations = Utils.Init4dArr(inputDimension.imageCount, 1, 1, numNeurons);

            #region Init initializers
            if (biasInitializer == null)
            {
                biasInitializer = new ConstantBiasInitializer(0d);
            }
            biases = biasInitializer.Initialize(numNeurons);


            int fanIn  = inputDimension.height;
            int fanOut = numNeurons;
            if (weightInitializer == null)
            {
                weightInitializer = new XavierWeightInitializer(fanIn, fanOut);
            }
            weights = weightInitializer.Initialize(new Dimension(inputDimension.height, 1, 1, numNeurons));
            #endregion Init initializers

            compiled = true;

            return(this);
        }