private static void SampleXor() { SigmaEnvironment sigma = SigmaEnvironment.Create("logical"); sigma.SetRandomSeed(0); sigma.Prepare(); RawDataset dataset = new RawDataset("xor"); dataset.AddRecords("inputs", new[] { 0, 0 }, new[] { 0, 1 }, new[] { 1, 0 }, new[] { 1, 1 }); dataset.AddRecords("targets", new[] { 0 }, new[] { 0 }, new[] { 0 }, new[] { 1 }); ITrainer trainer = sigma.CreateTrainer("xor-trainer"); trainer.Network.Architecture = InputLayer.Construct(2) + FullyConnectedLayer.Construct(2) + FullyConnectedLayer.Construct(1) + OutputLayer.Construct(1) + SquaredDifferenceCostLayer.Construct(); trainer.TrainingDataIterator = new MinibatchIterator(1, dataset); trainer.AddNamedDataIterator("validation", new UndividedIterator(dataset)); trainer.Optimiser = new GradientDescentOptimiser(learningRate: 0.1); trainer.Operator = new CudaSinglethreadedOperator(); trainer.AddInitialiser("*.*", new GaussianInitialiser(standardDeviation: 0.05)); trainer.AddLocalHook(new StopTrainingHook(atEpoch: 10000)); trainer.AddLocalHook(new AccumulatedValueReporter("optimiser.cost_total", TimeStep.Every(1, TimeScale.Epoch), averageValues: true)); trainer.AddLocalHook(new AccumulatedValueReporter("optimiser.cost_total", TimeStep.Every(1, TimeScale.Stop), averageValues: true)); trainer.AddLocalHook(new ValueReporter("network.layers.*<external_output>._outputs.default.activations", TimeStep.Every(1, TimeScale.Stop))); trainer.AddLocalHook(new ValueReporter("network.layers.*-fullyconnected.weights", TimeStep.Every(1, TimeScale.Stop))); trainer.AddLocalHook(new ValueReporter("network.layers.*-fullyconnected.biases", TimeStep.Every(1, TimeScale.Stop))); sigma.Run(); }
private static void SampleLoadExtractIterate() { SigmaEnvironment sigma = SigmaEnvironment.Create("test"); sigma.Prepare(); //var irisReader = new CsvRecordReader(new MultiSource(new FileSource("iris.data"), new UrlSource("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"))); //IRecordExtractor irisExtractor = irisReader.Extractor("inputs2", new[] { 0, 3 }, "targets2", 4).AddValueMapping(4, "Iris-setosa", "Iris-versicolor", "Iris-virginica"); //irisExtractor = irisExtractor.Preprocess(new OneHotPreprocessor(sectionName: "targets2", minValue: 0, maxValue: 2), new NormalisingPreprocessor(sectionNames: "inputs2", minInputValue: 0, maxInputValue: 6)); ByteRecordReader mnistImageReader = new ByteRecordReader(headerLengthBytes: 16, recordSizeBytes: 28 * 28, source: new CompressedSource(new MultiSource(new FileSource("train-images-idx3-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz")))); IRecordExtractor mnistImageExtractor = mnistImageReader.Extractor("inputs", new[] { 0L, 0L }, new[] { 28L, 28L }).Preprocess(new NormalisingPreprocessor(0, 255)); ByteRecordReader mnistTargetReader = new ByteRecordReader(headerLengthBytes: 8, recordSizeBytes: 1, source: new CompressedSource(new MultiSource(new FileSource("train-labels-idx1-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz")))); IRecordExtractor mnistTargetExtractor = mnistTargetReader.Extractor("targets", new[] { 0L }, new[] { 1L }).Preprocess(new OneHotPreprocessor(minValue: 0, maxValue: 9)); IComputationHandler handler = new CpuFloat32Handler(); ExtractedDataset dataset = new ExtractedDataset("mnist-training", ExtractedDataset.BlockSizeAuto, mnistImageExtractor, mnistTargetExtractor); IDataset[] slices = dataset.SplitRecordwise(0.8, 0.2); IDataset trainingData = slices[0]; IDataset validationData = slices[1]; MinibatchIterator trainingIterator = new MinibatchIterator(1, trainingData); MinibatchIterator validationIterator = new MinibatchIterator(1, validationData); while (true) { foreach (var block in trainingIterator.Yield(handler, sigma)) { Thread.Sleep(100); PrintFormattedBlock(block, PrintUtils.AsciiGreyscalePalette); Thread.Sleep(1000); } } //Random random = new Random(); //INDArray array = new ADNDArray<float>(3, 1, 2, 2); //new GaussianInitialiser(0.05, 0.05).Initialise(array, Handler, random); //Console.WriteLine(array); //new ConstantValueInitialiser(1).Initialise(array, Handler, random); //Console.WriteLine(array); //dataset.InvalidateAndClearCaches(); }
private static void SampleIris() { SigmaEnvironment sigma = SigmaEnvironment.Create("iris"); sigma.SetRandomSeed(0); sigma.Prepare(); IDataset dataset = Defaults.Datasets.Iris(); ITrainer trainer = sigma.CreateGhostTrainer("iris-trainer"); trainer.Network.Architecture = InputLayer.Construct(4) + FullyConnectedLayer.Construct(12) + FullyConnectedLayer.Construct(3) + OutputLayer.Construct(3) + SquaredDifferenceCostLayer.Construct(); //trainer.Network = Serialisation.ReadBinaryFileIfExists("iris.sgnet", trainer.Network); trainer.TrainingDataIterator = new MinibatchIterator(50, dataset); trainer.AddNamedDataIterator("validation", new UndividedIterator(dataset)); trainer.Optimiser = new GradientDescentOptimiser(learningRate: 0.06); trainer.Operator = new CudaSinglethreadedOperator(); trainer.AddInitialiser("*.*", new GaussianInitialiser(standardDeviation: 0.1)); //trainer.AddGlobalHook(new StopTrainingHook(atEpoch: 100)); //trainer.AddLocalHook(new EarlyStopperHook("optimiser.cost_total", 20, target: ExtremaTarget.Min)); trainer.AddLocalHook(new AccumulatedValueReporter("optimiser.cost_total", TimeStep.Every(1, TimeScale.Epoch), reportEpochIteration: true)); //.On(new ExtremaCriteria("optimiser.cost_total", ExtremaTarget.Min))); //trainer.AddLocalHook(new DiskSaviorHook<INetwork>("network.self", Namers.Dynamic("iris_epoch{0}.sgnet", "epoch"), verbose: true) // .On(new ExtremaCriteria("optimiser.cost_total", ExtremaTarget.Min))); trainer.AddHook(new MultiClassificationAccuracyReporter("validation", TimeStep.Every(1, TimeScale.Epoch), tops: 1)); trainer.AddHook(new StopTrainingHook(new ThresholdCriteria("shared.classification_accuracy_top1", ComparisonTarget.GreaterThanEquals, 0.98))); trainer.AddLocalHook(new RunningTimeReporter(TimeStep.Every(599, TimeScale.Iteration), 128)); trainer.AddLocalHook(new RunningTimeReporter(TimeStep.Every(1, TimeScale.Epoch), 4)); //Serialisation.WriteBinaryFile(trainer, "trainer.sgtrainer"); //trainer = Serialisation.ReadBinaryFile<ITrainer>("trainer.sgtrainer"); sigma.AddTrainer(trainer); sigma.AddMonitor(new HttpMonitor("http://+:8080/sigma/")); sigma.PrepareAndRun(); }
private static void Main() { SigmaEnvironment.EnableLogging(); SigmaEnvironment sigma = SigmaEnvironment.Create("Sigma-MNIST"); // create a new mnist trainer ITrainer trainer = CreateMnistTrainer(sigma); // for the UI we have to activate more features if (UI) { // create and attach a new UI framework WPFMonitor gui = sigma.AddMonitor(new WPFMonitor("MNIST")); // create a tab gui.AddTabs("Overview"); // access the window inside the ui thread gui.WindowDispatcher(window => { // enable initialisation window.IsInitializing = true; // add a panel that controls the learning process window.TabControl["Overview"].AddCumulativePanel(new ControlPanel("Control", trainer)); // create an accuracy cost that updates every iteration var cost = new TrainerChartPanel <CartesianChart, LineSeries, TickChartValues <double>, double>("Cost", trainer, "optimiser.cost_total", TimeStep.Every(1, TimeScale.Iteration)); // improve the chart performance cost.Fast(); // add the newly created panel window.TabControl["Overview"].AddCumulativePanel(cost); // finish initialisation window.IsInitializing = false; }); // the operators should not run instantly but when the user clicks play sigma.StartOperatorsOnRun = false; } sigma.Prepare(); sigma.Run(); }
private static void Main() { SigmaEnvironment.EnableLogging(); SigmaEnvironment sigma = SigmaEnvironment.Create("sigma_demo"); // create a new mnist trainer string name = DemoMode.Name; ITrainer trainer = DemoMode.CreateTrainer(sigma); trainer.AddLocalHook(new MetricProcessorHook <INDArray>("network.layers.*.weights", (a, h) => h.Divide(h.Sum(a), a.Length), "shared.network_weights_average")); trainer.AddLocalHook(new MetricProcessorHook <INDArray>("network.layers.*.weights", (a, h) => h.StandardDeviation(a), "shared.network_weights_stddev")); trainer.AddLocalHook(new MetricProcessorHook <INDArray>("network.layers.*.biases", (a, h) => h.Divide(h.Sum(a), a.Length), "shared.network_biases_average")); trainer.AddLocalHook(new MetricProcessorHook <INDArray>("network.layers.*.biases", (a, h) => h.StandardDeviation(a), "shared.network_biases_stddev")); trainer.AddLocalHook(new MetricProcessorHook <INDArray>("optimiser.updates", (a, h) => h.Divide(h.Sum(a), a.Length), "shared.optimiser_updates_average")); trainer.AddLocalHook(new MetricProcessorHook <INDArray>("optimiser.updates", (a, h) => h.StandardDeviation(a), "shared.optimiser_updates_stddev")); trainer.AddLocalHook(new MetricProcessorHook <INDArray>("network.layers.*<external_output>._outputs.default.activations", (a, h) => h.Divide(h.Sum(a), a.Length), "shared.network_activations_mean")); // create and attach a new UI framework WPFMonitor gui = sigma.AddMonitor(new WPFMonitor(name, DemoMode.Language)); gui.ColourManager.Dark = DemoMode.Dark; gui.ColourManager.PrimaryColor = DemoMode.PrimarySwatch; StatusBarLegendInfo iris = new StatusBarLegendInfo(name, MaterialColour.Blue); StatusBarLegendInfo general = new StatusBarLegendInfo("General", MaterialColour.Grey); gui.AddLegend(iris); gui.AddLegend(general); // create a tab gui.AddTabs("Overview", "Metrics", "Validation", "Maximisation", "Reproduction", "Update"); // access the window inside the ui thread gui.WindowDispatcher(window => { // enable initialisation window.IsInitializing = true; window.TabControl["Metrics"].GridSize = new GridSize(2, 4); window.TabControl["Validation"].GridSize = new GridSize(2, 5); window.TabControl["Maximisation"].GridSize = new GridSize(2, 5); window.TabControl["Reproduction"].GridSize = new GridSize(2, 5); window.TabControl["Update"].GridSize = new GridSize(1, 1); window.TabControl["Overview"].GridSize.Rows -= 1; window.TabControl["Overview"].GridSize.Columns -= 1; // add a panel that controls the learning process window.TabControl["Overview"].AddCumulativePanel(new ControlPanel("Control", trainer), legend: iris); ITimeStep reportTimeStep = DemoMode.Slow ? TimeStep.Every(1, TimeScale.Iteration) : TimeStep.Every(10, TimeScale.Epoch); var cost1 = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Cost / Epoch", trainer, "optimiser.cost_total", TimeStep.Every(1, TimeScale.Epoch)).Linearify(); var cost2 = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Cost / Epoch", trainer, "optimiser.cost_total", reportTimeStep); var weightAverage = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Mean of Weights / Epoch", trainer, "shared.network_weights_average", reportTimeStep, averageMode: true).Linearify(); var weightStddev = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Standard Deviation of Weights / Epoch", trainer, "shared.network_weights_stddev", reportTimeStep, averageMode: true).Linearify(); var biasesAverage = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Mean of Biases / Epoch", trainer, "shared.network_biases_average", reportTimeStep, averageMode: true).Linearify(); var biasesStddev = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Standard Deviation of Biases / Epoch", trainer, "shared.network_biases_stddev", reportTimeStep, averageMode: true).Linearify(); var updateAverage = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Mean of Parameter Updates / Epoch", trainer, "shared.optimiser_updates_average", reportTimeStep, averageMode: true).Linearify(); var updateStddev = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Standard Deviation of Parameter Updates / Epoch", trainer, "shared.optimiser_updates_stddev", reportTimeStep, averageMode: true).Linearify(); var outputActivationsMean = CreateChartPanel <CartesianChart, GLineSeries, GearedValues <double>, double>("Mean of Output Activations", trainer, "shared.network_activations_mean", reportTimeStep, averageMode: true).Linearify(); AccuracyPanel accuracy1 = null, accuracy2 = null; if (DemoMode != DemoType.Wdbc && DemoMode != DemoType.Parkinsons) { accuracy1 = new AccuracyPanel("Validation Accuracy", trainer, DemoMode.Slow ? TimeStep.Every(1, TimeScale.Epoch) : reportTimeStep, null, 1, 2); accuracy1.Fast().Linearify(); accuracy2 = new AccuracyPanel("Validation Accuracy", trainer, DemoMode.Slow ? TimeStep.Every(1, TimeScale.Epoch) : reportTimeStep, null, 1, 2); accuracy2.Fast().Linearify(); } IRegistry regTest = new Registry(); regTest.Add("test", DateTime.Now); var parameter = new ParameterPanel("Parameters", sigma, window); parameter.Add("Time", typeof(DateTime), regTest, "test"); ValueSourceReporter valueHook = new ValueSourceReporter(TimeStep.Every(1, TimeScale.Epoch), "optimiser.cost_total"); trainer.AddGlobalHook(valueHook); sigma.SynchronisationHandler.AddSynchronisationSource(valueHook); var costBlock = (UserControlParameterVisualiser)parameter.Content.Add("Cost", typeof(double), trainer.Operator.Registry, "optimiser.cost_total"); costBlock.AutoPollValues(trainer, TimeStep.Every(1, TimeScale.Epoch)); var learningBlock = (UserControlParameterVisualiser)parameter.Content.Add("Learning rate", typeof(double), trainer.Operator.Registry, "optimiser.learning_rate"); learningBlock.AutoPollValues(trainer, TimeStep.Every(1, TimeScale.Epoch)); var paramCount = (UserControlParameterVisualiser)parameter.Content.Add("Parameter count", typeof(long), trainer.Operator.Registry, "network.parameter_count"); paramCount.AutoPollValues(trainer, TimeStep.Every(1, TimeScale.Start)); window.TabControl["Overview"].AddCumulativePanel(cost1, 1, 2, legend: iris); window.TabControl["Overview"].AddCumulativePanel(parameter); //window.TabControl["Overview"].AddCumulativePanel(accuracy1, 1, 2, legend: iris); //window.TabControl["Metrics"].AddCumulativePanel(cost2, legend: iris); //window.TabControl["Metrics"].AddCumulativePanel(weightAverage, legend: iris); //window.TabControl["Metrics"].AddCumulativePanel(biasesAverage, legend: iris); window.TabControl["Update"].AddCumulativePanel(updateAverage, legend: iris); if (accuracy2 != null) { window.TabControl["Metrics"].AddCumulativePanel(accuracy2, legend: iris); } window.TabControl["Metrics"].AddCumulativePanel(weightStddev, legend: iris); window.TabControl["Metrics"].AddCumulativePanel(biasesStddev, legend: iris); window.TabControl["Metrics"].AddCumulativePanel(updateStddev, legend: iris); window.TabControl["Metrics"].AddCumulativePanel(outputActivationsMean, legend: iris); if (DemoMode == DemoType.Mnist) { NumberPanel outputpanel = new NumberPanel("Numbers", trainer); DrawPanel drawPanel = new DrawPanel("Draw", trainer, 560, 560, 20, outputpanel); window.TabControl["Validation"].AddCumulativePanel(drawPanel, 2, 3); window.TabControl["Validation"].AddCumulativePanel(outputpanel, 2); window.TabControl["Validation"].AddCumulativePanel(weightAverage); window.TabControl["Validation"].AddCumulativePanel(biasesAverage); for (int i = 0; i < 10; i++) { window.TabControl["Maximisation"].AddCumulativePanel(new MnistBitmapHookPanel($"Target Maximisation {i}", i, trainer, TimeStep.Every(1, TimeScale.Epoch))); } } if (DemoMode == DemoType.TicTacToe) { window.TabControl["Overview"].AddCumulativePanel(new TicTacToePanel("Play TicTacToe!", trainer)); } //for (int i = 0; i < 10; i++) //{ // window.TabControl["Reproduction"].AddCumulativePanel(new MnistBitmapHookPanel($"Target Maximisation 7-{i}", 8, 28, 28, trainer, TimeStep.Every(1, TimeScale.Start))); //} }); if (DemoMode == DemoType.Mnist) { sigma.AddMonitor(new HttpMonitor("http://+:8080/sigma/")); } // the operators should not run instantly but when the user clicks play sigma.StartOperatorsOnRun = false; sigma.Prepare(); sigma.RunAsync(); gui.WindowDispatcher(window => window.IsInitializing = false); }