Esempio n. 1
0
        public MnistSamples()
        {
            var sourceDir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent;
            var dataDir = Path.Combine(sourceDir.FullName, "MnistData");
            var dataFile = Path.Combine(dataDir, "Training.csv");

            var allSamples = File.ReadAllLines(dataFile)
                .Skip(1)
                .Select(line => new Sample(
                    Sample.GetValues(line.Substring(2)),
                    GetTargetArray(line.Substring(0, 1))))
                .ToArray();
            var testCount = allSamples.Length / 10;
            Training = new Sample[allSamples.Length - testCount];
            Array.Copy(allSamples, Training, Training.Length);
            Testing = new Sample[testCount];
            if (testCount > 0)
                Array.Copy(allSamples, Training.Length, Testing, 0, Testing.Length);
        }
Esempio n. 2
0
        public Trainer(Network net, Sample[] cases, 
            CheckCorrect checkCorrect, Sample[] testCases = null)
        {
            this.net = net;
            this.cases = cases;
            this.caseCount = cases.Length;

            if (caseCount == 0)
                throw new Exception("Don't have any samples to train with!");
            var sampleSample = cases[0];
            if (net.InputSize != sampleSample.input.Length)
                throw new Exception(string.Format(
                    "Net input size: {0}, doesn't match sample input size: {1}.",
                    net.InputSize, sampleSample.input.Length));
            if (net.OutputSize != sampleSample.target.Length)
                throw new Exception(string.Format(
                    "Net output size: {0}, doesn't match sample target size: {1}.",
                    net.OutputSize, sampleSample.target.Length));

            this.checkCorrect = checkCorrect;
            this.testCases = testCases ?? new Sample[0];
        }
Esempio n. 3
0
 Tuple<int, int> MeasureAccuracy(Sample[] samples)
 {
     var correctCount = (
         from sample in samples
         let output = net.FeedForward(sample.input)
         where checkCorrect(sample.target, output)
         select 1)
         .Count();
     return Tuple.Create(correctCount, samples.Length);
 }