Example #1
0
        public override float Product(SparseVec element1, SparseVec element2)
        {

            float chi = ChiSquaredKernel.ChiSquareDist(element1, element2);

            return (float)Math.Exp(-Gamma * chi);

        }
Example #2
0
 /// <summary>
 /// create dense vector based on sparese vector <see cref="mainVec"/>
 /// </summary>
 /// <param name="mainVec"></param>
 /// <param name="fillVector"></param>
 public static void FillDenseVector(SparseVec mainVec, float[] fillVector)
 {
     Array.Clear(fillVector, 0, fillVector.Length);
     for (int j = 0; j < mainVec.Count; j++)
     {
         int idx = mainVec.Indices[j];
         float val = (float)mainVec.Values[j];
         fillVector[idx] = val;
     }
 }
Example #3
0
 /// <summary>
 /// create dense vector based on sparese vector <see cref="mainVec"/>
 /// </summary>
 /// <param name="mainVec"></param>
 /// <param name="fillVector"></param>
 public static void FillDenseVector(SparseVec mainVec, float[] fillVector)
 {
     Array.Clear(fillVector, 0, fillVector.Length);
     for (int j = 0; j < mainVec.Count; j++)
     {
         int   idx = mainVec.Indices[j];
         float val = (float)mainVec.Values[j];
         fillVector[idx] = val;
     }
 }
Example #4
0
        /// <summary>
        /// Convert sparse vectors into CSR fromat (three array, one for values, one for indexes and one for vector pointers)
        /// </summary>
        /// <param name="vecVals"></param>
        /// <param name="vecIdx"></param>
        /// <param name="vecLenght"></param>
        /// <param name="problemElements"></param>
        public static void TransformToCSRFormat(out float[] vecVals, out int[] vecIdx, out int[] vecLenght, SparseVec[] problemElements)
        {
            //transform elements to specific array format -> CSR http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_.28CSR_or_CRS.29
            int avgVectorLenght = problemElements[0].Count;
            //list for all vectors values
            List<float> vecValsL = new List<float>(problemElements.Length * avgVectorLenght);

            //list for all vectors indexes
            List<int> vecIdxL = new List<int>(problemElements.Length * avgVectorLenght);

            //list of lenght of each vector, list of pointers
            List<int> vecLenghtL = new List<int>(problemElements.Length);

            //arrays for values, indexes and lenght

            int vecStartIdx = 0;
            // Stopwatch timer = Stopwatch.StartNew();
            for (int i = 0; i < problemElements.Length; i++)
            {
                var vec = problemElements[i];


                //!!!vector  not always has only one zero element at the end
                // mValues and mIndices have extra zero elements at the end, so 
                //after conversion we have to remove zeros from the end

                //coping and converting from double to float using Linq
                //var converted = vec.mValues.Take(vec.mValueCount).Select(x => Convert.ToSingle(x));
                //var converted = vec.mValues.Select(x => Convert.ToSingle(x)).Take(vec.mValueCount);
                //Array.ConstrainedCopy(vec.mValues, 0, vecVals, 0, vec.mValueCount);

                vecValsL.AddRange(vec.Values);

                vecIdxL.AddRange(vec.Indices);


                vecLenghtL.Add(vecStartIdx);
                vecStartIdx += vec.Count;
            }
            //  timer.Stop();


            //for last index
            vecLenghtL.Add(vecStartIdx);

            //convert list to arrays
            vecVals = vecValsL.ToArray();
            vecIdx = vecIdxL.ToArray();
            vecLenght = vecLenghtL.ToArray();

            //set list reference to null to free memeory
            vecIdxL = null;
            vecLenghtL = null;
            vecValsL = null;
        }
Example #5
0
 /// <summary>
 /// set the value on position which are the same as sparse vector indexes
 /// </summary>
 /// <param name="sparseVector"></param>
 /// <param name="bufferPtr"></param>
 /// <param name="value"></param>
 internal static void SetBufferIdx(SparseVec sparseVector, IntPtr bufferPtr, float value)
 {
     unsafe
     {
         float *vecPtr = (float *)bufferPtr.ToPointer();
         for (int j = 0; j < sparseVector.Count; j++)
         {
             int idx = sparseVector.Indices[j];
             vecPtr[idx] = value;
         }
     }
 }
Example #6
0
        public override float Product(SparseVec element1, SparseVec element2)
        {

            float x1Squere = linKernel.Product(element1, element1);
            float x2Squere = linKernel.Product(element2, element2);

            float dot = linKernel.Product(element1, element2);

            float prod = (float)Math.Exp(-Gamma * (x1Squere + x2Squere - 2 * dot));

            return prod;

        }
