Exemplo n.º 1
0
        public void Train(ILabeledExampleCollection <double, SparseVector <double> > dataset)
        {
            Utils.ThrowException(dataset == null ? new ArgumentNullException("dataset") : null);
            Utils.ThrowException(dataset.Count == 0 ? new ArgumentValueException("dataset") : null);
            LSqrSparseMatrix mat = new LSqrSparseMatrix(dataset.Count);

            double[] rhs     = new double[dataset.Count];
            int      solSize = -1;
            int      i       = 0;

            foreach (LabeledExample <double, SparseVector <double> > labeledExample in dataset)
            {
                if (labeledExample.Example.LastNonEmptyIndex + 1 > solSize)
                {
                    solSize = labeledExample.Example.LastNonEmptyIndex + 1;
                }
                foreach (IdxDat <double> item in labeledExample.Example)
                {
                    mat.InsertValue(i, item.Idx, item.Dat);
                }
                rhs[i++] = labeledExample.Label;
            }
            Utils.ThrowException((mInitSol != null && mInitSol.Length != solSize) ? new ArgumentValueException("InitialSolution") : null);
            LSqrSparseMatrix matT = new LSqrSparseMatrix(solSize);

            i = 0;
            foreach (LabeledExample <double, SparseVector <double> > labeledExample in dataset)
            {
                foreach (IdxDat <double> item in labeledExample.Example)
                {
                    matT.InsertValue(item.Idx, i, item.Dat);
                }
                i++;
            }
            int numIter = mNumIter < 0 ? solSize + dataset.Count + 50 : mNumIter;

            mSol = new ArrayList <double>(LSqrDll.DoLSqr(solSize, mat, matT, mInitSol, rhs, numIter));
            mat.Dispose();
            matT.Dispose();
        }
Exemplo n.º 2
0
        public void Train(IExampleCollection <double, SparseVector <double> .ReadOnly> dataset)
        {
            Utils.ThrowException(dataset == null ? new ArgumentNullException("dataset") : null);
            Utils.ThrowException(dataset.Count == 0 ? new ArgumentValueException("dataset") : null);
            LSqrSparseMatrix mat = new LSqrSparseMatrix(dataset.Count);

            double[] rhs      = new double[dataset.Count];
            int      sol_size = -1;
            int      i        = 0;

            foreach (LabeledExample <double, SparseVector <double> .ReadOnly> labeled_example in dataset)
            {
                if (labeled_example.Example.LastNonEmptyIndex + 1 > sol_size)
                {
                    sol_size = labeled_example.Example.LastNonEmptyIndex + 1;
                }
                foreach (IdxDat <double> item in labeled_example.Example)
                {
                    mat.InsertValue(i, item.Idx, item.Dat);
                }
                rhs[i++] = labeled_example.Label;
            }
            LSqrSparseMatrix mat_t = new LSqrSparseMatrix(sol_size);

            i = 0;
            foreach (LabeledExample <double, SparseVector <double> .ReadOnly> labeled_example in dataset)
            {
                foreach (IdxDat <double> item in labeled_example.Example)
                {
                    mat_t.InsertValue(item.Idx, i, item.Dat);
                }
                i++;
            }
            int num_iter = m_num_iter < 0 ? sol_size + dataset.Count + 50 : m_num_iter;

            m_sol = new ArrayList <double>(LSqrDll.DoLSqr(sol_size, mat, mat_t, rhs, num_iter));
            mat.Dispose();
            mat_t.Dispose();
        }