Example #1
0
    public static void CalculateNeabours <T>
        (CudaContext context,
        DeviceDataSet <T> teaching,
        DeviceDataSet <T> test,
        CudaDeviceVariable <int> calculatedNeabours,
        int threadsPerBlock
        ) where T : struct
    {
        var kernel = context.LoadKernel("kernels/VectorReduction.ptx", "calculateNearestNeabours");

        kernel.GridDimensions  = test.length / threadsPerBlock + 1;
        kernel.BlockDimensions = threadsPerBlock;

        kernel.SetConstantVariable("testVectorsCount", test.length);
        kernel.SetConstantVariable("teachingVectorsCount", teaching.length);
        kernel.SetConstantVariable("attributeCount", teaching.attributeCount);

        using (var deviceDistanceMemory =
                   new CudaDeviceVariable <float>(teaching.length * test.length))
        {
            kernel.Run(
                teaching.vectors.DevicePointer,
                test.vectors.DevicePointer,
                deviceDistanceMemory.DevicePointer,
                calculatedNeabours.DevicePointer
                );
            Thrust.sort_by_key_multiple(deviceDistanceMemory, calculatedNeabours, teaching.length, test.length);
        }
    }
Example #2
0
    public VectorReductionAccuracy(CudaContext context, DeviceDataSet <int> teaching, DeviceDataSet <int> test, int popSize)
    {
        this.teaching = teaching;
        this.test     = test;
        this.popSize  = popSize;
        this.context  = context;

        calculatedNeabours = new CudaDeviceVariable <int>(teaching.length * test.length);
        deviceAccuracy     = new CudaDeviceVariable <float>(popSize);

        Profiler.Start("calculate neabours");
        Neabours.CalculateNeabours(context, teaching, test, calculatedNeabours, ThreadsPerBlock);
        Profiler.Stop("calculate neabours");


        accuracyKernel = context.LoadKernel("kernels/VectorReduction.ptx", "calculateAccuracy");
        dim3 gridDimension = new dim3()
        {
            x = (uint)(test.length / ThreadsPerBlock + 1),
            y = (uint)popSize,
            z = 1
        };

        accuracyKernel.GridDimensions  = gridDimension;
        accuracyKernel.BlockDimensions = ThreadsPerBlock;

        accuracyKernel.SetConstantVariable("testVectorsCount", test.length);
        accuracyKernel.SetConstantVariable("teachingVectorsCount", teaching.length);
        accuracyKernel.SetConstantVariable("attributeCount", teaching.attributeCount);
        accuracyKernel.SetConstantVariable("genLength", teaching.length);

        K           = 3;
        CountToPass = 2;
    }
Example #3
0
    static void ReduceVectors(CudaDataSet <int> teaching, CudaDataSet <int> test)
    {
        using (CudaContext context = new CudaContext())
        {
            int popSize = 100;
            DeviceDataSet <int> deviceTeaching = new DeviceDataSet <int>(teaching);
            DeviceDataSet <int> deviceTest     = new DeviceDataSet <int>(test);

            FlattArray <byte> initialPopulation;

            VectorReductionAccuracy acc = new VectorReductionAccuracy(context, deviceTeaching, deviceTest, popSize)
            {
                K           = 5,
                CountToPass = 3
            };

            VectorReductionFitness fitnessFunc =
                new VectorReductionFitness(context, acc, popSize, deviceTeaching.length)
            {
                Alpha = 0.7f
            };

            //Drop3 drop = new Drop3();
            //drop.CasheSize = 5;
            //drop.K = 3;

            //Profiler.Start("Drop3");
            //var indexesToStay = drop.Apply(teaching, context);
            //Profiler.Stop("Drop3");


            //byte[] parrent = new byte[teaching.Vectors.GetLength(0)];
            //foreach (var item in indexesToStay)
            //{
            //    parrent[item] = 1;
            //}



            initialPopulation = CreateRandomPopulation(popSize, deviceTeaching.length);
            //CreatePopulationBasedOnParent(parrent, popSize, 0.2f, 0.05f);

            var d = new Evolutionary2(context, fitnessFunc, initialPopulation)
            {
                Elitism      = 0.001f,
                MutationRate = 0.001f
            };
            for (int i = 0; i < 30; i++)
            {
                Profiler.Start("iteration");
                d.CreateNewPopulation();
                Profiler.Stop("iteration");
            }
            var best = d.FindFitest();
            Console.WriteLine(acc.GenAccuracy(best.index) / (float)deviceTest.length);
            Console.WriteLine(fitnessFunc.GenLength(best.index));

            Profiler.Print();
        }
    }