Example #7
0
        internal static void InitBuffer(SparseVec sparseVector, IntPtr bufferPtr)
        {
            unsafe
            {
                float *vecPtr = (float *)bufferPtr.ToPointer();

                for (int j = 0; j < sparseVector.Count; j++)
                {
                    int   idx = sparseVector.Indices[j];
                    float val = (float)sparseVector.Values[j];
                    vecPtr[idx] = val;
                }
            }
        }
Example #8
0
        public static void TransformToCSCFormat2(out float[] vecVals, out int[] vecIdx, out int[] vecLenght, SparseVec[] problemElements)
        {
            //transform elements to specific array format -> CSR http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_.28CSR_or_CRS.29
            int avgVectorLenght = problemElements[0].Count;
            int Dim = problemElements[0].Dim;

            List<int>[] colIdx = new List<int>[Dim + 1];
            List<float>[] colVals = new List<float>[Dim + 1];
            for (int i = 1; i <= Dim; i++)
            {
                colIdx[i] = new List<int>();
                colVals[i] = new List<float>();
            }

            //list for all vectors values
            List<float> vecValsL = new List<float>(problemElements.Length * avgVectorLenght);
            //list for all vectors indexes
            List<int> vecIdxL = new List<int>(problemElements.Length * avgVectorLenght);
            //list of lenght of each vector, list of pointers
            List<int> vecLenghtL = new List<int>(Dim + 1);

            for (int k = 0; k < problemElements.Length; k++)
            {
                var vec = problemElements[k];

                for (int s = 0; s < vec.Count; s++)
                {
                    int idx = vec.Indices[s];
                    float val = vec.Values[s];

                    colIdx[idx].Add(k);
                    colVals[idx].Add(val);
                }
            }


            int curColSize = 0;

            vecLenghtL.Add(curColSize);
            for (int i = 1; i <= Dim; i++)
            {
                curColSize += colIdx[i].Count;
                vecLenghtL.Add(curColSize);
                vecIdxL.AddRange(colIdx[i]);
                vecValsL.AddRange(colVals[i]);

                colIdx[i] = null;
                colVals[i] = null;
            }



            //convert list to arrays
            vecVals = vecValsL.ToArray();
            vecIdx = vecIdxL.ToArray();
            vecLenght = vecLenghtL.ToArray();

            //set list reference to null to free memeory
            vecIdxL = null;
            vecLenghtL = null;
            vecValsL = null;
        }
Example #9
0
        /// <summary>
        /// Convert sparse vectors into compact sparse column(CSC) fromat (three array, one for values, one for indexes and one for vector pointers)
        /// 
        /// 
        /// </summary>
        /// <param name="vecVals"></param>
        /// <param name="vecIdx"></param>
        /// <param name="vecLenght"></param>
        /// <param name="problemElements"></param>
        public static void TransformToCSCFormat(out float[] vecVals, out int[] vecIdx, out int[] vecLenght, SparseVec[] problemElements)
        {
            //transform elements to specific array format -> CSR http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_.28CSR_or_CRS.29
            int avgVectorLenght = problemElements[0].Count;
            //list for all vectors values
            List<float> vecValsL = new List<float>(problemElements.Length * avgVectorLenght);

            //list for all vectors indexes
            List<int> vecIdxL = new List<int>(problemElements.Length * avgVectorLenght);


            int Dim = problemElements[0].Dim;
            //list of lenght of each vector, list of pointers
            List<int> vecLenghtL = new List<int>(Dim + 1);

            //arrays for values, indexes and lenght
            int[] elementsCheckedDims = new int[problemElements.Length];
            int vecStartIdx = 0;
            int curColSize = 0;

            for (int i = 1; i <= Dim; i++)
            {
                curColSize = 0;
                for (int k = 0; k < problemElements.Length; k++)
                {
                    var vec = problemElements[k];

                    int s = elementsCheckedDims[k];

                    //find max index 
                    while (s < vec.Indices.Length && vec.Indices[s] < i)
                    {
                        s++;
                    }

                    // int val = Array.BinarySearch(vec.Indices, s, vec.Indices.Length, i);

                    elementsCheckedDims[k] = s;
                    if (s < vec.Indices.Length && vec.Indices[s] == i)
                    {
                        //insert in to vals and idx
                        vecValsL.Add(vec.Values[s]);
                        vecIdxL.Add(k);//k-th vector in k- column
                        curColSize++;

                    }

                }
                vecLenghtL.Add(vecStartIdx);
                vecStartIdx += curColSize;
            }

            //for last index
            vecLenghtL.Add(vecStartIdx);

            //convert list to arrays
            vecVals = vecValsL.ToArray();
            vecIdx = vecIdxL.ToArray();
            vecLenght = vecLenghtL.ToArray();

            //set list reference to null to free memeory
            vecIdxL = null;
            vecLenghtL = null;
            vecValsL = null;
        }
Example #10
0
 protected override void SetCudaEvalFuncParamsForVector(SparseVec vec)
 {
     cuda.SetParameter(cuFuncEval, vectorSelfDotParamOffset, vec.DotProduct());
 }
