Esempio n. 1
0
        public void Train(ManagedArray input, ManagedArray output, ConvolutionalNeuralNetworkOptions opts)
        {
            var temp_input  = new ManagedArray(input.x, input.y, opts.BatchSize, false);
            var temp_output = new ManagedArray(opts.BatchSize, output.y, false);

            var index_list = new ManagedIntList(opts.Items);

            for (var epoch = 0; epoch < opts.Epochs; epoch++)
            {
                var start = Profiler.now();

                if (opts.Shuffle)
                {
                    ManagedOps.Shuffle(index_list);
                }

                var rLVal = 0.0;

                rL.Clear();

                for (var i = 0; i < opts.Items; i += opts.BatchSize)
                {
                    if (opts.Shuffle)
                    {
                        ManagedOps.Copy3D(temp_input, input, 0, 0, i, index_list);
                        ManagedOps.Copy2D(temp_output, output, i, 0, index_list);
                    }
                    else
                    {
                        ManagedOps.Copy3D(temp_input, input, 0, 0, i);
                        ManagedOps.Copy2D(temp_output, output, i, 0);
                    }

                    FeedForward(temp_input, opts.Pool);
                    BackPropagation(temp_output);
                    ApplyGradients(opts);

                    if (rL.Count == 0)
                    {
                        rL.Add(L);
                    }

                    rLVal = 0.99 * rL[rL.Count - 1] + 0.01 * L;

                    rL.Add(rLVal);
                }

                Console.WriteLine("epoch {0}/{1} elapsed time is {2} ms - Error: {3}", (epoch + 1).ToString("D", ManagedMatrix.ci), opts.Epochs.ToString("D", ManagedMatrix.ci), Profiler.Elapsed(start).ToString("D", ManagedMatrix.ci), rLVal.ToString("0.000000", ManagedMatrix.ci));
            }

            ManagedOps.Free(index_list);

            ManagedOps.Free(temp_input, temp_output);
        }