private int[] ApplyRest(CudaContext context, CudaDataSet <int> data)
    {
        int vectorsCount   = data.Vectors.GetLength(0);
        int attributeCount = data.Vectors.GetLength(1);

        var kernel = context.LoadKernel("kernels/drop3.ptx", "findNeighbours");

        kernel.GridDimensions  = data.Vectors.GetLength(0) / ThreadsPerBlock + 1;
        kernel.BlockDimensions = ThreadsPerBlock;

        using (CudaDeviceVariable <int> d_classes = data.Classes)
            using (CudaDeviceVariable <float> vectors = data.Vectors.Raw)
                using (var heapMemory = new CudaDeviceVariable <HeapData>(data.Vectors.GetLength(0) * CasheSize))
                    using (var nearestEnemyDistances = new CudaDeviceVariable <float>(data.Vectors.GetLength(0)))
                    {
                        kernel.Run(
                            vectors.DevicePointer,
                            data.Vectors.GetLength(0),
                            data.Vectors.GetLength(1),
                            CasheSize,
                            d_classes.DevicePointer,
                            heapMemory.DevicePointer,
                            nearestEnemyDistances.DevicePointer
                            );



                        float[]   hostNearestEnemy = nearestEnemyDistances;
                        float[][] hostVectors      = data.Vectors.To2d();

                        var Neighbors        = new FlattArray <HeapData>(heapMemory, CasheSize);
                        var nearestNeighbors = new int[vectorsCount][];
                        for (int i = 0; i < vectorsCount; i++)
                        {
                            nearestNeighbors[i] = new int[CasheSize];

                            for (int j = 0; j < CasheSize; j++)
                            {
                                nearestNeighbors[i][j] = Neighbors[i, j].label;
                            }
                        }

                        HostDataset host = data.ToHostDataSet();
                        SortDataDesc(host, nearestNeighbors, hostNearestEnemy);


                        return(proccesData(context, host, nearestNeighbors));
                    }
    }
    public static void Sort(HostDataset host, int[] sortingIndeces)
    {
        float[][] sortedVectors = new float[host.Vectors.Length][];
        int[]     sortedClasses = new int[host.Classes.Length];
        int[]     sortedIndeces = new int[host.OrginalIndeces.Length];

        for (int i = 0; i < sortingIndeces.Length; i++)
        {
            sortedVectors[i] = host.Vectors[sortingIndeces[i]];
            sortedClasses[i] = host.Classes[sortingIndeces[i]];
            sortedIndeces[i] = host.OrginalIndeces[sortingIndeces[i]];
        }
        host.Vectors        = sortedVectors;
        host.Classes        = sortedClasses;
        host.OrginalIndeces = sortedIndeces;
    }
