예제 #1
0
 public static void Learn()
 {
     net = new Perceptron();
     net.Load(Settings.Instance.NetworkFileName);
     net.Learn(TrainingSet.Load(Settings.Instance.DataDirectoryPath));
     net.Save(Settings.Instance.NetworkFileName);
 }
예제 #2
0
        public void TrainOnMemory(IConcurrentMemory <TData> memory)
        {
            var trainingData = _dataBuilder.BuildDataset(memory);

            if (trainingData == null || trainingData.Count == 0)
            {
                return;
            }

            var clonedInstance = _network.Clone();
            // Train the network
            var result = NetworkManager.TrainNetwork(clonedInstance,
                                                     trainingData,
                                                     TrainingAlgorithms.AdaDelta(),
                                                     _configuration.Epochs, 0.5f,
                                                     TrackBatchProgress,
                                                     TrainingProgress);

            Console.WriteLine("\nTraining session completed, moving to next one");

            var backupName = $"backup-network-{DateTime.Now:yyyyMMdd-HH-mm-ss-fff}.modl";

            _network.Save(File.Create(backupName));
            Console.WriteLine($"Backup model {backupName} saved");
            _network = clonedInstance;
        }
예제 #3
0
        public void Save([NotNull] Stream stream, [CanBeNull] ITyresMachineExtras extras)
        {
            using (var zip = new ZipArchive(stream, ZipArchiveMode.Create, true)) {
                Add("Options.json", _options);
                Add("Input/Sources.json", _tyresSources);
                Add("Input/LUTs.json", _luts.Select(x => x.Where(y => y.Value != null).ToDictionary(y => y.Key, y => y.Value.ToString())));
                Add("Input/Normalizations.json", _inputNormalizations);
                Add("Output/Keys.json", _outputKeys);
                Add("Output/Normalizations.json", _outputNormalizations);

                var manifest = _loadedManifest ?? new JObject();
                manifest["version"]      = SaveFormatVersion;
                manifest["tyresVersion"] = TyresVersion;

                _loadedManifest = manifest;
                extras?.OnSave(zip, manifest, this);
                Add("Manifest.json", manifest);

                if (_networks != null)
                {
                    for (var i = 0; i < _networks.Length; i++)
                    {
                        var key         = _outputKeys[i];
                        var network     = _networks[i];
                        var networkType = network.GetType().FullName;
                        var data        = network.Save();
                        if (networkType == null || data == null)
                        {
                            continue;
                        }
                        zip.AddString($"Networks/{key}/Type.txt", networkType);
                        zip.AddBytes($"Networks/{key}/Data.bin", data);
                    }
                }
                else if (_singleNetwork != null)
                {
                    var networkType = _singleNetwork.GetType().FullName;
                    var data        = _singleNetwork.Save();
                    if (networkType != null && data != null)
                    {
                        zip.AddString("Network/Type.txt", networkType);
                        zip.AddBytes("Network/Data.bin", data);
                    }
                }

                void Add(string key, object data)
                {
                    zip.AddString(key, JsonConvert.SerializeObject(data, Formatting.Indented));
                }
            }
        }
예제 #4
0
        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();
        }
        public void NetworkSerialization()
        {
            INeuralNetwork network = NetworkManager.NewSequential(TensorInfo.Image <Rgb24>(120, 120),
                                                                  NetworkLayers.Convolutional((10, 10), 20, ActivationFunctionType.AbsoluteReLU),
                                                                  NetworkLayers.Convolutional((5, 5), 20, ActivationFunctionType.ELU),
                                                                  NetworkLayers.Convolutional((10, 10), 20, ActivationFunctionType.Identity),
                                                                  NetworkLayers.Pooling(ActivationFunctionType.ReLU),
                                                                  NetworkLayers.Convolutional((10, 10), 20, ActivationFunctionType.Identity),
                                                                  NetworkLayers.Pooling(ActivationFunctionType.ReLU),
                                                                  NetworkLayers.FullyConnected(125, ActivationFunctionType.Tanh),
                                                                  NetworkLayers.Softmax(133));

            using (MemoryStream stream = new MemoryStream())
            {
                network.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                INeuralNetwork copy = NetworkLoader.TryLoad(stream, LayersLoadingPreference.Cpu);
                Assert.IsTrue(network.Equals(copy));
            }
        }
예제 #6
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));
            }
        }
예제 #7
0
 public void Save(INeuralNetwork network)
 {
     network.Save(_file);
 }
        private void Save(INeuralNetwork network)
        {
            var timestamp = DateTime.UtcNow.ToString("u").Replace("-", "").Replace(":", "").Replace("T", "");

            network.Save($"{network.Name}-{timestamp}.nn");
        }
예제 #9
0
 public static void Create(int[] layersSizes)
 {
     net = new Perceptron(layersSizes);
     net.Initialize();
     net.Save(Settings.Instance.NetworkFileName);
 }
예제 #10
0
 public static void Create(int[] layersSizes) {
     net = new Perceptron(layersSizes);
     net.Initialize();
     net.Save(Settings.Instance.NetworkFileName);
 }
예제 #11
0
 public static void Learn() {
     net = new Perceptron();
     net.Load(Settings.Instance.NetworkFileName);
     net.Learn(TrainingSet.Load(Settings.Instance.DataDirectoryPath));
     net.Save(Settings.Instance.NetworkFileName);
 }