Example #11
0
        /// <summary>
        /// Predicts the specified elements.
        /// </summary>
        /// <param name="elements">The elements.</param>
        /// <returns>array of predicted labels +1 or -1</returns>
        public override float[] Predict(SparseVec[] elements)
        {
            if (!IsInitialized)
                throw new ApplicationException("Evaluator is not initialized. Call init method");


            //tranfsorm elements to matrix in CSR format
            // elements values
            float[] vecVals;
            //elements indexes
            int[] vecIdx;
            //elements lenght
            int[] vecLenght;
            CudaHelpers.TransformToCSRFormat(out vecVals, out vecIdx, out vecLenght, elements);

            //copy data to device, set cuda function parameters
            valsPtr = cuda.CopyHostToDevice(vecVals);
            idxPtr = cuda.CopyHostToDevice(vecIdx);
            vecLenghtPtr = cuda.CopyHostToDevice(vecLenght);

            //release arrays
            vecVals = null;
            vecIdx = null;
            vecLenght = null;

            uint memElementsSize = (uint)(elements.Length * sizeof(float));
            //allocate mapped memory for our results
            outputIntPtr = cuda.HostAllocate(memElementsSize, CUDADriver.CU_MEMHOSTALLOC_DEVICEMAP);
            outputPtr = cuda.GetHostDevicePointer(outputIntPtr, 0);

            //outputPtr = cuda.Allocate(memElementsSize);

            // Set the cuda kernel paramerters
            #region set cuda parameters
            uint Rows = (uint)elements.Length;
            uint Cols = (uint)TrainedModel.SupportElements.Length;


            cuda.SetFunctionBlockShape(cuFunc, blockSizeX, blockSizeY, 1);

            int offset = 0;
            //set elements param
            cuda.SetParameter(cuFunc, offset, valsPtr.Pointer);
            offset += IntPtr.Size;
            cuda.SetParameter(cuFunc, offset, idxPtr.Pointer);
            offset += IntPtr.Size;
            cuda.SetParameter(cuFunc, offset, vecLenghtPtr.Pointer);
            offset += IntPtr.Size;

            //set labels param
            cuda.SetParameter(cuFunc, offset, labelsPtr.Pointer);
            offset += IntPtr.Size;
            //set alphas param
            cuda.SetParameter(cuFunc, offset, alphasPtr.Pointer);
            offset += IntPtr.Size;
            //set output (reslut) param
            cuda.SetParameter(cuFunc, offset, outputPtr.Pointer);
            offset += IntPtr.Size;
            //set number of elements param
            cuda.SetParameter(cuFunc, offset, (uint)Rows);
            offset += sizeof(int);
            //set number of support vectors param
            cuda.SetParameter(cuFunc, offset, (uint)Cols);
            offset += sizeof(int);
            //set support vector index param
            lastParameterOffset = offset;
            cuda.SetParameter(cuFunc, offset, (uint)0);
            offset += sizeof(int);
            cuda.SetParameterSize(cuFunc, (uint)offset);
            #endregion

            int gridDimX = (int)Math.Ceiling((Rows + 0.0) / (blockSizeX));


            for (int k = 0; k < TrainedModel.SupportElements.Length; k++)
            {
                //set the buffer values from k-th support vector
                CudaHelpers.InitBuffer(TrainedModel.SupportElements[k], svVecIntPtrs[k % 2]);

                cuda.SynchronizeStream(stream);
                //copy asynchronously from buffer to devece
                cuda.CopyHostToDeviceAsync(mainVecPtr, svVecIntPtrs[k % 2], memSvSize, stream);
                //set the last parameter in kernel (column index)   
                // colIndexParamOffset
                cuda.SetParameter(cuFunc, lastParameterOffset, (uint)k);
                //launch kernl    
                cuda.LaunchAsync(cuFunc, gridDimX, 1, stream);

                if (k > 0)
                {
                    //clear the previous host buffer
                    CudaHelpers.SetBufferIdx(TrainedModel.SupportElements[k - 1], svVecIntPtrs[(k + 1) % 2], 0.0f);
                }

            }

            //CUdeviceptr symbolAdr;
            //CUDARuntime.cudaGetSymbolAddress(ref symbolAdr,"RHO");
            rho = TrainedModel.Bias;
            //IntPtr symbolVal = new IntPtr(&rho);
            //CUDARuntime.cudaMemcpyToSymbol("RHO", symbolVal, 1, 1, cudaMemcpyKind.cudaMemcpyHostToDevice);

            cuda.SetFunctionBlockShape(cuFuncSign, blockSizeX, blockSizeY, 1);
            int signFuncOffset = 0;
            //set array param
            cuda.SetParameter(cuFuncSign, signFuncOffset, outputPtr.Pointer);
            signFuncOffset += IntPtr.Size;
            //set size 
            cuda.SetParameter(cuFuncSign, signFuncOffset, Rows);
            signFuncOffset += sizeof(int);

            cuda.SetParameter(cuFuncSign, signFuncOffset, rho);
            signFuncOffset += sizeof(float);

            cuda.SetParameterSize(cuFuncSign, (uint)signFuncOffset);


            //gridDimX is valid for this function
            cuda.LaunchAsync(cuFuncSign, gridDimX, 1, stream);

            //wait for all computation
            cuda.SynchronizeContext();


            float[] result = new float[elements.Length];
            //copy result
            Marshal.Copy(outputIntPtr, result, 0, elements.Length);

            return result;
        }
