Exemplo n.º 1
0
        /// <summary>
        /// Runs through the network and makes a prediction.
        /// </summary>
        /// <param name="input">The input image to run the prediction on.</param>
        /// <returns>The values from the final layer. The largest value is the networks prediction.</returns>
        public double[] FeedForward(double[] input)
        {
            Matrix <double> _currentImages = CreateMatrix.DenseOfRowVectors(CreateVector.DenseOfArray <double>(input));

            foreach (ILayerInformation _layerInformation in this.LayerInformation)
            {
                switch (_layerInformation.LayerType)
                {
                case (LayerType.Convolutional):
                    ConvolutionalLayerInformation _convInfo = _layerInformation as ConvolutionalLayerInformation;
                    _currentImages = this.Convolve(_convInfo, _currentImages);
                    break;

                case (LayerType.Pooling):
                    PoolingLayerInformation _poolInfo = _layerInformation as PoolingLayerInformation;
                    _currentImages = this.Pool(_poolInfo, _currentImages);
                    break;

                case (LayerType.NonLinear):
                    NonLinearLayerInformation _nonLinearInfo = _layerInformation as NonLinearLayerInformation;
                    _currentImages = this.NonLinear(_nonLinearInfo, _currentImages);
                    break;
                }
            }

            double[] _fullyConnectedInput = CreateVector.DenseOfEnumerable <double>(_currentImages.EnumerateRows().SelectMany(a => a)).ToArray();

            return(this.FullyConnectedNetwork.FeedForward(_currentImages.Enumerate().ToArray()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs through a single pooling layer.
        /// </summary>
        /// <param name="layerInfo">The layer info used for this layer.</param>
        /// <param name="inputImages">
        /// A matrix of all the images that will be pooled. Each row is an image.
        /// </param>
        /// <returns>A matrix of all the resulting images. Each row is an image.</returns>
        internal Matrix <double> Pool(PoolingLayerInformation layerInfo, Matrix <double> inputImages)
        {
            Matrix <double> _preConvolutionMap = null;
            Matrix <double> _outputImages      = CreateMatrix.Dense <double>(inputImages.RowCount, (int)Math.Pow((Math.Sqrt(inputImages.ColumnCount) - layerInfo.KernelSize) / layerInfo.Stride + 1, 2));

            for (int i = 0; i < inputImages.RowCount; i++)
            {
                _preConvolutionMap = this.CreateMaskingMap(layerInfo.KernelSize, layerInfo.Stride, inputImages.Row(i));

                switch (layerInfo.PoolingType)
                {
                case (PoolingType.MaxPooling):
                    _outputImages.SetRow(i, _preConvolutionMap.ColumnNorms(double.PositiveInfinity));
                    break;
                }
            }

            return(_outputImages);
        }