コード例 #1
0
        /// <summary>
        /// Loads the standard data from file.
        /// </summary>
        /// <returns>Collection with datas</returns>
        public static TrainingCollection LoadStandardData()
        {
            const int HOW_MANY_NUMBERS_TO_LEARN_FROM_FILE = 1000;
            TrainingCollection collection = new TrainingCollection(HOW_MANY_NUMBERS_TO_LEARN_FROM_FILE);

            GiveItemsFromFile itemReturner = new GiveItemsFromFile();
            sbyte[,] expectedResultFromFile = itemReturner.GiveArrayOfExpectedResults();
            sbyte[,] trainDataFromFile = itemReturner.GiveArrayOfTrainData();
            const int NUMBER_OF_RESULTS = 10;
            const int SIZE_OF_IMAGE = 28 * 28;

            for (int numberCounter = 0; numberCounter < HOW_MANY_NUMBERS_TO_LEARN_FROM_FILE; numberCounter++)
            {
                double[] expectedResult = new double[NUMBER_OF_RESULTS];
                int whatNumber = -1;
                for (int i = 0; i < NUMBER_OF_RESULTS; i++)
                {
                    expectedResult[i] = expectedResultFromFile[numberCounter, i];
                    if (expectedResult[i] == 1)
                        whatNumber = i;
                }

                double[] trainData = new double[SIZE_OF_IMAGE];
                for (int i = 0; i < SIZE_OF_IMAGE; i++)
                {
                    trainData[i] = trainDataFromFile[numberCounter, i];
                }

                TrainingSet trainingSet = new TrainingSet();
                trainingSet.Input = trainData;
                trainingSet.Response = expectedResult;
                trainingSet.Value = whatNumber;

                collection.Values.Add(trainingSet);
            }

            return collection;
        }
コード例 #2
0
        // Test for recognizing digits from ubyte file and recognizing theirs labels
        private void TestRecognizingNumbersFromFile()
        {
            GiveItemsFromFile itemReturner = new GiveItemsFromFile();
            sbyte[,] expectedResultFromFile = itemReturner.GiveArrayOfExpectedResults();
            sbyte[,] trainDataFromFile = itemReturner.GiveArrayOfTrainData();
            const int sizeOfImage = 28 * 28; // heightOfImage * widthOfImage (28 x 28 pixel);

            for (int numberCounter = 0; numberCounter < expectedResultFromFile.GetLength(0); numberCounter++)
            {
                int index = 0;
                double[] expectedResult = new double[NUMBER_OF_RESULTS];
                for (int i = 0; i < NUMBER_OF_RESULTS; i++)
                {
                    expectedResult[i] = expectedResultFromFile[numberCounter, i];
                    if (expectedResult[i] == 1)
                        index = i;
                }

                double[] trainData = new double[sizeOfImage];
                for (int i = 0; i < sizeOfImage; i++)
                {
                    trainData[i] = trainDataFromFile[numberCounter, i];
                }

                _graphics.Clear(Color.White);
                Bitmap bitmap = (Bitmap)_neuralNetworkGUI.GiveImageFromDrawingArea();
                for (int w = 0; w < HEIGHTH_FOR_BITMAP; w++)
                {
                    for (int k = 0; k < WIDTH_FOR_BITMAP; k++)
                    {
                        if (trainData[w * WIDTH_FOR_BITMAP + k] == 1)
                            bitmap.SetPixel(k, w, Color.Black);
                    }
                }
                _neuralNetworkGUI.RefreshDrawingArea();
                _neuralNetworkGUI.SelectNumber(index);
            }
        }