Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //TestExp();
            int    T        = 1000;
            int    nFeature = 784;
            int    nLabel   = 10;
            double momentum = 0.0;

            IFunction[]    type          = new IFunction[] { new Sigmoid(1.0, 0.0), new Sigmoid(1.0, 0.0), new Sigmoid(1.0, 0.0) };
            IErrorFunction errorFunction = new MediumSquareErrorFunction();

            //IFunction type = new Tanh(0.666666, 1.7159, 0.0);

            double[]   eta          = new double[] { 1.0, 1.0, 1.0, 1.0 };
            double[]   dropoutValue = new double[] { 1.0, 1.0, 1.0 };
            int[]      nodeLayer    = new int[] { nFeature, 400, 20, nLabel };
            double[][] dataMatrix   = null;
            double[][] labelMatrix  = null;

            readDataset("data.dat", new[] { "  " }, ref dataMatrix);
            readDataset("tlabel.dat", new[] { " " }, ref labelMatrix);

            double[][] testDataMatrix  = null;
            double[][] testLabelMatrix = null;

            readDataset("testmnist.dat", new[] { "  " }, ref testDataMatrix);
            readDataset("tlabeltest.dat", new[] { " " }, ref testLabelMatrix);

            ANN network = new ANN(
                T,
                nodeLayer,
                eta,
                momentum,
                dropoutValue,
                type,
                errorFunction);

            network.SetThread(6);

            #region Start test error

            double[][] result = new double[testDataMatrix.Length][];
            for (int i = 0; i < testDataMatrix.Length; i++)
            {
                result[i] = network.GetNetworkOutput(testDataMatrix[i]);
            }

            result = ResultFinalize(result);
            Console.WriteLine("Network error " + GetError(result, testLabelMatrix));

            #endregion

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Reset();
            stopwatch.Start();

            double[][] resultTrain = new double[dataMatrix.Length][];
            int        nTrial      = 80;
            for (int i = 0; i < nTrial; i++)
            {
                network.Train(dataMatrix, labelMatrix, 10);

                for (int j = 0; j < testDataMatrix.Length; j++)
                {
                    result[j] = network.GetNetworkOutput(testDataMatrix[j]);
                }
                result = ResultFinalize(result);
                Console.WriteLine("Network test error " + GetError(result, testLabelMatrix));

                for (int j = 0; j < dataMatrix.Length; j++)
                {
                    resultTrain[j] = network.GetNetworkOutput(dataMatrix[j]);
                }
                resultTrain = ResultFinalize(resultTrain);
                Console.WriteLine("Network train error " + GetError(resultTrain, labelMatrix));
            }

            stopwatch.Stop();
            Console.WriteLine("Train Elapsed={0}", stopwatch.ElapsedMilliseconds * 0.001);



            result = new double[testDataMatrix.Length][];
            for (int i = 0; i < testDataMatrix.Length; i++)
            {
                result[i] = network.GetNetworkOutput(testDataMatrix[i]);
            }

            //Finalizzo il risultato
            for (int i = 0; i < testDataMatrix.Length; i++)
            {
                double max   = double.MinValue;
                int    index = 0;
                for (int j = 0; j < result[i].Length; j++)
                {
                    if (result[i][j] > max)
                    {
                        max   = result[i][j];
                        index = j;
                    }
                    result[i][j] = 0.0;
                }
                result[i][index] = 1.0;
            }
            Console.WriteLine("Network error " + GetError(result, testLabelMatrix));
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        static void RNNExample()
        {
            int    T        = 1000;
            int    nFeature = 6;
            int    nLabel   = 4;
            double momentum = 0.0;

            //IFunction[] type = new IFunction[] { new Sigmoid(1.0, 0.0), new SoftMax() };
            //IFunction[] type = new IFunction[] { new Tanh(0.666666, 1.7159, 0.0), new SoftMax() };
            //IFunction[] type = new IFunction[] { new Tanh(0.666666, 1.7159, 0.0), new SoftMax() };
            IFunction[] type = new IFunction[] { new Sigmoid(1.0, 0.0), new Sigmoid(1.0, 0.0) };
            //IErrorFunction errorFunction = new CrossEntropyErrorFunc();
            IErrorFunction errorFunction = new MediumSquareErrorFunction();

            double[] eta          = new double[] { 0.01, 0.01, 0.01 };
            double[] dropoutValue = new double[] { 1.0, 1.0 };
            int[]    nodeLayer    = new int[] { nFeature, 50, nLabel };
            int      timeLength   = 40;

            double[][] dataMatrix  = null;
            double[][] labelMatrix = null;

            //Training set
            GenerateDataset(timeLength, 12000, ref dataMatrix, ref labelMatrix);

            //readDataset("international-airline-passengers.csv", new[] { "  " }, ref dataMatrix);
            //readDataset("international-airline-passengers.csv", new[] { " " }, ref labelMatrix);

            //NormalizeDataColumn(ref dataMatrix);
            //NormalizeDataColumn(ref labelMatrix);

            RNN network = new RNN(
                T,
                timeLength,
                10,
                nodeLayer,
                eta,
                momentum,
                dropoutValue,
                type,
                errorFunction);

            int nTrial = 60;

            for (int i = 0; i < nTrial; i++)
            {
                int batch = 1;// Helper.GetRandom(1, 5);
                network.Train(dataMatrix, labelMatrix, batch, true);
                Console.WriteLine("Trial " + i);
                Console.WriteLine();
            }

            double[][] testDataMatrix  = null;
            double[][] testLabelMatrix = null;

            //Network test
            Console.WriteLine("TEST");
            double totErr = 0.0;

            for (int i = 0; i < 200; i++)
            {
                GenerateDataset(timeLength, timeLength, ref testDataMatrix, ref testLabelMatrix);
                double[] res = network.GetNetworkOutput(testDataMatrix);
                Console.WriteLine("Res " + res[0] + " " + res[1] + " " + res[2] + " " + res[3]);
                Console.WriteLine("Expected " + testLabelMatrix[timeLength - 1][0] + " " +
                                  testLabelMatrix[timeLength - 1][1] + " " +
                                  testLabelMatrix[timeLength - 1][2] + " " +
                                  testLabelMatrix[timeLength - 1][3] + " ");
                //double diff = (testLabelMatrix[timeLength - 1][0] - res[0]);
                //totErr = diff * diff;

                //Console.WriteLine("Diff " + diff);
                Console.WriteLine();
            }

            //Console.WriteLine("Test err " + totErr / 100);

            Console.ReadLine();
        }