Example #12
0
 private static void RBFProduct(SparseVec[] vectors, int mainIndex, ref float[] output)
 {
     SparseVec vec1 = vectors[mainIndex];
     float vec1Dot = DotProd(vec1, vec1);
     for (int i = 0; i < vectors.Length; i++)
     {
         float dotVecI = DotProd(vectors[i], vectors[i]);
         float dotCross = DotProd(vec1, vectors[i]);
         float sum = dotVecI + vec1Dot - 2 * dotCross;
         output[i] = (float)Math.Exp(-Gamma * sum);
     }
 }
Example #13
0
        /// <summary>
        /// Convert sparse vector into slice ellpack format, all data has column majored ordering, with group of <see cref="threadPerRow"/> elements
        /// </summary>
        /// <param name="vecVals">vector values</param>
        /// <param name="vecCols"></param>
        /// <param name="sliceStart"></param>
        /// <param name="rowLenght"></param>
        /// <param name="problemElements"></param>
        /// <param name="threadsPerRow"></param>
        /// <param name="sliceSize"></param>
        public static void TransformToSERTILP(out float[] vecVals, out int[] vecCols, out int[] sliceStart, out int[] rowLenght, SparseVec[] problemElements, int threadsPerRow, int sliceSize,int preFetch)
        {
            //int alignold = 128 *(int) Math.Ceiling((float)(sliceSize * threadsPerRow) / 128);

            int alignParam = 64;

            //int align = (int)Math.Ceiling(sliceSize * threadsPerRow / 64.0)*64;
            //int align = (int)Math.Ceiling(sliceSize * threadsPerRow / 2.0) * 2;
            int align = (int)Math.Ceiling(1.0 * sliceSize * threadsPerRow / alignParam) * alignParam;


            int numRows = problemElements.Length;
            int numSlices = (int)Math.Ceiling((numRows + 0.0) / sliceSize);

            rowLenght = new int[numRows];

            sliceStart = new int[numSlices + 1];
            //max non-zero in slice
            int[] sliceMax = new int[numSlices];

            int sliceNr = 0;
            //find max in slice
            for (int i = 0; i < numSlices; i++)
            {
                sliceMax[i] = -1;
                int idx = -1;
                for (int j = 0; j < sliceSize; j++)
                {
                    idx = j + i * sliceSize;
                    if (idx < numRows)
                    {
                        rowLenght[idx] = problemElements[idx].Count; 
                        if (sliceMax[i] < rowLenght[idx])
                        {
                            sliceMax[i] = rowLenght[idx];
                        }
                        rowLenght[idx] = (int)Math.Ceiling(1.0 * rowLenght[idx] / (threadsPerRow * preFetch));
                    }
                }
                //different than original Slice EllR-T, multiplicated by preFech
                sliceStart[i + 1] = sliceStart[i] + (int)Math.Ceiling( 1.0*sliceMax[i] /(preFetch* threadsPerRow) ) *preFetch * align;
            }

            //
            int nnzEl = sliceStart[numSlices];
            vecCols = new int[nnzEl];
            vecVals = new float[nnzEl];

            sliceNr = 0;
            int rowInSlice = 0;
            //fill slice ellpack values and cols arrays
            for (int i = 0; i < numRows; i++)
            {
                //slice number in whole dataset
                sliceNr = i / sliceSize;
                //row number  in particular slice
                rowInSlice = i % sliceSize;
                var vec = problemElements[i];

                int threadNr = -1;
                float value = 0;
                int col = -1;

                int rowSlice = -1;// (int)Math.Ceiling((0.0 + vec.Count) / threadsPerRow);
                for (int k = 0; k < vec.Count; k++)
                {
                    threadNr = k % threadsPerRow;
                    rowSlice = k / threadsPerRow;
                    value = vec.Values[k];
                    col = vec.Indices[k];
                    //eg. if sliceSize=8, threadsPerRow=4, for first vector (i=0) with size 9
                    //computed idx should be= [0 1 2 3 , 32,33,34,35, 64]
                    int idx = sliceStart[sliceNr] + align * rowSlice + rowInSlice * threadsPerRow + threadNr;

                    vecVals[idx] = value;
                    vecCols[idx] = col;
                }

            }

        }
