public static double GetLoss(NeuralNet nn, Dataset ds) { double loss = 0.0; int prg = 0; Parallel.For(0, ds.Count(), RobustnessOptions.ParallelOptions, i => // for (int i =0; i < ds.Count(); i++) { double[] softmax = Utils.ULabel.RunWithSoftmax(nn, ds.GetDatum(i), true); lock (lockObject) { prg++; int lab = ds.GetLabel(i); // safety for infinity ... if (softmax[lab] == 0.0) { softmax[lab] += 1e-10; } loss += Math.Log(softmax[lab]); Console.Write("\r{0:0.000}%", (double)prg * 100.0 / ds.Count()); } }); Console.Write("\nTotal loss: "); Console.WriteLine(loss); return(loss); }
public static int[] Label(NeuralNet model, Dataset input) { int[] result = new int[input.Count()]; for (int i = 0; i < input.Count(); i++) { result[i] = Label(model, input.GetDatum(i), true); } return(result); }
public static LabelWithConfidence[] LabelWithConfidence(NeuralNet model, Dataset input) { LabelWithConfidence[] result = new LabelWithConfidence[input.Count()]; for (int i = 0; i < input.Count(); i++) { result[i] = LabelWithConfidence(model, input.GetDatum(i), true); } return(result); }
public static double GetAccuracy(NeuralNet nn, Dataset ds) { int cnt = 0; int prg = 0; // Parallel.For(0, ds.Count(), RobustnessOptions.ParallelOptions, i => for (int i = 0; i < ds.Count(); i++) { //if (i < 10000) { continue; } double[] datum; int ground_label; datum = ds.GetDatum(i); ground_label = ds.GetLabel(i); var labconf = Utils.ULabel.LabelWithConfidence(nn, ds.GetDatum(i), true); int label = labconf.actualLabel; // int label = Utils.ULabel.Label(nn, ds.GetDatum(i), true); lock (lockObject) { prg++; // Console.WriteLine("Confidence = {0}", labconf.softMaxValue); } if (label == ground_label) { lock (lockObject) { cnt++; } } else { // Console.WriteLine("Missclassifciation: " + label + " vs " + testImages.Dataset.GetLabel(i)); } lock (lockObject) { Console.Write("\r{0:0.000}%, Accuracy:{1:0.000}%", (double)prg * 100.0 / ds.Count(), (double)cnt * 100.0 / prg); // Utils.UDraw.DisplayImageAndPause(Utils.UArray.ToRGBArray(datum, 1.0, 0.0), 32, 32, true); } } ; Console.WriteLine("\nCorrectly classified = {0}", cnt); Console.WriteLine("Total images = {0}", ds.Count()); double acc = (double)cnt / ds.Count(); Console.Write("\nAccuracy: "); Console.WriteLine(acc); Console.WriteLine("ReLU Collisions = {0}", Instrumentation.Collisions); return(acc); }
public static void CalculateDistances(Dataset dataset) { double[,] avgdata = new double[dataset.LabelCount(), dataset.LabelCount()]; int[,] countdata = new int[dataset.LabelCount(), dataset.LabelCount()]; List <Tuple <double[], int> > data = new List <Tuple <double[], int> >(); for (int i = 0; i < dataset.Count(); i++) { data.Add(new Tuple <double[], int>(dataset.GetDatum(i), dataset.GetLabel(i))); } var query = data.GroupBy(x => x.Item2); foreach (IGrouping <int, Tuple <double[], int> > grp1 in query) { foreach (IGrouping <int, Tuple <double[], int> > grp2 in query) { if (grp1 == grp2) { continue; } foreach (var x1 in grp1) { foreach (var x2 in grp2) { double dist = UMath.L1Distance(x1.Item1, x2.Item1); avgdata[x1.Item2, x2.Item2] += dist; countdata[x1.Item2, x2.Item2]++; } } } } for (int i = 0; i < dataset.LabelCount(); i++) { for (int j = 0; j < dataset.LabelCount(); j++) { if (i == j) { continue; } avgdata[i, j] = avgdata[i, j] / (double)countdata[i, j]; } } Console.WriteLine("Distance statistics:"); for (int i = 0; i < dataset.LabelCount(); i++) { for (int j = 0; j < dataset.LabelCount(); j++) { if (i == j) { continue; } Console.WriteLine("Average distance, classes {0}-{1} = {2}", i, j, avgdata[i, j]); } } }
public static Dataset Filter(NeuralNet nn, Dataset ds, Func <NeuralNet, double[], int, bool> predicate) { Dataset ret = new Dataset(ds.LabelCount()); for (int i = 0; i < ds.Count(); i++) { double[] datum; int ground_label; datum = ds.GetDatum(i); ground_label = ds.GetLabel(i); if (predicate(nn, datum, ground_label)) { ret.Data.Add(new MemAccessor <double[]>(datum)); ret.Labels.Add(new MemAccessor <int>(ground_label)); } } return(ret); }