Beispiel #1
0
        private void MnistDemo()
        {
            BuilderInstance.Volume = new VolumeBuilder();

            var datasets = new DataSets();

            if (!datasets.Load(100))
            {
                return;
            }

            // Create network
            this._net = new Net <float>();
            this._net.AddLayer(new InputLayer(28, 28, 1));
            this._net.AddLayer(new ConvLayer(5, 5, 8)
            {
                Stride = 1, Pad = 2
            });
            this._net.AddLayer(new ReluLayer());
            this._net.AddLayer(new PoolLayer(2, 2)
            {
                Stride = 2
            });
            this._net.AddLayer(new ConvLayer(5, 5, 16)
            {
                Stride = 1, Pad = 2
            });
            this._net.AddLayer(new ReluLayer());
            this._net.AddLayer(new PoolLayer(3, 3)
            {
                Stride = 3
            });
            this._net.AddLayer(new FullyConnLayer(10));
            this._net.AddLayer(new SoftmaxLayer(10));

            // Fluent version
            //        this._net = FluentNet<float>.Create(24, 24, 1)
            //                   .Conv(5, 5, 8).Stride(1).Pad(2)
            //                   .Relu()
            //                   .Pool(2, 2).Stride(2)
            //                   .Conv(5, 5, 16).Stride(1).Pad(2)
            //                   .Relu()
            //                   .Pool(3, 3).Stride(3)
            //                   .FullyConn(10)
            //                   .Softmax(10)
            //                   .Build();

            this._trainer = new SgdTrainer(this._net)
            {
                LearningRate = 0.01f,
                BatchSize    = 1024,
                L2Decay      = 0.001f,
                Momentum     = 0.9f
            };

            Console.WriteLine("Convolutional neural network learning...[Press any key to stop]");
            do
            {
                var trainSample = datasets.Train.NextBatch(this._trainer.BatchSize);
                Train(trainSample.Item1, trainSample.Item2, trainSample.Item3);

                var testSample = datasets.Test.NextBatch(this._trainer.BatchSize);
                Test(testSample.Item1, testSample.Item3, this._testAccWindow);

                Console.WriteLine("Loss: {0} Train accuracy: {1}% Test accuracy: {2}%", this._trainer.Loss,
                                  Math.Round(this._trainAccWindow.Items.Average() * 100.0, 2),
                                  Math.Round(this._testAccWindow.Items.Average() * 100.0, 2));

                Console.WriteLine("Example seen: {0} Fwd: {1}ms Bckw: {2}ms Updt: {3}ms", this._stepCount,
                                  Math.Round(this._trainer.ForwardTimeMs, 2),
                                  Math.Round(this._trainer.BackwardTimeMs, 2),
                                  Math.Round(this._trainer.UpdateWeightsTimeMs, 2));
            } while (!Console.KeyAvailable);
        }
Beispiel #2
0
        private void MnistDemo()
        {
            BuilderInstance <float> .Volume = new ConvNetSharp.Volume.GPU.Single.VolumeBuilder();

            var datasets = new DataSets();

            if (!datasets.Load(100))
            {
                return;
            }

            // Create network
            this._net = new Net <float>();
            this._net.AddLayer(new InputLayer <float>());
            this._net.AddLayer(new ConvLayer <float>(5, 5, 8)
            {
                Stride = 1, Pad = 2, BiasPref = 0.1f
            });
            this._net.AddLayer(new ReluLayer <float>());
            this._net.AddLayer(new PoolLayer <float>(2, 2)
            {
                Stride = 2
            });
            this._net.AddLayer(new ConvLayer <float>(5, 5, 16)
            {
                Stride = 1, Pad = 2, BiasPref = 0.1f
            });
            this._net.AddLayer(new ReluLayer <float>());
            this._net.AddLayer(new PoolLayer <float>(3, 3)
            {
                Stride = 3
            });
            this._net.AddLayer(new FullyConnLayer <float>(10));
            this._net.AddLayer(new SoftmaxLayer <float>());

            // Fluent version
            //this._net = Net<float>.Create()
            //           .Conv(5, 5, 8).Stride(1).Pad(2)
            //           .Relu()
            //           .Pool(2, 2).Stride(2)
            //           .Conv(5, 5, 16).Stride(1).Pad(2)
            //           .Relu()
            //           .Pool(3, 3).Stride(3)
            //           .FullyConn(10)
            //           .Softmax()
            //           .Build();

            this._trainer = new SgdTrainer <float>(this._net, 0.01f)
            {
                BatchSize = 1024,
                //L2Decay = 0.001f,
                //Momentum = 0.9f
            };

            if (File.Exists("loss.csv"))
            {
                File.Delete("loss.csv");
            }

            Console.WriteLine("Convolutional neural network learning...[Press any key to stop]");
            do
            {
                var trainSample = datasets.Train.NextBatch(this._trainer.BatchSize);
                Train(trainSample.Item1, trainSample.Item2, trainSample.Item3);

                var testSample = datasets.Test.NextBatch(this._trainer.BatchSize);
                Test(testSample.Item1, testSample.Item3, this._testAccWindow);

                Console.WriteLine("Loss: {0} Train accuracy: {1}% Test accuracy: {2}%", this._trainer.Loss,
                                  Math.Round(this._trainAccWindow.Items.Average() * 100.0, 2),
                                  Math.Round(this._testAccWindow.Items.Average() * 100.0, 2));

                Console.WriteLine("Example seen: {0} Fwd: {1}ms Bckw: {2}ms Updt: {3}ms", this._stepCount,
                                  Math.Round(this._trainer.ForwardTimeMs, 2),
                                  Math.Round(this._trainer.BackwardTimeMs, 2),
                                  Math.Round(this._trainer.UpdateWeightsTimeMs, 2));

                File.AppendAllLines("loss.csv", new[] { $"{this._stepCount}, {this._trainer.Loss}, { Math.Round(this._trainAccWindow.Items.Average() * 100.0, 2)}, {Math.Round(this._testAccWindow.Items.Average() * 100.0, 2)}" });
            } while (!Console.KeyAvailable);


            // Display graph
            //var vm = new ViewModel<float>(_net.Op);
            //var app = new Application();
            //app.Run(new GraphControl { DataContext = vm });

            this._net.Dispose();
            this._trainer.Dispose();
        }