Example #14
0
        /// <summary>
        /// Convert sparse vectors into ERTILP format, all data has collumn majored ordering
        /// 
        /// </summary>
        /// <param name="vecVals"></param>
        /// <param name="vecCols"></param>
        /// <param name="rowLength"></param>
        /// <param name="problemElements"></param>
        public static void TransformToERTILPFormat(out float[] vecVals, out int[] vecCols, out int[] rowLength, SparseVec[] problemElements, int align, int ThreadsPerRow)
        {
            int maxEl = 1;

            maxEl = (from m in problemElements
                     select m.Count).AsParallel().Max();

            //if align not divide maxEl
            var rest = maxEl % align;
            // we add small number in order to maxEll was divided by align
            if (rest != 0)
                maxEl = maxEl + align - rest;


            double avgEl = (from t in problemElements
                            select t.Count).Average();

            int numRows = problemElements.Length;
            //2d array stored in 1d array
            vecVals = new float[numRows * maxEl];
            vecCols = new int[numRows * maxEl];
            //1d array
            rowLength = new int[numRows];

            for (int i = 0; i < numRows; i++)
            {
                var vec = problemElements[i];

                for (int j = 0; j < vec.Count; j++)
                {
                    int k = j / ThreadsPerRow;
                    int t = j % ThreadsPerRow;
                    vecVals[k * numRows * ThreadsPerRow + i * ThreadsPerRow + t] = vec.Values[j];
                    vecCols[k * numRows * ThreadsPerRow + i * ThreadsPerRow + t] = vec.Indices[j];
                }

                rowLength[i] = (int)Math.Ceiling((vec.Count + 0.0) / align);

            }

        }
Example #15
0
        /// <summary>
        /// set the value on position which are the same as sparse vector indexes
        /// </summary>
        /// <param name="sparseVector"></param>
        /// <param name="bufferPtr"></param>
        /// <param name="value"></param>
        internal static void SetBufferIdx(SparseVec sparseVector, IntPtr bufferPtr, float value)
        {
            unsafe
            {

                float* vecPtr = (float*)bufferPtr.ToPointer();
                for (int j = 0; j < sparseVector.Count; j++)
                {
                    int idx = sparseVector.Indices[j];
                    vecPtr[idx] = value;
                }

            }
        }
Example #16
0
 public override float Product(SparseVec element1, SparseVec element2)
 {
     //return chiSquared.Product(element1, element2);
     return ChiSquaredNormKernel.ChiSquareNormDist(element1, element2);
 }
 public override float Product(SparseVec element1, SparseVec element2)
 {
     return chiSquared.Product(element1, element2);
 }
Example #18
0
 public override float Product(SparseVec element1, SparseVec element2)
 {
     return linKernel.Product(element1, element2);
 }
Example #19
0
 public override float Product(SparseVec element1, SparseVec element2)
 {
     return(linKernel.Product(element1, element2));
 }
