Example #1
0
 public void Backprop(Layer output)
 {
     Errors = new double[Length];
     for (int k = 0; k < output.Length; k++)
     {
         for (int j = 0; j < Length; j++)
         {
             Errors[j] += output.Weights[k, j] * ActivationFunctions.TanhDerriv(output.Values[k]) * output.Errors[k];
         }
     }
 }
Example #2
0
 public void Calculate(double[] input, bool output)
 {
     Values = new double[Length];
     for (int k = 0; k < Length; k++)
     {
         for (int j = 0; j < InputLength; j++)
         {
             Values[k] += Weights[k, j] * input[j];
         }
         if (!output)
         {
             Values[k] += Biases[k];
             Values[k]  = ActivationFunctions.Tanh(Values[k]);
         }
         else
         {
             Values[k] = Values[k];
         }
     }
 }
Example #3
0
        //Read a matrix from a file offset by two bytes of metadata
        public static double[,] ReadNextImage()
        {
            //Singleton
            if (ImageReaderRunning)
            {
                throw new Exception("Already accessing file");
            }

            //Read image
            FileStream fs = File.OpenRead(ImagePath);

            //Reset parameters and decrement NN hyperparameters upon new epoch (currently disabled)
            if (!(ImageOffset < fs.Length))
            {
                ImageOffset = 16; LabelOffset = 8;
            }
            fs.Position = ImageOffset;
            byte[] b = new byte[Resolution * Resolution];
            try
            {
                fs.Read(b, 0, Resolution * Resolution);
            }
            catch (Exception ex) { Console.WriteLine("Reader exception: " + ex.ToString()); Console.ReadLine(); }
            int[] array = Array.ConvertAll(b, Convert.ToInt32);
            ImageOffset += Resolution * Resolution;
            //Convert to 2d array
            double[,] result = new double[Resolution, Resolution];
            //Convert array to doubles and store in result
            for (int i = 0; i < Resolution; i++)
            {
                for (int ii = 0; ii < Resolution; ii++)
                {
                    result[i, ii] = (double)array[(Resolution * i) + ii];
                }
            }
            //Normalize the result matrix
            ActivationFunctions.Normalize(result, Resolution, Resolution);

            fs.Close();
            return(result);
        }