public ClassifierRecurrentNeuralNetwork(int input, int output, int hidden, int units, float learningRate, float std, EnumOptimizerFunction opt)
        {
            encoder = new RecurrentCellModel();
            decoder = new DenseModel();

            int x = input;

            for (int i = 0; i < units; i++)
            {
                encoder.AddLayer(BuildedModels.RecurrentTanh(x, hidden, learningRate, std, opt));
                x = hidden;
            }

            decoder.AddLayer(BuildedModels.DenseSoftMax(hidden, output, learningRate, std, opt));
            decoder.SetLossFunction(new CrossEntropyLossFunction());

            input_size     = input;
            hidden_size    = hidden;
            output_size    = output;
            recurrentUnits = units;
            learning_rate  = learningRate;
        }
Example #2
0
        private static void Main(string[] args)
        {
            Console.Clear();

            var watch = Stopwatch.StartNew();

            ProcessingDevice.Device = DeviceType.CPU_Parallel;

            watch.Stop();
            Console.WriteLine($"Device Time: {watch.ElapsedMilliseconds}ms");

            var model = new DenseModel();

            model.AddLayer(BuildedModels.DenseLeakRelu(784, 100, 1e-4f, 2e-1f, EnumOptimizerFunction.RmsProp));
            model.AddLayer(BuildedModels.DenseLeakRelu(100, 30, 1e-4f, 2e-1f, EnumOptimizerFunction.RmsProp));
            model.AddLayer(BuildedModels.DenseSoftMax(30, 10, 1e-4f, 2e-1f, EnumOptimizerFunction.RmsProp));
            model.SetLossFunction(new CrossEntropyLossFunction());

            watch = Stopwatch.StartNew();
            watch.Stop();
            Console.WriteLine($"Sinapse Time: {watch.ElapsedMilliseconds}ms");

            MnistLoader.DataPath = path;
            var trainingValues = MnistLoader.OpenMnist();

            int cont      = 0;
            int sizeTrain = trainingValues.Count;

            var err = 100f;
            var e   = 0f;

            while (true)
            {
                watch = Stopwatch.StartNew();
                e     = 0f;
                int ct = 0;
                for (int i = 0; i < sizeTrain; i++)
                {
                    var index = i;

                    var inputs = new FloatArray(ArrayMethods.ByteToArray(trainingValues[index].pixels, 28, 28));
                    var target = new FloatArray(ArrayMethods.ByteToArray(trainingValues[index].label, 10));

                    //  Learning
                    e += model.Learn(inputs, target);

                    // Sample
                    if (ct % 1000 == 0)
                    {
                        Write(trainingValues[index].ToString());
                        Write(ArrayMethods.PrintArray(model.Output(inputs), 10));
                        Write("\n");
                    }

                    ct++;
                }

                err = 0.999f * err + 0.001f * e;

                cont++;
                watch.Stop();
                var time = watch.ElapsedMilliseconds;
                Console.Title =
                    $"Error: {err} --- TSPS (Training Sample per Second): {Math.Ceiling(1000d / ((double)time / (double)sizeTrain))}";
            }
        }