void drawCelsiusFaringate2()
        {
            var ira = new IRINA_NN(new int[] { 1, 6, 9, 3, 1 }, 0.2);

            var input = new List <double[]>
            {
                new double[] { 0.040 },
                new double[] { 0.035 },
                new double[] { 0.037 },
                new double[] { 0.048 },
                new double[] { -0.012 },
                new double[] { -0.040 },
                new double[] { 0.057 },
                new double[] { 0.089 },
                new double[] { -0.005 },
                new double[] { 0.100 },
                new double[] { -0.042 }
            };

            var target = new List <double[]>
            {
                new double[] { 0.104 },
                new double[] { 0.095 },
                new double[] { 0.0986 },
                new double[] { 0.1184 },
                new double[] { 0.0104 },
                new double[] { -0.040 },
                new double[] { 0.1346 },
                new double[] { 0.1922 },
                new double[] { 0.023 },
                new double[] { 0.212 },
                new double[] { -0.0436 }
            };

            var samplesCount = input.Count;

            var random = new Random();

            for (int epoch = 0; epoch < count; ++epoch)
            {
                var index = random.Next(0, samplesCount);

                var inputItem  = input[index];
                var targetItem = target[index];

                ira.Train(inputItem, targetItem);

                var error = IRINA_NN.MSE(ira.Predict(inputItem), inputItem);

                if (epoch % even == 0)
                {
                    SeriesCollection[0].Values.Add(error);
                }
            }

            var result = ira.Predict(new double[] { 0.67 })[0];

            result = 0d;
        }
        void drawXor()
        {
            var ira = new IRINA_NN(new int[] { 2, 4, 4, 1 }, 0.2);

            var e1 = ira.Predict(input: new double[] { 0.99, 0.99 })[0];
            var e2 = ira.Predict(input: new double[] { 0.99, 0.01 })[0];
            var e3 = ira.Predict(input: new double[] { 0.01, 0.99 })[0];
            var e4 = ira.Predict(input: new double[] { 0.01, 0.01 })[0];

            var error = (e1 + e2 + e3 + e4) / 4d;

            for (int epoch = 0; epoch < count; ++epoch)
            {
                ira.Train(
                    input: new double[] { 0.01, 0.01 },
                    target: new double[] { 0.01 }
                    );

                e1 = IRINA_NN.MSE(ira.Predict(new double[] { 0.01, 0.01 }), new double[] { 0.01 });

                ira.Train(
                    input: new double[] { 0.99, 0.01 },
                    target: new double[] { 0.99 }
                    );

                e2 = IRINA_NN.MSE(ira.Predict(new double[] { 0.99, 0.01 }), new double[] { 0.99 });

                ira.Train(
                    input: new double[] { 0.01, 0.99 },
                    target: new double[] { 0.99 }
                    );

                e3 = IRINA_NN.MSE(ira.Predict(new double[] { 0.01, 0.99 }), new double[] { 0.99 });

                ira.Train(
                    input: new double[] { 0.99, 0.99 },
                    target: new double[] { 0.01 }
                    );

                e4 = IRINA_NN.MSE(ira.Predict(new double[] { 0.99, 0.99 }), new double[] { 0.01 });

                error = (e1 + e2 + e3 + e4) / 4d;

                //lbOutput.Items.Add($"Ошибка эпохи { epoch }: { error }");

                if (epoch % even == 0)
                {
                    SeriesCollection[0].Values.Add(error);
                }
            }

            e2 = ira.Predict(input: new double[] { 0.99, 0.01 })[0];
            e1 = ira.Predict(input: new double[] { 0.99, 0.99 })[0];
            e4 = ira.Predict(input: new double[] { 0.01, 0.01 })[0];
            e3 = ira.Predict(input: new double[] { 0.01, 0.99 })[0];
            e1 = e2 = e3 = e4;
        }
        void drawCelsiusFaringate()
        {
            var ira = new IRINA_NN(new int[] { 8, 16, 32, 16, 8 }, 0.2);

            for (int epoch = 0; epoch < count; ++epoch)
            {
                ira.Train(                 // 40 - 104
                    input: new double[] { 0.01, 0.01, 0.99, 0.01, 0.99, 0.01, 0.01, 0.01 },
                    target: new double[] { 0.01, 0.99, 0.99, 0.01, 0.99, 0.01, 0.01, 0.01 }
                    );

                var e1 = IRINA_NN.MSE(ira.Predict(new double[] { 0.01, 0.01, 0.99, 0.01, 0.99, 0.01, 0.01, 0.01 }), new double[] { 0.01, 0.99, 0.99, 0.01, 0.99, 0.01, 0.01, 0.01 });

                ira.Train(                 // 60 - 140
                    input: new double[] { 0.01, 0.01, 0.99, 0.99, 0.99, 0.99, 0.01, 0.01 },
                    target: new double[] { 0.99, 0.01, 0.01, 0.01, 0.01, 0.99, 0.99, 0.01 }
                    );

                var e2 = IRINA_NN.MSE(ira.Predict(new double[] { 0.01, 0.01, 0.99, 0.99, 0.99, 0.99, 0.01, 0.01 }), new double[] { 0.99, 0.01, 0.01, 0.01, 0.01, 0.99, 0.99, 0.01 });

                ira.Train(                 // 15 - 59
                    input: new double[] { 0.01, 0.01, 0.01, 0.01, 0.99, 0.99, 0.99, 0.99 },
                    target: new double[] { 0.01, 0.01, 0.99, 0.99, 0.99, 0.01, 0.99, 0.99 }
                    );

                var e3 = IRINA_NN.MSE(ira.Predict(new double[] { 0.01, 0.01, 0.01, 0.01, 0.99, 0.99, 0.99, 0.99 }), new double[] { 0.01, 0.01, 0.99, 0.99, 0.99, 0.01, 0.99, 0.99 });

                ira.Train(                 // 25 - 77
                    input: new double[] { 0.01, 0.01, 0.01, 0.99, 0.99, 0.01, 0.01, 0.99 },
                    target: new double[] { 0.01, 0.99, 0.01, 0.01, 0.99, 0.99, 0.01, 0.99 }
                    );

                var e4 = IRINA_NN.MSE(ira.Predict(new double[] { 0.01, 0.01, 0.01, 0.99, 0.99, 0.01, 0.01, 0.99 }), new double[] { 0.01, 0.99, 0.01, 0.01, 0.99, 0.99, 0.01, 0.99 });

                var error = (e1 + e2 + e3 + e4) / 4d;

                if (epoch % even == 0)
                {
                    SeriesCollection[0].Values.Add(error);
                }
            }

            var predicted = ira.Predict(new double[] { 0.01, 0.01, 0.01, 0.99, 0.99, 0.01, 0.01, 0.99 });
            var target    = new double[] { 0.01, 0.99, 0.01, 0.01, 0.99, 0.99, 0.01, 0.99 };

            var err = IRINA_NN.MSE(predicted, target);

            predicted = ira.Predict(new double[] { 0.01, 0.01, 0.99, 0.01, 0.01, 0.01, 0.99, 0.99 });
            target    = new double[] { 0.01, 0.99, 0.01, 0.99, 0.99, 0.99, 0.99, 0.99 };

            err = IRINA_NN.MSE(predicted, target);
        }
