コード例 #1
0
ファイル: Cnn.cs プロジェクト: Maggie123-huang/CnnDemo
        /// <summary>
        /// 增加全连接层,仅用于BP网络
        /// </summary>
        /// <param name="cnnFullLayer"></param>
        public void AddCnnFullLayer(int inputCount, int outputCount, CnnNode.ActivationFunctionTypes activationFunctionType, bool standardization)
        {
            CnnFullLayer cnnFullLayer = new CnnFullLayer(inputCount,
                                                         outputCount, activationFunctionType, standardization);

            CnnFullLayerList.Add(cnnFullLayer);
        }
コード例 #2
0
 /// <summary>
 /// 增加全连接层,仅用于BP网络
 /// </summary>
 /// <param name="cnnFullLayer"></param>
 public void AddCnnFullLayer(int inputCount, int outputCount, CnnNode.ActivationFunctionTypes activationFunctionType, bool standardization)
 {
     foreach (Cnn cnn in cnnList)
     {
         cnn.AddCnnFullLayer(inputCount, outputCount, activationFunctionType, standardization);
     }
 }
コード例 #3
0
 /// <summary>
 /// 创建卷积层
 /// </summary>
 /// <param name="convolutionKernelCount"></param>
 /// <param name="inputWidth"></param>
 /// <param name="inputHeight"></param>
 /// <param name="receptiveFieldWidth"></param>
 /// <param name="receptiveFieldHeight"></param>
 /// <param name="offsetWidth"></param>
 /// <param name="offsetHeight"></param>
 /// <param name="activationFunctionType"></param>
 public void CreateCnnKernel(int convolutionKernelCount, int inputWidth, int inputHeight,
                             int receptiveFieldWidth, int receptiveFieldHeight, int offsetWidth,
                             int offsetHeight, CnnNode.ActivationFunctionTypes activationFunctionType, int LastLayerCount, bool standardization, bool[,] layerLinks)
 {
     this.ConvolutionKernelCount = convolutionKernelCount;
     CnnKernelList = new List <CnnKernel>();
     for (int i = 0; i < ConvolutionKernelCount; i++)
     {
         int inputCount = 0;//单个卷积神经元输入数量
         for (int j = 0; j < LastLayerCount; j++)
         {
             if (layerLinks == null)
             {
                 inputCount = 1;
                 break;
             }
             if (layerLinks[i, j])
             {
                 inputCount++;
             }
         }
         CnnKernelList.Add(new CnnKernel(inputWidth, inputHeight,
                                         receptiveFieldWidth, receptiveFieldHeight,
                                         offsetWidth, offsetHeight, activationFunctionType, inputCount, ConvolutionKernelCount, standardization));
     }
 }
コード例 #4
0
 /// <summary>
 /// 增加后续卷积层(最后一层一定为单个输出,代表输出中的其中一个值)
 /// </summary>
 /// <param name="cnnConvolutionLayer"></param>
 public void AddCnnConvolutionLayer(int convolutionKernelCount,
                                    int receptiveFieldWidth, int receptiveFieldHeight,
                                    int offsetWidth, int offsetHeight, CnnNode.ActivationFunctionTypes activationFunctionType,
                                    int poolingReceptiveFieldWidth, int poolingReceptiveFieldHeight, CnnPooling.PoolingTypes poolingType,
                                    bool standardization, bool isFullLayerLinks)
 {
     foreach (Cnn cnn in cnnList)
     {
         cnn.AddCnnConvolutionLayer(convolutionKernelCount,
                                    receptiveFieldWidth, receptiveFieldHeight,
                                    offsetWidth, offsetHeight, activationFunctionType,
                                    poolingReceptiveFieldWidth, poolingReceptiveFieldHeight, poolingType,
                                    standardization, isFullLayerLinks);
     }
 }
コード例 #5
0
ファイル: Cnn.cs プロジェクト: Maggie123-huang/CnnDemo
        /// <summary>
        /// 增加首个卷积层
        /// </summary>
        /// <param name="cnnConvolutionLayer"></param>
        public void AddCnnConvolutionLayer(int convolutionKernelCount,
                                           int inputWidth, int inputHeight, int receptiveFieldWidth, int receptiveFieldHeight,
                                           int offsetWidth, int offsetHeight, CnnNode.ActivationFunctionTypes activationFunctionType,
                                           int poolingReceptiveFieldWidth, int poolingReceptiveFieldHeight, CnnPooling.PoolingTypes poolingType,
                                           bool standardization)
        {
            CnnConvolutionLayer cnnConvolutionLayer = new CnnConvolutionLayer();

            //创建卷积层
            cnnConvolutionLayer.CreateCnnKernel(convolutionKernelCount, inputWidth, inputHeight, receptiveFieldWidth, receptiveFieldHeight, offsetWidth, offsetHeight, activationFunctionType, 1, standardization, null);

            if (poolingType != 0)
            {
                //创建池化层
                cnnConvolutionLayer.CreateCnnPooling(poolingReceptiveFieldWidth, poolingReceptiveFieldHeight, activationFunctionType, poolingType);
            }
            CnnConvolutionLayerList.Add(cnnConvolutionLayer);
        }
