Пример #1
0
        // Classify data using trained network parameters and count classification errors
        public int Classify(ManagedArray test_input, ManagedArray test_output, int classes, int items, int batchsize, ManagedArray classification, bool pool = false)
        {
            var errors = 0;

            var tempx     = new ManagedArray(test_input.x, test_input.y, batchsize, false);
            var tempy     = new ManagedArray(batchsize, classes, false);
            var tempclass = new ManagedArray(1, batchsize, false);

            ManagedOps.Free(classification);

            classification = new ManagedArray(1, items, false);

            for (var i = 0; i < items; i += batchsize)
            {
                // generate batch
                ManagedOps.Copy3D(tempx, test_input, 0, 0, i);
                ManagedOps.Copy2D(tempy, test_output, i, 0);

                // classify
                FeedForward(tempx, pool);

                // count classifcation errors
                errors += Test(tempy, tempclass);

                // save classification
                ManagedOps.Copy2DOffset(classification, tempclass, i, 0);
            }

            ManagedOps.Free(tempx, tempy, tempclass);

            return(errors);
        }
Пример #2
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);
        }
Пример #3
0
        // Flip 3D Matrix along a dimension
        public static void FlipAll(ManagedArray dst, ManagedArray src)
        {
            dst.Resize(src.x, src.y, src.z, false);

            var tmp = new ManagedArray(src.x, src.y, src.z, false);

            ManagedOps.Copy3D(tmp, src, 0, 0, 0);

            for (int FlipDim = 0; FlipDim < 3; FlipDim++)
            {
                Flip(dst, tmp, FlipDim);

                ManagedOps.Copy3D(tmp, dst, 0, 0, 0);
            }

            ManagedOps.Free(tmp);
        }