Example #20
0
        public override Model <SparseVec> ComputeModel()
        {
            int j;
            int l      = problem.ElementsCount; //prob.l;
            int n      = problem.FeaturesCount; // prob.n;
            int w_size = n;                     // prob.n;
            Model <SparseVec> model = new Model <SparseVec>();

            model.FeaturesCount = n;

            if (bias >= 0)
            {
                //Add to each feature vector last feature ==1;
                model.FeaturesCount = n - 1;
            }
            else
            {
                model.FeaturesCount = n;
            }

            model.Bias = bias;

            int[] perm = new int[l];
            // group training data of the same class
            //GroupClassesReturn rv = groupClasses(prob, perm);
            int nr_class = 0;

            int[] label; // = new int[l];// = rv.label;
            int[] start; // = rv.start;
            int[] count; // = rv.count;

            groupClasses(problem, out nr_class, out label, out start, out count, perm);

            model.NumberOfClasses = nr_class;


            model.Labels = new float[nr_class];
            for (int i = 0; i < nr_class; i++)
            {
                model.Labels[i] = (float)label[i];
            }

            // calculate weighted C
            double[] weighted_C = new double[nr_class];
            for (int i = 0; i < nr_class; i++)
            {
                weighted_C[i] = C;
            }


            SetClassWeights(nr_class, label, weighted_C);

            // constructing the subproblem
            //permutated vectors
            SparseVec[] permVec = new SparseVec[problem.ElementsCount];
            Debug.Assert(l == problem.ElementsCount);
            for (int i = 0; i < l; i++)
            {
                permVec[i] = problem.Elements[perm[i]];
            }


            Problem <SparseVec> sub_prob = new Problem <SparseVec>();

            sub_prob.ElementsCount = l;
            sub_prob.FeaturesCount = n;
            //we set labels below
            sub_prob.Y        = new float[sub_prob.ElementsCount];
            sub_prob.Elements = permVec;

            //Initailize CUDA driver and load module
            InitCudaModule();

            if (nr_class == 2)
            {
                float[] w = new float[w_size];


                int e0 = start[0] + count[0];
                int k  = 0;
                for (; k < e0; k++)
                {
                    sub_prob.Y[k] = +1;
                }
                for (; k < sub_prob.ElementsCount; k++)
                {
                    sub_prob.Y[k] = -1;
                }

                //copy all needed data to CUDA device
                SetCudaData(sub_prob);

                //Fill data on CUDA
                FillDataOnCuda(sub_prob, w, weighted_C[0], weighted_C[1]);

                Stopwatch solverTime = Stopwatch.StartNew();
                solve_l2r_l2_bb_svc_cuda(sub_prob, w, epsilon, weighted_C[0], weighted_C[1]);
                //solve_l2r_l1l2_svc(model.W, epsilon, weighted_C[0], weighted_C[1], solverType);
                solverTime.Stop();
                Console.WriteLine("------ solver time {0}", solverTime.Elapsed);

                model.W = new double[w_size];
                for (int s = 0; s < w.Length; s++)
                {
                    model.W[s] = w[s];
                }
            }
            else
            {
                model.W = new double[w_size * nr_class];
                float[] w = new float[w_size];

                SetCudaData(sub_prob);

                ///one against many
                for (int i = 0; i < nr_class; i++)
                {
                    int si = start[i];
                    int ei = si + count[i];

                    int k = 0;
                    for (; k < si; k++)
                    {
                        sub_prob.Y[k] = -1;
                    }
                    for (; k < ei; k++)
                    {
                        sub_prob.Y[k] = +1;
                    }
                    for (; k < sub_prob.ElementsCount; k++)
                    {
                        sub_prob.Y[k] = -1;
                    }

                    FillDataOnCuda(sub_prob, w, weighted_C[i], C);
                    //train_one(sub_prob, param, w, weighted_C[i], param.C);
                    solve_l2r_l2_bb_svc_cuda(sub_prob, w, epsilon, weighted_C[i], C);

                    for (j = 0; j < n; j++)
                    {
                        model.W[j * nr_class + i] = w[j];
                    }
                }
            }

            DisposeCuda();

            return(model);
        }
Example #21
0
 public override float Product(SparseVec element1, SparseVec element2)
 {
     return(chiSquared.Product(element1, element2));
 }
Example #22
0
        /// <summary>
        /// Convert sparse vectors into ellpack-r format, all data has collumn majored ordering
        /// 
        /// </summary>
        /// <param name="vecVals"></param>
        /// <param name="vecCols"></param>
        /// <param name="rowLength"></param>
        /// <param name="problemElements"></param>
        public static void TransformToEllpackRFormat(out float[] vecVals, out int[] vecCols, out int[] rowLength, SparseVec[] problemElements)
        {
            int maxEl = 1;
            //for (int i = 0; i < problemElements.Length; i++)
            //{
            //    if(maxEl<problemElements[i].Count)
            //        maxEl=problemElements[i].Count;
            //}
            maxEl = (from m in problemElements
                     select m.Count).AsParallel().Max();

            double avgEl = (from t in problemElements
                            select t.Count).Average();

            int numRows = problemElements.Length;
            //2d array stored in 1d array
            vecVals = new float[numRows * maxEl];
            vecCols = new int[numRows * maxEl];
            //1d array
            rowLength = new int[numRows];

            for (int i = 0; i < numRows; i++)
            {
                var vec = problemElements[i];
                for (int j = 0; j < vec.Count; j++)
                {
                    vecVals[j * numRows + i] = vec.Values[j];
                    vecCols[j * numRows + i] = vec.Indices[j];
                }
                rowLength[i] = vec.Count;
            }

        }
Example #23
0
        private static void NormalSparseDotProduct(SparseVec[] vectors, int mainIndex, ref float[] output)
        {
            SparseVec vec1 = vectors[mainIndex];

            for (int i = 0; i < vectors.Length; i++)
            {
                output[i] = DotProd(vec1, vectors[i]);
            }
        }
Example #24
0
        private static float DotProd(SparseVec vec1, SparseVec vec2)
        {
            int i1 = 0;
            int i2 = 0;
            float result = 0;

            while (i1 < vec1.size && i2 < vec2.size)
            {
                int index1 = vec1.indices[i1];
                int index2 = vec2.indices[i2];

                if (index1 == index2)
                {
                    result += vec1.values[i1] * vec2.values[i2];
                    i1++; i2++;
                }
                else if (index1 < index2)
                {
                    i1++;
                }
                else
                {
                    i2++;
                }
            }

            return result;
        }
