public void Init()
 {
     INeuralNetwork network = NetworkManager.NewSequential(TensorInfo.Image <Alpha8>(20, 20),
                                                           CuDnnNetworkLayers.Convolutional((5, 5), 20, ActivationType.Identity),
                                                           CuDnnNetworkLayers.Pooling(ActivationType.LeakyReLU),
                                                           CuDnnNetworkLayers.Convolutional((3, 3), 40, ActivationType.LeakyReLU),
                                                           CuDnnNetworkLayers.Pooling(ActivationType.LeakyReLU),
                                                           CuDnnNetworkLayers.FullyConnected(125, ActivationType.LeakyReLU),
                                                           CuDnnNetworkLayers.FullyConnected(64, ActivationType.LeakyReLU),
                                                           CuDnnNetworkLayers.Softmax(10));
 }
        public static async Task Main()
        {
            // Create the network
            INeuralNetwork network = NetworkManager.NewSequential(TensorInfo.Image <Alpha8>(28, 28),
                                                                  CuDnnNetworkLayers.Convolutional((5, 5), 20, ActivationType.Identity),
                                                                  CuDnnNetworkLayers.Pooling(ActivationType.LeakyReLU),
                                                                  CuDnnNetworkLayers.Convolutional((3, 3), 40, ActivationType.Identity),
                                                                  CuDnnNetworkLayers.Pooling(ActivationType.LeakyReLU),
                                                                  CuDnnNetworkLayers.FullyConnected(125, ActivationType.LeCunTanh),
                                                                  CuDnnNetworkLayers.Softmax(10));

            // Prepare the dataset
            ITrainingDataset trainingData = await Mnist.GetTrainingDatasetAsync(400); // Batches of 400 samples

            ITestDataset testData = await Mnist.GetTestDatasetAsync(p => Printf($"Epoch {p.Iteration}, cost: {p.Result.Cost}, accuracy: {p.Result.Accuracy}"));

            if (trainingData == null || testData == null)
            {
                Printf("Error downloading the datasets");
                Console.ReadKey();
                return;
            }

            // Setup and network training
            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, e) => cts.Cancel();
            TrainingSessionResult result = await NetworkManager.TrainNetworkAsync(network,
                                                                                  trainingData,
                                                                                  TrainingAlgorithms.AdaDelta(),
                                                                                  20, 0.5f,
                                                                                  TrackBatchProgress,
                                                                                  testDataset : testData, token : cts.Token);

            // Save the training reports
            string
                timestamp = DateTime.Now.ToString("yy-MM-dd-hh-mm-ss"),
                path      = Path.GetDirectoryName(Path.GetFullPath(Assembly.GetExecutingAssembly().Location)),
                dir       = Path.Combine(path ?? throw new InvalidOperationException("The dll path can't be null"), "TrainingResults", timestamp);

            Directory.CreateDirectory(dir);
            File.WriteAllText(Path.Combine(dir, $"{timestamp}_cost.py"), result.TestReports.AsPythonMatplotlibChart(TrainingReportType.Cost));
            File.WriteAllText(Path.Combine(dir, $"{timestamp}_accuracy.py"), result.TestReports.AsPythonMatplotlibChart(TrainingReportType.Accuracy));
            network.Save(new FileInfo(Path.Combine(dir, $"{timestamp}{NetworkLoader.NetworkFileExtension}")));
            File.WriteAllText(Path.Combine(dir, $"{timestamp}.json"), network.SerializeMetadataAsJson());
            File.WriteAllText(Path.Combine(dir, $"{timestamp}_report.json"), result.SerializeAsJson());
            Printf($"Stop reason: {result.StopReason}, elapsed time: {result.TrainingTime}");
            Console.ReadKey();
        }
Beispiel #3
0
        public void NetworkSerialization()
        {
            INeuralNetwork network = NetworkManager.NewSequential(TensorInfo.Image <Rgb24>(120, 120),
                                                                  CuDnnNetworkLayers.Convolutional(ConvolutionInfo.New(ConvolutionMode.CrossCorrelation), (10, 10), 20, ActivationType.AbsoluteReLU),
                                                                  CuDnnNetworkLayers.Convolutional(ConvolutionInfo.New(ConvolutionMode.Convolution, 2, 2), (5, 5), 20, ActivationType.ELU),
                                                                  CuDnnNetworkLayers.Convolutional(ConvolutionInfo.Default, (10, 10), 20, ActivationType.Identity),
                                                                  CuDnnNetworkLayers.Pooling(PoolingInfo.New(PoolingMode.AverageIncludingPadding, 2, 2, 1, 1), ActivationType.ReLU),
                                                                  CuDnnNetworkLayers.Convolutional(ConvolutionInfo.Default, (10, 10), 20, ActivationType.Identity),
                                                                  CuDnnNetworkLayers.Pooling(PoolingInfo.Default, ActivationType.ReLU),
                                                                  CuDnnNetworkLayers.FullyConnected(125, ActivationType.Tanh),
                                                                  CuDnnNetworkLayers.FullyConnected(27, ActivationType.Tanh),
                                                                  CuDnnNetworkLayers.Softmax(133));

            using (MemoryStream stream = new MemoryStream())
            {
                network.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                INeuralNetwork copy = NetworkLoader.TryLoad(stream, ExecutionModePreference.Cuda);
                Assert.IsTrue(network.Equals(copy));
            }
        }
        public void ForwardTest2()
        {
            INeuralNetwork cpu = NetworkManager.NewSequential(TensorInfo.Image <Alpha8>(28, 28),
                                                              CuDnnNetworkLayers.Convolutional(ConvolutionInfo.Default, (5, 5), 20, ActivationType.Identity),
                                                              CuDnnNetworkLayers.Pooling(PoolingInfo.Default, ActivationType.ReLU),
                                                              CuDnnNetworkLayers.Convolutional(ConvolutionInfo.Default, (3, 3), 20, ActivationType.Identity),
                                                              CuDnnNetworkLayers.Pooling(PoolingInfo.Default, ActivationType.ReLU),
                                                              CuDnnNetworkLayers.FullyConnected(100, ActivationType.LeCunTanh),
                                                              CuDnnNetworkLayers.FullyConnected(50, ActivationType.LeCunTanh),
                                                              CuDnnNetworkLayers.Softmax(10));
            INeuralNetwork gpu = NetworkManager.NewGraph(TensorInfo.Image <Alpha8>(28, 28), root =>
            {
                var conv1 = root.Layer(_ => cpu.Layers[0].Clone());
                var pool1 = conv1.Layer(_ => cpu.Layers[1].Clone());
                var conv2 = pool1.Layer(_ => cpu.Layers[2].Clone());
                var pool2 = conv2.Layer(_ => cpu.Layers[3].Clone());
                var fc1   = pool2.Layer(_ => cpu.Layers[4].Clone());
                var fc2   = fc1.Layer(_ => cpu.Layers[5].Clone());
                fc2.Layer(_ => cpu.Layers[6].Clone());
            });

            ForwardTest(cpu, gpu);
        }