Пример #4
0
        public static void Export(IRINA_NN network, string fileName)
        {
            try
            {
                using (var fileStream = new FileStream(fileName, FileMode.Create))
                {
                    BinaryFormatter formatter = new BinaryFormatter();

                    formatter.Serialize(fileStream, network);
                }
            }
            catch (Exception e)
            {
                throw new Exception("Ошибка экспорта", e);
            }
        }
        void drawIris()
        {
            var ira = new IRINA_NN(new int[] { 4, 16, 64, 24, 3 }, 0.02);

            IrisDataReader irisDataReader = new IrisDataReader(@"D:\NeuralNetworks\DataSets\Iris Plants Database\bezdekIris.data");

            var rand = new Random();

            for (int epoch = 0; epoch < count; ++epoch)
            {
                var irisData = irisDataReader.Read(rand.Next(0, 150));
                var(input, target) = irisToNN(irisData);

                ira.Train(input, target);

                var predict = ira.Predict(input);

                var error = IRINA_NN.MSE(predict, target);

                if (epoch % even == 0)
                {
                    SeriesCollection[0].Values.Add(error);
                }
            }

            var data = irisDataReader.Read(25);

            var(inputRes, targetRes) = irisToNN(data);

            var predictRes = ira.Predict(inputRes);

            var errorRes = IRINA_NN.MSE(predictRes, targetRes);

            data = irisDataReader.Read(72);
            (inputRes, targetRes) = irisToNN(data);

            predictRes = ira.Predict(inputRes);

            errorRes = IRINA_NN.MSE(predictRes, targetRes);

            data = irisDataReader.Read(119);
            (inputRes, targetRes) = irisToNN(data);

            predictRes = ira.Predict(inputRes);

            errorRes = IRINA_NN.MSE(predictRes, targetRes);
        }
		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			ira = IRINA_NN.Import(@"mnist.nn");
			reader = new MNISTDataReader.ImageDataReader(@"D:\NeuralNetworks\DataSets\MNIST\t10k-images.idx3-ubyte");
			lablesReader = new MNISTDataReader.LabelDataReader(@"D:\NeuralNetworks\DataSets\MNIST\t10k-labels.idx1-ubyte");

			Predict();

			//IrisDataReader irisDataReader = new IrisDataReader(@"D:\NeuralNetworks\DataSets\Iris Plants Database\bezdekIris.data");

			//var rand = new Random();

			//while(true)
			//{
			//	irisDataReader.Read(rand.Next(0, 150));
			//}

			//calcError();
		}
        void drawMNIST()
        {
            var ira = new IRINA_NN(new int[] { 784, 200, 10 }, 0.2);

            var imageReader = new MNISTDataReader.ImageDataReader(@"D:\NeuralNetworks\DataSets\MNIST\train-images.idx3-ubyte");

            var labelReader = new MNISTDataReader.LabelDataReader(@"D:\NeuralNetworks\DataSets\MNIST\train-labels.idx1-ubyte");

            var itemsCount = imageReader.ItemsCount;
            var random     = new Random();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            for (int epoch = 0; epoch < count; ++epoch)
            {
                //var errors = new List<double>();

                for (int sample = 0; sample < samplesInBatch; ++sample)
                {
                    var realIndex = random.Next(0, itemsCount);
                    var input     = prepareInput(imageReader.Read(realIndex));
                    var label     = labelReader.Read(realIndex);
                    var target    = prepareTarget(label);

                    ira.Train(input, target);

                    //var predicted = ira.Predict(input);
                    //errors.Add(0.99 - predicted[label]);
                }

                //SeriesCollection[0].Values.Add(errors.Average());
            }

            stopwatch.Stop();

            IRINA_NN.Export(ira, @"mnist.nn");

            MessageBox.Show($"Complete { count } epochs, { samplesInBatch } samples per one. Estimated time: { stopwatch.ElapsedMilliseconds }ms");
        }