コード例 #6
0
ファイル: Cnn.cs プロジェクト: Maggie123-huang/CnnDemo
 /// <summary>
 /// 增加全连接层,在卷积层后,要先创建完卷积层
 /// </summary>
 /// <param name="cnnFullLayer"></param>
 public void AddCnnFullLayer(int outputCount, CnnNode.ActivationFunctionTypes activationFunctionType, bool standardization)
 {
     if (CnnFullLayerList.Count == 0)
     {
         //连接卷积层
         var          cnnConvolutionLayerLast = CnnConvolutionLayerList[CnnConvolutionLayerList.Count - 1];//最后的卷积层
         CnnFullLayer cnnFullLayer            = new CnnFullLayer(cnnConvolutionLayerLast.ConvolutionKernelCount
                                                                 * cnnConvolutionLayerLast.OutputWidth
                                                                 * cnnConvolutionLayerLast.OutputHeight,
                                                                 outputCount, activationFunctionType, standardization);
         CnnFullLayerList.Add(cnnFullLayer);
     }
     else
     {
         var          cnnFullLayerLast = CnnFullLayerList[CnnFullLayerList.Count - 1];//最后的卷积层
         CnnFullLayer cnnFullLayer     = new CnnFullLayer(cnnFullLayerLast.OutputCount,
                                                          outputCount, activationFunctionType, standardization);
         CnnFullLayerList.Add(cnnFullLayer);
     }
 }
コード例 #7
0
ファイル: Cnn.cs プロジェクト: Maggie123-huang/CnnDemo
        /// <summary>
        /// 增加后续卷积层
        /// </summary>
        /// <param name="cnnConvolutionLayer"></param>
        public void AddCnnConvolutionLayer(int convolutionKernelCount,
                                           int receptiveFieldWidth, int receptiveFieldHeight,
                                           int offsetWidth, int offsetHeight, CnnNode.ActivationFunctionTypes activationFunctionType,
                                           int poolingReceptiveFieldWidth, int poolingReceptiveFieldHeight, CnnPooling.PoolingTypes poolingType,
                                           bool standardization, bool isFullLayerLinks)
        {
            var cnnConvolutionLayerLast = CnnConvolutionLayerList[CnnConvolutionLayerList.Count - 1];//最后的卷积层

            bool[,] layerLinks = new bool[convolutionKernelCount, cnnConvolutionLayerLast.ConvolutionKernelCount];
            if (!isFullLayerLinks)
            {
                //随机创建卷积层间连接
                Random random = new Random();
                for (int i = 0; i < convolutionKernelCount; i++)
                {
                    int linkCount = 0;    //每层链接数
                    while (linkCount < 2) //确保最低链接数
                    {
                        for (int j = 0; j < cnnConvolutionLayerLast.ConvolutionKernelCount; j++)
                        {
                            if (random.NextDouble() < 0.5)
                            {
                                layerLinks[i, j] = false;
                            }
                            else
                            {
                                layerLinks[i, j] = true;
                                linkCount++;
                            }
                        }
                    }
                    //排除相同链接
                    for (int j = 0; j < i; j++)
                    {
                        linkCount = 0;//相同链接数
                        for (int k = 0; k < cnnConvolutionLayerLast.ConvolutionKernelCount; k++)
                        {
                            if (layerLinks[i, k] == layerLinks[j, k])
                            {
                                linkCount++;
                            }
                        }
                        if (linkCount == cnnConvolutionLayerLast.ConvolutionKernelCount)
                        {
                            i--;
                            break;
                        }
                    }
                }
            }
            else
            {
                //全链接
                for (int i = 0; i < convolutionKernelCount; i++)
                {
                    for (int j = 0; j < cnnConvolutionLayerLast.ConvolutionKernelCount; j++)
                    {
                        layerLinks[i, j] = true;
                    }
                }
            }
            convolutionLinkList.Add(layerLinks);
            CnnConvolutionLayer cnnConvolutionLayer = new CnnConvolutionLayer();

            //创建卷积层
            cnnConvolutionLayer.CreateCnnKernel(convolutionKernelCount, cnnConvolutionLayerLast.OutputWidth, cnnConvolutionLayerLast.OutputHeight,
                                                receptiveFieldWidth, receptiveFieldHeight, offsetWidth, offsetHeight, activationFunctionType, cnnConvolutionLayerLast.ConvolutionKernelCount, standardization, layerLinks);

            if (poolingType != 0)
            {
                //创建池化层
                cnnConvolutionLayer.CreateCnnPooling(poolingReceptiveFieldWidth, poolingReceptiveFieldHeight, activationFunctionType, poolingType);
            }
            CnnConvolutionLayerList.Add(cnnConvolutionLayer);
        }
コード例 #8
0
 /// <summary>
 /// 创建池化层
 /// </summary>
 /// <param name="receptiveFieldWidth"></param>
 /// <param name="receptiveFieldHeight"></param>
 /// <param name="activationFunctionType"></param>
 public void CreateCnnPooling(int receptiveFieldWidth, int receptiveFieldHeight, CnnNode.ActivationFunctionTypes activationFunctionType, CnnPooling.PoolingTypes poolingType)
 {
     if (CnnKernelList == null || CnnKernelList.Count == 0)
     {
         throw new Exception("需先创建卷积层");
     }
     CnnPoolingList = new List <CnnPooling>();
     for (int i = 0; i < ConvolutionKernelCount; i++)
     {
         CnnPoolingList.Add(new CnnPooling(CnnKernelList[0].ConvolutionKernelWidth, CnnKernelList[0].ConvolutionKernelHeight,
                                           receptiveFieldWidth, receptiveFieldHeight, activationFunctionType, poolingType, CnnKernelList[0].InputCount, CnnKernelList[0].OutputCount));
     }
 }