Exemple #3
0
        public void SortDataTest()
        {
            var vectors = new float[][] {
                new float[] { 1, 2, 3 },
                new float[] { 3, 2, 4 },
                new float[] { 7, 8, 9 },
                new float[] { 6, 5, 8 }
            };
            var classes         = new int[] { 1, 1, 2, 2 };
            var nearestNaboures = new int[][] {
                new int [] { 1, 2 },
                new int [] { 3, 0 },
                new int [] { 0, 1 },
                new int [] { 0, 2 },
            };
            var sortBy = new float[] { 3, 4, 2, 1 };

            var originaIndeces = new int[] { 0, 1, 2, 3 };

            HostDataset s = new HostDataset()
            {
                Vectors        = vectors,
                Classes        = classes,
                OrginalIndeces = originaIndeces
            };

            Drop3.SortDataDesc(s, nearestNaboures, sortBy);

            Assert.AreEqual(1, s.OrginalIndeces[0]);
            Assert.AreEqual(0, s.OrginalIndeces[1]);
            Assert.AreEqual(2, s.OrginalIndeces[2]);
            Assert.AreEqual(3, s.OrginalIndeces[3]);

            Assert.AreEqual(vectors[0], s.Vectors[1]);

            Assert.AreEqual(0, nearestNaboures[1][0]);
            Assert.AreEqual(2, nearestNaboures[1][1]);
        }
 public static void Sort(HostDataset host, float[] sortBy)
 {
     Sort(host, SortingIndeces(sortBy));
 }
    int[] proccesData(CudaContext context, HostDataset host, int[][] nearestNeighbors)
    {
        int len = host.Vectors.Length;
        int initialCasheSize = nearestNeighbors[0].Length - K;

        List <int>[] associates = new List <int> [len];
        for (int i = 0; i < len; i++)
        {
            associates[i] = new List <int>();
        }


        byte[] isInDaaset          = new byte[len];
        int[]  nextNearestNabour   = new int[len];
        int[]  nearestNaboursSizes = new int[len];

        for (int i = 0; i < isInDaaset.Length; i++)
        {
            isInDaaset[i]          = 1;
            nextNearestNabour[i]   = K + 1;
            nearestNaboursSizes[i] = initialCasheSize + K;
        }
        NeighborFinder finder = new NeighborFinder(context, new FlattArray <float>(host.Vectors), initialCasheSize / 2);

        Func <int, int, int> isClasifiedCorectly =
            (int vectorToCheck, int removed) =>
        {
            int correctCount = 0;
            int myClass      = host.Classes[vectorToCheck];
            for (int i = 0; i < K; i++)
            {
                if (nearestNeighbors[vectorToCheck][i] != removed &&
                    host.Classes[nearestNeighbors[vectorToCheck][i]] == myClass)
                {
                    correctCount++;
                }
            }

            if (correctCount >= K / 2)
            {
                return(1);
            }
            else
            {
                return(0);
            }
        };


        Action <int, int> findNewNearestNeabout =
            (int vector, int removed) =>
        {
            int toRemove = -1;
            for (int i = 0; i < K; i++)
            {
                if (removed == nearestNeighbors[vector][i])
                {
                    toRemove = i;
                    break;
                }
            }

            for (int i = nextNearestNabour[vector]; i < nearestNaboursSizes[vector]; i++)
            {
                if (isInDaaset[nearestNeighbors[vector][i]] == 1)
                {
                    nearestNeighbors[vector][toRemove] = nearestNeighbors[vector][i];
                    nextNearestNabour[vector]          = i + 1;
                    associates[nearestNeighbors[vector][i]].Add(vector);
                    return;
                }
            }

            int vectorsFound;
            var neabours = finder.Find(isInDaaset, vector, out vectorsFound);
            nearestNeighbors[vector][toRemove] = neabours[0];
            associates[neabours[0]].Add(vector);
            nextNearestNabour[vector]   = K + 1;
            nearestNaboursSizes[vector] = K + vectorsFound;
            Array.Copy(
                neabours,
                1,
                nearestNeighbors[vector],
                K + 1,
                neabours.Length - 1
                );
        };



        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < K; j++)
            {
                associates[nearestNeighbors[i][j]].Add(i);
            }
        }


        for (int i = 0; i < len; i++)
        {
            int correctCount = 0;
            for (int j = 0; j < associates[i].Count; j++)
            {
                correctCount += isClasifiedCorectly(associates[i][j], i);
            }
            if (correctCount >= (associates[i].Count - correctCount))
            {
                isInDaaset[i] = 0;
                for (int j = 0; j < associates[i].Count; j++)
                {
                    findNewNearestNeabout(associates[i][j], i);
                }
            }
        }

        List <int> indecesInDataSet = new List <int>();

        for (int i = 0; i < isInDaaset.Length; i++)
        {
            if (isInDaaset[i] == 1)
            {
                indecesInDataSet.Add(host.OrginalIndeces[i]);
            }
        }


        finder.Dispose();

        return(indecesInDataSet.ToArray());
    }
 public static void SortDataDesc(HostDataset host, int[][] nearestNeabours, float[] sortBy)
 {
     Sorting.SortDesc(host, sortBy);
     Sorting.sortAndRemapDesc(nearestNeabours, sortBy);
 }