Example #25
0
        private static float[] NormalRBFDotProd()
        {
            //always the same values
            Random rnd = new Random(1);

            SparseVec[] vectors = new SparseVec[N];
            mainIndex = StartingIndex;

            Console.WriteLine("array init ");
            Stopwatch t = Stopwatch.StartNew();
            for (int i = 0; i < N; i++)
            {
                vectors[i] = new SparseVec();

                int vecSize = avgElements + i % stdElements;
                vectors[i].size = vecSize;
                float[] vals = Helpers.InitValues(i, vecSize, maxVal);

                int[] index = Helpers.InitIndices(i, vecSize, ref maxIndex);

                vectors[i].indices = index;
                vectors[i].values = vals;
            }
            Console.WriteLine("init takes {0}", t.Elapsed);
            float[] output = new float[N];

            Stopwatch timer = Stopwatch.StartNew();

            RBFProduct(vectors, mainIndex, ref output);

            timer.Stop();

            Console.Write("RBF kernel with mainIndex {0} and {1}-vectors takes {2}", mainIndex, N, timer.Elapsed);

            int lenght = Math.Min(displayCount, N);
            Console.WriteLine();
            for (int i = 0; i < lenght; i++)
            {
                Console.WriteLine("{0}-{1}", i, output[i]);
            }

            return output;
        }
Example #26
0
 public override float Product(SparseVec element1, SparseVec element2)
 {
     //return chiSquared.Product(element1, element2);
     return(ChiSquaredNormKernel.ChiSquareNormDist(element1, element2));
 }
Example #27
0
        internal static void InitBuffer(SparseVec sparseVector, IntPtr bufferPtr)
        {
            unsafe
            {

                float* vecPtr = (float*)bufferPtr.ToPointer();

                for (int j = 0; j < sparseVector.Count; j++)
                {
                    int idx = sparseVector.Indices[j];
                    float val = (float)sparseVector.Values[j];
                    vecPtr[idx] = val;


                }

            }
        }
Example #28
0
 protected override void SetCudaEvalFuncParamsForVector(SparseVec vec)
 {
     cuda.SetParameter(cuFuncEval, vectorSelfSumParamOffset, vec.Values.Sum());
 }
Example #29
0
        public override Model<SparseVec> ComputeModel()
        {

            int j;
            int l = problem.ElementsCount; //prob.l;
            int n = problem.FeaturesCount;// prob.n;
            int w_size = n; // prob.n;
            Model<SparseVec> model = new Model<SparseVec>();
            model.FeaturesCount = n;

            if (bias >= 0)
            {
                //Add to each feature vector last feature ==1;
                model.FeaturesCount = n - 1;
            }
            else
                model.FeaturesCount = n;

            model.Bias = bias;

            int[] perm = new int[l];
            // group training data of the same class
            //GroupClassesReturn rv = groupClasses(prob, perm);
            int nr_class = 0;
            int[] label;// = new int[l];// = rv.label;
            int[] start;// = rv.start;
            int[] count;// = rv.count;

            groupClasses(problem, out nr_class, out label, out start, out count, perm);

            model.NumberOfClasses = nr_class;


            model.Labels = new float[nr_class];
            for (int i = 0; i < nr_class; i++)
                model.Labels[i] = (float)label[i];

            // calculate weighted C
            double[] weighted_C = new double[nr_class];
            for (int i = 0; i < nr_class; i++)
            {
                weighted_C[i] = C;
            }

           
            SetClassWeights(nr_class, label, weighted_C);
           
            // constructing the subproblem
            //permutated vectors
            SparseVec[] permVec = new SparseVec[problem.ElementsCount];
            Debug.Assert(l == problem.ElementsCount);
            for (int i = 0; i < l; i++)
            {
                permVec[i] = problem.Elements[perm[i]];
            }


            Problem<SparseVec> sub_prob = new Problem<SparseVec>();
            sub_prob.ElementsCount = l;
            sub_prob.FeaturesCount = n;
            //we set labels below
            sub_prob.Y = new float[sub_prob.ElementsCount];
            sub_prob.Elements = permVec;

            //Initailize CUDA driver and load module
            InitCudaModule();

            if (nr_class == 2)
            {

                float[] w = new float[w_size];


                int e0 = start[0] + count[0];
                int k = 0;
                for (; k < e0; k++)
                    sub_prob.Y[k] = +1;
                for (; k < sub_prob.ElementsCount; k++)
                    sub_prob.Y[k] = -1;

                Debug.WriteLine("init data on cuda");
                //copy all needed data to CUDA device
                SetCudaData(sub_prob);
                Debug.WriteLine("set cuda data complete");
                //Fill data on CUDA
                FillDataOnCuda(sub_prob, w, weighted_C[0], weighted_C[1]);

                Stopwatch solverTime = Stopwatch.StartNew();
                solve_l2r_l2_bb_svc_cuda(sub_prob, w, epsilon, weighted_C[0], weighted_C[1]);
                //solve_l2r_l1l2_svc(model.W, epsilon, weighted_C[0], weighted_C[1], solverType);
                solverTime.Stop();
                Console.WriteLine("------ solver time {0}", solverTime.Elapsed);

                model.W = new double[w_size];
                for (int s = 0; s < w.Length; s++)
                {
                    model.W[s] = w[s];
                }
            }
            else
            {
                model.W = new double[w_size * nr_class];
                float[] w = new float[w_size];

                SetCudaData(sub_prob);

                ///one against many
                for (int i = 0; i < nr_class; i++)
                {
                    int si = start[i];
                    int ei = si + count[i];

                    int k = 0;
                    for (; k < si; k++)
                        sub_prob.Y[k] = -1;
                    for (; k < ei; k++)
                        sub_prob.Y[k] = +1;
                    for (; k < sub_prob.ElementsCount; k++)
                        sub_prob.Y[k] = -1;

                    FillDataOnCuda(sub_prob, w, weighted_C[i], C);
                    //train_one(sub_prob, param, w, weighted_C[i], param.C);
                    solve_l2r_l2_bb_svc_cuda(sub_prob, w, epsilon, weighted_C[i], C);

                    for (j = 0; j < n; j++)
                        model.W[j * nr_class + i] = w[j];
                }
            }

            
             DisposeCuda();

            return model;
        }
