public static void performEvaluatorTest(Net network, int[] expectedOutput, params double[] input) { var evaluator = new NetEvaluator(network); var actual = evaluator.Evaluate(DenseMatrix.Build.DenseOfRowArrays(input)); Assert.AreEqual(1, actual.RowCount); Assert.AreEqual(expectedOutput.Length, actual.ColumnCount); for (int i = 0; i < expectedOutput.Length; ++i) { int actualInt = (int)(Math.Round(actual[0, i])); Assert.AreEqual(expectedOutput[i], actualInt); } }
public CurrentEvaluator(TrainStorageManager tsm) { trainStorageManager = tsm; NetId = trainStorageManager.LoadDefaultNetName(); if (NetId != null) { var net = trainStorageManager.LoadNet(NetId); var settings = trainStorageManager.LoadTrainSettings(NetId); if (net != null && settings != null) { Evaluator = new NetEvaluator(net); TrainSettings = settings; } } }
public bool SetDefaultNetwork(string netId) { var net = trainStorageManager.LoadNet(netId); var settings = trainStorageManager.LoadTrainSettings(netId); if (net == null || settings == null) { return(false); } NetId = netId; Evaluator = new NetEvaluator(net); TrainSettings = settings; trainData = null; return(trainStorageManager.SaveDefaultNetName(netId)); }
public void TrainNetwork() { stopwatch.Reset(); stopwatch.Start(); sendMessage("Loading training settings."); trainSettings = storageManager.LoadTrainSettings(netId); if (trainSettings == null) { sendMessage("Train settings file was not found."); return; } using (var db = MausrDb.Create()) { sendMessage("Initializing learning environment."); trainData = new TrainData(); int inputSize = trainSettings.InputImgSizePx * trainSettings.InputImgSizePx; int outputSize = db.Symbols.Count(); var layout = new NetLayout(inputSize, trainSettings.HiddenLayersSizes, outputSize); network = new Net(layout, new SigomidActivationFunc(), new NetCostFunction()); network.SetOutputMap(db.Symbols.Select(s => s.SymbolId).ToArray()); netEvaluator = new NetEvaluator(network); var optimizer = createOptimizer(trainSettings); var trainer = new NetTrainer(network, optimizer, trainSettings.RegularizationLambda); unpackedCoefs = network.Layout.AllocateCoefMatrices(); sendMessage("Preparing inputs and outputs."); prepareInOut(db); sendMessage(string.Format("Learning of {0} samples started.", trainInputs.RowCount)); bool converged = trainer.TrainBatch(trainInputs, trainSettings.BatchSize, trainSettings.LearnRounds, trainOutIds, trainSettings.InitSeed, trainSettings.MinDerivCompMaxMagn, trainIterationCallback, job.CancellationToken); if (job.CancellationToken.IsCancellationRequested) { sendMessage("Training {0}.", job.Canceled ? "canceled" : "stopped"); } else { sendMessage("Training done ({0}converged).", converged ? "" : "not "); } if (!job.Canceled) { sendMessage("Saving trained data."); if (!storageManager.SaveNet(netId, network)) { sendMessage("Failed to save the network."); } if (!storageManager.SaveTrainData(netId, trainData)) { sendMessage("Failed to save training data."); } sendMessage("Saving results visualization."); if (!createAndSaveResultsVis(3, 0.01, 40)) { sendMessage("Failed visualize results."); } } stopwatch.Stop(); } sendMessage("All done."); }