Example #4
0
    static void ReduceDimension(CudaDataSet <int> teaching, CudaDataSet <int> test)
    {
        using (CudaContext context = new CudaContext())
        {
            int popSize   = 20;
            int genLength = teaching.Vectors.GetLength(1);

            DeviceDataSet <int> deviceTeaching = new DeviceDataSet <int>(teaching);
            DeviceDataSet <int> deviceTest     = new DeviceDataSet <int>(test);


            FlattArray <byte> initialPopulation;

            DimensionReductionAccuracy accuracy = new DimensionReductionAccuracy(context, deviceTeaching, deviceTest, popSize)
            {
                K           = 3,
                CountToPass = 2
            };

            DimensionReductionFitness fitnessFunc =
                new DimensionReductionFitness(context, accuracy, popSize, genLength)
            {
                Alpha = 1f
            };


            Console.WriteLine(accuracy.BaseAccuracy());
            Console.WriteLine();
            initialPopulation = CreateRandomPopulation(popSize, deviceTeaching.attributeCount);



            var d = new Evolutionary2(context, fitnessFunc, initialPopulation)
            {
                CrossOverRate = 0.5f,
                MutationRate  = 0.02f,
                Elitism       = 0.2f
            };

            for (int i = 0; i < 200; i++)
            {
                Profiler.Start("iteration");
                d.CreateNewPopulation();
                Profiler.Stop("iteration");
            }
            var best = d.FindFitest();
            // Console.WriteLine(best.fitness);
            var acc = accuracy.GenAccuracy(best.index);
            var len = fitnessFunc.GenLength(best.index);
            var gen = d.genGen(best.index);
            foreach (var item in gen)
            {
                Console.Write(item + " ");
            }

            Console.WriteLine($"accuracy: {acc / (float)deviceTest.length} length: {len / (float)genLength}");
            Console.WriteLine();
            //  Profiler.Print();
        }
    }
Example #5
0
    static void ReduceVectorsRegresion(CudaDataSet <float> teaching, CudaDataSet <float> test)
    {
        using (CudaContext context = new CudaContext())
        {
            int popSize        = 100;
            var deviceTeaching = new DeviceDataSet <float>(teaching);
            var deviceTest     = new DeviceDataSet <float>(test);

            FlattArray <byte> initialPopulation;

            var acc = new VectorReductionAccuracyRegresion(context, deviceTeaching, deviceTest, popSize)
            {
                K           = 3,
                CountToPass = 2
            };

            VectorReductionFitness fitnessFunc =
                new VectorReductionFitness(context, acc, popSize, deviceTeaching.length)
            {
                Alpha = 0.7f
            };

            initialPopulation = CreateRandomPopulation(popSize, deviceTeaching.length);

            var d = new Evolutionary2(context, fitnessFunc, initialPopulation)
            {
                Elitism      = 0.1f,
                MutationRate = 0.05f
            };
            for (int i = 0; i < 10; i++)
            {
                Profiler.Start("iteration");
                d.CreateNewPopulation();
                Profiler.Stop("iteration");
            }
            Console.WriteLine(acc.BaseAccuracy());
            var best = d.FindFitest();
            Console.WriteLine(acc.GenAccuracy(best.index));
            Console.WriteLine(fitnessFunc.GenLength(best.index) / (float)deviceTeaching.length);

            Profiler.Print();
        }
    }
    public DimensionReductionAccuracy(
        CudaContext context,
        DeviceDataSet <int> teaching,
        DeviceDataSet <int> test,
        int popSize
        )
    {
        this.popSize  = popSize;
        this.teaching = teaching;
        this.test     = test;
        this.context  = context;

        accuracyKernel = context.LoadKernel
                         (
            "kernels/dimensionsReductions.ptx",
            "geneticKnn"
                         );

        accuracyKernel.GridDimensions = new dim3()
        {
            x = (uint)(test.vectors.Size / ThreadsPerBlock) + 1,
            y = (uint)popSize,
            z = 1
        };
        accuracyKernel.BlockDimensions = ThreadsPerBlock;

        K           = 3;
        CountToPass = 2;
        accuracyKernel.SetConstantVariable("atributeCount", test.attributeCount);
        accuracyKernel.SetConstantVariable("teachingVectorsCount", teaching.length);
        accuracyKernel.SetConstantVariable("testVectorsCount", test.length);
        accuracyKernel.SetConstantVariable("popSize", popSize);
        accuracyKernel.DynamicSharedMemory = (uint)(test.attributeCount * sizeof(float));


        saveCasheKernel = context.LoadKernel(
            "kernels/dimensionsReductions.ptx",
            "saveToCashe"
            );
        saveCasheKernel.GridDimensions  = (popSize * 32) / ThreadsPerBlock + 1;
        saveCasheKernel.BlockDimensions = ThreadsPerBlock;
        saveCasheKernel.SetConstantVariable("atributeCount", teaching.attributeCount);
        saveCasheKernel.SetConstantVariable("popSize", teaching.attributeCount);


        readCasheKernel = context.LoadKernel(
            "kernels/dimensionsReductions.ptx",
            "readCashe"
            );
        readCasheKernel.GridDimensions  = 1;
        readCasheKernel.BlockDimensions = popSize;
        readCasheKernel.SetConstantVariable("atributeCount", teaching.attributeCount);


        casheTreeRoot = new Node()
        {
            mutex = 0,
            one   = (IntPtr)0,
            zero  = (IntPtr)0,
        };

        isInCashe = new CudaDeviceVariable <byte>(popSize);
        accuracy  = new CudaDeviceVariable <float>(popSize);
    }