Example #30
0
        public override Model<SparseVec> ComputeModel()
        {

            int j;
            int l = problem.ElementsCount; //prob.l;
            int n = problem.FeaturesCount;// prob.n;
            int w_size = n; // prob.n;
            Model<SparseVec> model = new Model<SparseVec>();
            model.FeaturesCount = n;

            if (bias >= 0)
            {
                //Add to each feature vector last feature ==1;
                model.FeaturesCount = n - 1;
            }
            else
                model.FeaturesCount = n;

            model.Bias = bias;

            int[] perm = new int[l];
            // group training data of the same class
            //GroupClassesReturn rv = groupClasses(prob, perm);
            int nr_class = 0;
            int[] label;// = new int[l];// = rv.label;
            int[] start;// = rv.start;
            int[] count;// = rv.count;

            groupClasses(problem, out nr_class, out label, out start, out count, perm);

            model.NumberOfClasses = nr_class;


            model.Labels = new float[nr_class];
            for (int i = 0; i < nr_class; i++)
                model.Labels[i] = (float)label[i];

            // calculate weighted C
            double[] weighted_C = new double[nr_class];
            for (int i = 0; i < nr_class; i++)
            {
                weighted_C[i] = C;
            }


            SetClassWeights(nr_class, label, weighted_C);

            // constructing the subproblem
            //permutated vectors
            SparseVec[] permVec = new SparseVec[problem.ElementsCount];
            Debug.Assert(l == problem.ElementsCount);
            for (int i = 0; i < l; i++)
            {
                permVec[i] = problem.Elements[perm[i]];
            }


            Problem<SparseVec> sub_prob = new Problem<SparseVec>();
            sub_prob.ElementsCount = l;
            sub_prob.FeaturesCount = n;
            //we set labels below
            sub_prob.Y = new float[sub_prob.ElementsCount];
            sub_prob.Elements = permVec;


            if (nr_class == 2)
            {

                float[] w = new float[w_size];
                //for (int z = 0; z < w.Length; z++)
                //{
                //    w[z] = 1.0f;
                //}


                int e0 = start[0] + count[0];
                int k = 0;
                for (; k < e0; k++)
                    sub_prob.Y[k] = +1;
                for (; k < sub_prob.ElementsCount; k++)
                    sub_prob.Y[k] = -1;



                solve_l2r_l2_svc_parallel(sub_prob, w, epsilon, weighted_C[0], weighted_C[1]);
                //solve_l2r_l1l2_svc(model.W, epsilon, weighted_C[0], weighted_C[1], solverType);

                model.W = new double[w_size];
                for (int s = 0; s < w.Length; s++)
                {
                    model.W[s] = w[s];
                }
            }
            else
            {
                model.W = new double[w_size * nr_class];
                float[] w = new float[w_size];


                ///one against many
                for (int i = 0; i < nr_class; i++)
                {
                    int si = start[i];
                    int ei = si + count[i];

                    int k = 0;
                    for (; k < si; k++)
                        sub_prob.Y[k] = -1;
                    for (; k < ei; k++)
                        sub_prob.Y[k] = +1;
                    for (; k < sub_prob.ElementsCount; k++)
                        sub_prob.Y[k] = -1;

                    //train_one(sub_prob, param, w, weighted_C[i], param.C);
                    solve_l2r_l2_svc_parallel(sub_prob, w, epsilon, weighted_C[i], C);

                    for (j = 0; j < n; j++)
                        model.W[j * nr_class + i] = w[j];
                }
            }



            return model;
        }